適用於:✅Microsoft Fabric 中的 SQL 資料庫
您可以使用 Fabric REST API 來啟動和停止將 SQL 資料庫鏡像到 Fabric 環境中的 OneLake。 本文和範例腳本示範如何使用 PowerShell 呼叫 Fabric REST API 啟動或停止鏡像。
在預設情況下,OneLake 的 SQL 資料庫鏡像會一直運行。 在某些情況下,可能需要停止 Fabric 中 SQL 資料庫的鏡像。 例如,若要啟用現有資料表上叢集資料行索引的建立,該索引無法在資料庫鏡像執行時建立。
先決條件
- 您需要具備現有的 Fabric 容量。 如果您不這麼做, 請啟動 Fabric 試用版。
- 您可以使用現有的工作區或 建立新的 Fabric 工作區。
- 您必須是 工作區管理員或成員角色 的成員,才能建立 SQL 資料庫。
- 安裝 SQLCMD 的 golang 版本。 在 Windows 上執行
winget install sqlcmd以安裝。 如需其他操作系統,請參閱 aka.ms/go-sqlcmd。 - PowerShell 5.1 或 PowerShell 7.4 和更新版本
- Az PowerShell 模組。 在 PowerShell 中執行
Install-Module az以安裝。
停止將 SQL 資料庫鏡像至 Fabric 中的 OneLake
下列 PowerShell 範例會停止將 SQL 資料庫鏡像至 Fabric 中的 OneLake。
此範例文本會使用 Connect-AzAccount的別名 az login 來提示輸入認證。 它會使用這些認證來取得存取令牌,以用於 REST API 呼叫。 SQLCMD 使用提供給 Connect-AzAccount 的帳戶上下文。
在下列指令碼中,您需要提供工作區識別碼和資料庫識別碼。 兩者都可以在 URL 中找到。
https://powerbi.com/groups/<fabric_workspace_id>/sqldatabases/<fabric_sql_database_id>。 URL 中的第一個字串是 Fabric 工作區識別碼,第二個字串是 SQL 資料庫識別碼。
- 請將
<your workspace id>替換為您的 Fabric 工作區識別碼。 您可以在 URL 中輕鬆找到工作區的識別碼,這是瀏覽器視窗中之後/兩/groups/個字元內的唯一字串。 - 將
<your database id>替換為 SQL 資料庫的 Fabric 資料庫識別碼。 您可以在 URL 中輕鬆找到資料庫項目的 ID,它是瀏覽器視窗中兩個/字元/sqldatabases/內的唯一字串。
此文稿示範:
- 使用 Get-AzAccessToken 取得存取令牌 ,並 將其從安全字串轉換。 如果使用 PowerShell 7,ConvertFrom-SecureString 也是一個選項。
- 組合 API 呼叫。
- 發起 API 呼叫
Import-Module Az.Accounts
az login
$workspaceid = '<your workspace id>' # Find in the URL
$databaseid = '<your database id>' # Find in the URL
$headers = $null
# 1. Get the access token and add it to the headers
$access_token = (Get-AzAccessToken -AsSecureString -ResourceUrl https://api.fabric.microsoft.com)
$ssPtr = [System.Runtime.InteropServices.Marshal]::SecureStringToBSTR($access_token.Token)
try {
$headers = @{
Authorization = $access_token.Type + ' ' + ([System.Runtime.InteropServices.Marshal]::PtrToStringBSTR($ssPtr))
}
$access_token.UserId -match('^[^@]+') | Out-Null
$stopMirroringUri = "https://api.fabric.microsoft.com/v1/workspaces/$workspaceid/sqlDatabases/$databaseid/stopMirroring"
$parameters = @{
Method="Post"
Headers=$headers
Uri = $stopMirroringUri
}
Invoke-RestMethod @parameters -ErrorAction Stop
} finally {
# The following lines ensure that sensitive data is not left in memory.
$headers = [System.Runtime.InteropServices.Marshal]::ZeroFreeBSTR($ssPtr)
}
開始將 SQL 資料庫同步至 Fabric 的 OneLake
下列 PowerShell 範例會開始將 SQL 資料庫鏡像至 Fabric 中的 OneLake。
此範例文本會使用 Connect-AzAccount的別名 az login 來提示輸入認證。 它會使用這些認證來取得存取令牌,以用於 REST API 呼叫。 SQLCMD 使用提供給 Connect-AzAccount 的帳戶上下文。
在下列腳本中,將 <your workspace id> 替換為您的 Fabric 工作區識別碼。
您可以在 URL 中輕鬆找到工作區的識別碼,這是瀏覽器視窗中之後/兩/groups/個字元內的唯一字串。 例如,在 11aa111-a11a-1111-1abc-aa1111aaaa 中的 https://fabric.microsoft.com/groups/11aa111-a11a-1111-1abc-aa1111aaaa/。
此文稿示範:
- 使用 Get-AzAccessToken 取得存取令牌 ,並 將其從安全字串轉換。 如果使用 PowerShell 7,ConvertFrom-SecureString 也是一個選項。
- 組合 API 呼叫。
- 呼叫 API
Import-Module Az.Accounts
az login
$workspaceid = '<your workspace id>' # Find in the URL
$databaseid = '<your database id>' # Find in the URL
$headers = $null
# 1. Get the access token and add it to the headers
$access_token = (Get-AzAccessToken -AsSecureString -ResourceUrl https://api.fabric.microsoft.com)
$ssPtr = [System.Runtime.InteropServices.Marshal]::SecureStringToBSTR($access_token.Token)
try {
$headers = @{
Authorization = $access_token.Type + ' ' + ([System.Runtime.InteropServices.Marshal]::PtrToStringBSTR($ssPtr))
}
$access_token.UserId -match('^[^@]+') | Out-Null
$startMirroringUri = "https://api.fabric.microsoft.com/v1/workspaces/$workspaceid/sqlDatabases/$databaseid/startMirroring"
$parameters = @{
Method="Post"
Headers=$headers
Uri = $startMirroringUri
}
Invoke-RestMethod @parameters -ErrorAction Stop
} finally {
# The following lines ensure that sensitive data is not left in memory.
$headers = [System.Runtime.InteropServices.Marshal]::ZeroFreeBSTR($ssPtr)
}