适用于:✅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以安装。 
在 Fabric 中停止将 SQL 数据库镜像到 OneLake
以下 PowerShell 示例停止将 SQL 数据库镜像到 Fabric 中的 OneLake。
此示例脚本使用 Connect-AzAccount(az login 的别名)来提示输入凭据。 它使用这些凭据获取用于 REST API 调用的访问令牌。 SQLCMD 使用提供给 Connect-AzAccount 的帐户的上下文。
在以下脚本中,需要提供工作区 ID 和数据库 ID。 这两者都可以在 URL 中找到。 
              https://powerbi.com/groups/<fabric_workspace_id>/sqldatabases/<fabric_sql_database_id>。 URL 中的第一个字符串是 Fabric 工作区 ID,第二个字符串是 SQL 数据库 ID。
- 将 
<your workspace id>替换为你的 Fabric 工作区 ID。 可以在 URL 中轻松找到工作区的 ID,它是浏览器窗口中/后两个/groups/字符内的唯一字符串。 - 将 
<your database id>替换为在 Fabric 数据库 ID 中的 SQL 数据库。 你可以在 URL 中轻松找到数据库项的标识符,它是在浏览器窗口中/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 工作区 ID。 可以在 URL 中轻松找到工作区的 ID,它是浏览器窗口中 / 后两个 /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)
}