適用於:Azure SQL 資料庫
分割合併工具可讓您在分區化資料庫之間移動資料。 請參閱在橫向擴展的雲端資料庫之間移動資料。
注意
分割合併工具適用於 Azure Web Apps。 雲端服務 (傳統) 的生命周期結束時間是 2024 年 8 月 31 日。 如果您在雲端服務 (傳統) 使用分割合併工具,請在 2024 年 8 月 31 日之前移轉至 Azure Web Apps。
必要條件
建立 SQL 資料庫,作為分割合併狀態的資料庫使用。 前往 Azure 入口網站。 建立新的 SQL Database。 為資料庫命名,並建立新的系統管理員和密碼。 請務必記錄名稱和密碼以供日後使用。
請確定您在 Azure 的邏輯伺服器能讓 Azure 服務與之連線。 在 Azure 入口網站中,進入你的邏輯伺服器的[防火牆設定],確保[允許存取 Azure 服務] 設定為[開啟]。 選取 [儲存] 圖示。
建立診斷輸出用的 Azure 儲存體帳戶。
使用公共的分割合併 Docker 映像,或者將分割合併 Docker 映像推送至您所選擇的 Azure 容器服務或 Docker registry。
為服務建立兩個 Azure Web Apps
建立兩個 Web 應用程式 - 一個 worker Web 應用程式和一個 UI Web 應用程式。
Worker 網頁應用程式
在[發佈] 欄位中,選取[容器]。
針對 [作業系統],選取 [Windows]。
繼續前往 Docker 索引標籤。
填入下列資訊:
映像來源:Docker hub
存取類型:Public
映像及標籤:mcr.microsoft.com/splitmerge/splitmergeworker:20240812.1使用[檢閱 + 建立],建立 Web App。
用戶介面網頁應用程式
若要建立 UI Web App,請遵循用來建立 Worker Web App 的相同步驟,但有一個不同點:
- 不同的 Docker 映像在[映像和標籤]欄位中:
mcr.microsoft.com/splitmerge/splitmergeweb:20240812.1
設定分割合併的網頁應用程式
設定安全性
如需設定服務安全性的詳細指示,請參閱分割合併安全性設定。
為了進行本教學課程的簡單測試部署,將會執行一組最少的組態步驟,讓服務啟動並執行。 這些步驟只會讓執行它們的機器/帳戶與服務通訊。
建立自我簽署憑證與 PFX 檔案
使用 PowerShell 建立自我簽署憑證與 PFX 檔案。
首先,建立新目錄。 然後據此取代內嵌值,然後從新目錄執行下列 PowerShell 命令。 以有效的密碼取代 <password>。
$cert = New-SelfSignedCertificate -Subject "CN=*.cloudapp.net" -CertStoreLocation "Cert:\CurrentUser\My" -KeyExportPolicy Exportable -KeySpec Signature -KeyLength 2048 -KeyAlgorithm RSA -HashAlgorithm SHA256
$mypwd = ConvertTo-SecureString -String "<password>" -Force -AsPlainText ## Replace <password>
Export-PfxCertificate -Cert $cert -FilePath "C:\Users\admin\Desktop\$certname.pfx" -Password $mypwd ## Specify your preferred location
將 PFX 檔案上傳至 Web Apps 並啟用憑證
針對 worker 和 UI Web Apps 重複下列步驟。
- 前往 Azure 入口網站。
- 選取 [應用程式服務]。
- 選取您在上方為分割合併工具建立的網路應用程式。
- 從選單中選取憑證。
- 選擇使用自備的憑證 (.pfx)。
- 在工具列中選取新增憑證。
- 選取 PFX 檔案,然後輸入與上述相同的密碼。
- 完成後,請從清單中的新項目複製憑證指紋。
- 在 [Web App] 功能表開啟[設定] / [組態]。
- 將[用戶端憑證模式] 設定為
Require。
網頁應用程式設定
針對 worker 和 UI Web Apps 重複下列步驟。
開啟已部署的 Web App,然後移至[設定]>[環境變數]>[應用程式設定]。 選取 <新增>。
新增名稱為 ElasticScaleMetadata 的變數,以及新增具有先前部署狀態資料庫之連接字串的值。
重要
此時,狀態資料庫必須使用拉丁文定序 (
SQL\_Latin1\_General\_CP1\_CI\_AS)。 如需詳細資訊,請參閱 Windows 定序名稱。若為 Azure SQL 資料庫,連接字串的格式通常如下:
Server=<serverName>.database.windows.net; Database=<databaseName>;User ID=<userId>; Password=<password>; Encrypt=True; Connection Timeout=30新增其他變數:
名稱 價值 WorkerRoleSynchronizationStorageAccountConnectionString 先前建立之 Azure 儲存體的有效連接字串。 資料加密主憑證指紋 先前產生的憑證指紋。 MetadataExpirationPeriodInMinutes (中繼資料過期時間以分鐘計) 20160 最大重試次數 (MaxRetryCount) 5 網站載入憑證 * 網站_VNET內的鏡像提取 0 選取[套用] 並重新啟動應用程式。
針對
worker和UIWeb Apps 重複相同的步驟。
部署疑難排解
如果您的 Web 角色無法上線,則安全性設定可能會有問題。 檢查 TLS/SSL 是否已照先前描述的方式設定。
如果您的工作角色無法上線,但 Web 角色成功上線,很可能是因為連接到您之前建立的狀態資料庫時發生了問題。
請確定連接字串正確無誤。
檢查伺服器和資料庫是否存在,以及使用者識別碼和密碼是否正確。
若為 Azure SQL 資料庫,連接字串的格式應該是:
Server=<serverName>.database.windows.net; Database=<databaseName>;User ID=<user>; Password=<password>; Encrypt=True; Connection Timeout=30請確定伺服器名稱開頭不是
https://。請確定您的伺服器能讓 Azure 服務與之連線。 若要這麼做,請在入口網站開啟資料庫,並確定[允許存取 Azure 服務] 設定設為[開啟]。
測試服務部署
使用網頁瀏覽器連線
移至 Web App 的UI[概觀],然後選取[瀏覽]。 如果出現提示,請選擇正確的憑證。
使用 PowerShell 指令碼測試
藉由執行包含的範例 PowerShell 指令碼,即可測試部署和您的環境。
重要
範例指令碼會在 PowerShell 5.1 上執行。 它們目前不會在 PowerShell 6或更新版本上執行。
包含的指令碼檔案如下:
SetupSampleSplitMergeEnvironment.ps1- 設定用於分割合併的測試資料層級。- 建立分片映射管理資料庫。
- 建立兩個分片資料庫。
- 為這些資料庫建立分區對應 (刪除這些資料庫上的任何現有分區對應)。
- 在兩個資料分片中建立小型範例資料表,並在其中一個分片中填入資料。
- 宣告分片資料表的 SchemaInfo。
ExecuteSampleSplitMerge.ps1- 在測試資料層上執行測試作業。- 將分割要求傳送至分割合併服務 Web 前端,將一半資料從第一個分區分割至第二個分區。
- 查詢網頁前端以獲取分割要求的狀態,並等待要求完成。
- 將合併要求傳送至分割合併服務 Web 前端,將資料從第二個分區移回第一個分區。
- 輪詢 Web 前端的合併要求狀態,並等候要求完成。
GetMappings.ps1- 最上層範例指令碼,會列印出分區對應的目前狀態。ShardManagement.psm1- 包裝 ShardManagement API 的協助程式指令碼。SqlDatabaseHelpers.psm1- 協助程式指令碼,用於在 SQL Database 中建立和管理資料庫。
使用 PowerShell 來驗證部署
開啟新的 PowerShell 視窗,並瀏覽至您下載分割合併套件的目錄,然後瀏覽至 "PowerShell" 目錄。
建立一台伺服器 (或選擇一台現有伺服器),其中將建立分片圖管理器和分片。
注意
SetupSampleSplitMergeEnvironment.ps1指令碼預設會在相同伺服器上建立所有這些資料庫,以保持指令碼簡單。 這不是分割合併服務本身的限制。分割合併服務需要有一個具備讀取/寫入資料庫權限的 SQL 驗證登入,以便移動資料並更新分片對應。 由於分割合併服務會在雲端中執行,因此目前不支援整合式驗證。
請確定伺服器已設定為允許從執行這些指令碼之電腦的 IP 位址存取。 您可以在 SQL Server / 防火牆和虛擬網路 / 用戶端 IP 位址下找到此設定。
執行
SetupSampleSplitMergeEnvironment.ps1指令碼以建立範例環境。執行此指令碼將會刪除分區對應管理員資料庫及分區上的任何現有分區對應管理資料結構。 如果您想要重新初始化分區對應或分區,重新執行指令碼可能很有用。
範例命令行。 以有效的密碼取代
<password>。.\SetupSampleSplitMergeEnvironment.ps1 ^ -UserName 'mysqluser' -Password '<password>' -ShardMapManagerServerName 'abcdefghij.database.windows.net'執行 Getmappings.ps1 指令碼,以檢視目前存在於範例環境中的對應。 以有效的密碼取代
<password>。.\GetMappings.ps1 ^ -UserName 'mysqluser' -Password '<password>' -ShardMapManagerServerName 'abcdefghij.database.windows.net'執行
ExecuteSampleSplitMerge.ps1指令碼來執行分割作業 (將第一個分區上的一半資料移至第二個分區),然後執行合併作業 (將資料移回第一個分區)。 如果您已設定 TLS 並讓 HTTP 端點保持停用,請確定您改用 https:// 端點。範例命令行。 以有效的密碼取代
<password>。.\ExecuteSampleSplitMerge.ps1 ^ -UserName 'mysqluser' -Password '<password>' ^ -ShardMapManagerServerName 'abcdefghij.database.windows.net' ^ -SplitMergeServiceEndpoint 'https://mysplitmergeservice.cloudapp.net' ^ -CertificateThumbprint '0123456789abcdef0123456789abcdef01234567'如果您收到下列錯誤,很可能是 Web 端點憑證發生問題。 嘗試使用您慣用的網頁瀏覽器連線到 Web 端點,並檢查是否有憑證錯誤。
Invoke-WebRequest : The underlying connection was closed: Could not establish trust relationship for the SSL/TLSsecure channel.如果成功,輸出看起來應該會像下列輸出。 以有效的密碼取代
<password>。.\ExecuteSampleSplitMerge.ps1 -UserName 'mysqluser' -Password '<password>' -ShardMapManagerServerName 'abcdefghij.database.windows.net' -SplitMergeServiceEndpoint 'http://mysplitmergeservice.cloudapp.net' -CertificateThumbprint 0123456789abcdef0123456789abcdef01234567 Sending split request Began split operation with id dc68dfa0-e22b-4823-886a-9bdc903c80f3 Polling split-merge request status. Press Ctrl-C to end Progress: 0% | Status: Queued | Details: [Informational] Queued request Progress: 5% | Status: Starting | Details: [Informational] Starting split-merge state machine for request. Progress: 5% | Status: Starting | Details: [Informational] Performing data consistency checks on target shards. Progress: 20% | Status: CopyingReferenceTables | Details: [Informational] Moving reference tables from source to target shard. Progress: 20% | Status: CopyingReferenceTables | Details: [Informational] Waiting for reference tables copy completion. Progress: 20% | Status: CopyingReferenceTables | Details: [Informational] Moving reference tables from source to target shard. Progress: 44% | Status: CopyingShardedTables | Details: [Informational] Moving key range [100:110) of Sharded tables Progress: 44% | Status: CopyingShardedTables | Details: [Informational] Successfully copied key range [100:110) for table [dbo].[MyShardedTable] ... ... Progress: 90% | Status: Completing | Details: [Informational] Successfully deleted shardlets in table [dbo].[MyShardedTable]. Progress: 90% | Status: Completing | Details: [Informational] Deleting any temp tables that were created while processing the request. Progress: 100% | Status: Succeeded | Details: [Informational] Successfully processed request. Sending merge request Began merge operation with id 6ffc308f-d006-466b-b24e-857242ec5f66 Polling request status. Press Ctrl-C to end Progress: 0% | Status: Queued | Details: [Informational] Queued request Progress: 5% | Status: Starting | Details: [Informational] Starting split-merge state machine for request. Progress: 5% | Status: Starting | Details: [Informational] Performing data consistency checks on target shards. Progress: 20% | Status: CopyingReferenceTables | Details: [Informational] Moving reference tables from source to target shard. Progress: 44% | Status: CopyingShardedTables | Details: [Informational] Moving key range [100:110) of Sharded tables Progress: 44% | Status: CopyingShardedTables | Details: [Informational] Successfully copied key range [100:110) for table [dbo].[MyShardedTable] ... ... Progress: 90% | Status: Completing | Details: [Informational] Successfully deleted shardlets in table [dbo].[MyShardedTable]. Progress: 90% | Status: Completing | Details: [Informational] Deleting any temp tables that were created while processing the request. Progress: 100% | Status: Succeeded | Details: [Informational] Successfully processed request.用其他資料類型實驗。 所有這些指令碼都會採用選擇性的 -ShardKeyType 參數,可讓您指定金鑰類型。 預設為 Int32,但您也可以指定 Int64、Guid 或二進位。
建立要求
此服務可以透過 Web UI 使用,也可以透過匯入和使用 SplitMerge.psm1 PowerShell 模組來使用服務,該模組將透過 Web 角色提交您的要求。
服務可以移動分區資料表和參考資料表中的資料。 分片資料表具有分片鍵欄位,並且每個分片上的資料列數據不同。 參考資料表並未分區化,因此它會在每個分區上包含相同的資料列資料。 參考資料表對不會經常變更的資料很有用,而且會用於在查詢中與分區資料表聯結。
若要執行分割合併作業,您必須宣告要移動的分區資料表和參考資料表。 這是使用 SchemaInfo API 完成的。 此 API 位於 Microsoft.Azure.SqlDatabase.ElasticScale.ShardManagement.Schema 命名空間中。
- 針對每個分區資料表,建立 ShardedTableInfo 物件來描述資料表的父系結構描述名稱 (選用,預設值為 「dbo」)、資料表名稱,以及包含分區化索引鍵之資料表中的資料行名稱。
- 針對每個參考資料表,建立 ReferenceTableInfo 物件來描述資料表的父系結構描述 (選用,預設值為 「dbo」) 和資料表名稱。
- 將先前的 TableInfo 物件加入新的 SchemaInfo 物件。
- 取得 ShardMapManager 物件的參考,並呼叫 GetSchemaInfoCollection。
- 將 SchemaInfo 新增至 SchemaInfoCollection,並提供分區對應名稱。
您可以在 SetupSampleSplitMergeEnvironment.ps1 指令碼中看到此範例。
分割合併服務不會為您建立目標資料庫 (或資料庫中任何資料表的結構描述)。 必須先預先建立它們,才能將要求傳送至服務。
已知錯誤
執行範例 PowerShell 指令碼時,您可能會看到下列訊息:
Invoke-WebRequest : The underlying connection was closed: Could not establish trust relationship for the SSL/TLS secure channel.
此錯誤表示您的 TLS/SSL 憑證未正確設定。 請遵照使用網頁瀏覽器連線一節中的指示。
如果您無法提交要求,您可能會看到下列內容:
[Exception] System.Data.SqlClient.SqlException (0x80131904): Could not find stored procedure 'dbo.InsertRequest'.
在此情況下,請檢查組態檔,特別是 WorkerRoleSynchronizationStorageAccountConnectionString 的設定。 此錯誤通常表示工人角色未能在首次使用時成功初始化元資料資料庫。
相關內容
尚未使用彈性資料庫工具? 請參閱使用者入門指南。 如有疑問,請在 SQL Database 的 Microsoft Q&A 問題頁面上與我們連絡。如有功能要求,請在 SQL Database 意見反應論壇中新增想法或投票支持現有的想法。