對 Azure SQL 資料庫和 Azure SQL 受控執行個體的連線問題和其他問題進行疑難排解

適用於:Azure SQL 資料庫Azure SQL 受控執行個體

當 Azure SQL Database 或 Azure SQL 受控執行個體的連線失敗時,您會收到錯誤訊息。

和往常一樣,在應用程式設計程序期間套用最佳做法和設計指導方針。

注意

您可以使用 Azure SQL 連線檢查程式來偵測並修正 Azure SQL 資料庫、Azure SQL 受控執行個體及 Azure Synapse Analytics 環境中各種連線錯誤。

修復常見連線問題的步驟

  1. 確定已啟用 TCP/IP 作為應用程式伺服器上的用戶端通訊協定。 在未安裝 SQL 工具的應用程式伺服器上,執行 cliconfg.exe (SQL Server 用戶端網路公用程式),以確認已啟用 TCP/IP

  2. 檢查應用程式的連接字串,以確定其已正確設定。 例如,確定連接字串會指定正確的連接埠 (1433) 和完整的伺服器名稱。 請參閱使用 SQL Server Management Studio取得連線資訊

  3. 嘗試增加連線逾時值。 建議至少使用 30 秒的連線逾時。

  4. 使用 SQL Server Management Studio (SSMS)、UDL 檔案、Ping 或 Telnet,來測試應用程式伺服器與 Azure SQL Database 之間的連線能力。 如需詳細資訊,請參閱針對連線問題進行疑難排解 (英文) 和診斷連線問題

    注意

    作為疑難排解步驟,您也可以在不同的用戶端電腦上測試連線能力。

  5. 最佳做法是,雲端連線的應用程式必須使用重試邏輯

如果這些步驟無法解決您的問題,請嘗試收集更多資料,然後連絡支援人員。 如果您的應用程式是雲端服務,請啟用記錄。 此步驟會傳回失敗的 UTC 時間戳記。 如需如何啟用記錄的詳細資訊,請參閱在 Azure App Service 中針對應用程式啟用診斷記錄。 此外,SQL Database 會傳回追蹤識別碼。 Microsoft 客戶支援服務可以使用此資訊。

實作重試邏輯

強烈建議您的用戶端應用程式使用重試邏輯,以便在給予暫時性錯誤一些時間自行修正後,可以嘗試重新建立連線。 我們建議您在您第一次重試前延遲 5 秒鐘。 在少於 5 秒的延遲之後重試,就會產生使雲端服務超過負荷的風險。 對於後續每次重試,延遲應以指數方式成長,最大值為 60 秒。

如需重試邏輯的程式碼範例,請參閱:

如需在應用程式中處理暫時性錯誤的詳細資訊,請檢閱對 SQL Database 的暫時性連線錯誤進行疑難排解

連線集區 (ADO.NET) 中討論了有關使用 ADO.NET 之用戶端的「封鎖期間」。

暫時性錯誤訊息 (40197、40613 及其他)

Azure 基礎結構能夠在 SQL Database 服務出現繁重的工作負載時動態重新設定伺服器。 此動態行為可能導致您的用戶端程式遺失其與資料庫或執行個體的連線。 此類錯誤情況稱為「暫時性錯誤」 。 資料庫重新設定事件由於規劃的事件 (例如,軟體升級) 或未規劃的事件 (例如,處理序損毀或負載平衡) 而發生。 大部分的重新組態事件都是短期的,至多應在不到 60 秒的時間完成。 不過,這些事件可能偶爾會需要更長時間才能完成,例如當大型交易導致長時間執行的復原時。 下表列出應用程式在連線到 Azure SQL Database 時可能收到的各種暫時性錯誤。

暫時性錯誤碼清單

錯誤碼 嚴重性 描述
926 14 Database 'replicatedmaster' cannot be opened. It has been marked SUSPECT by recovery. See the SQL Server error log for more information.

在重新設定的最後一個階段,當舊的主要資料庫正在關閉其記錄時,此錯誤可能會短期記錄於 SQL 受控執行個體錯誤記錄檔中。
如需涉及此錯誤訊息的其他非暫時性案例,請參閱 MSSQL 錯誤文件
4060 16 Cannot open database "%.*ls" requested by the login. The login failed.

如需詳細資訊,請參閱錯誤 4000 到 4999
40197 17 The service has encountered an error processing your request. Please try again. Error code %d.

當服務因為軟體或硬體升級、硬體故障或任何其他容錯移轉問題而關閉時,您會收到這個錯誤。 內嵌在錯誤 40197 訊息中的錯誤代碼 (%d) 提供發生的失敗或容錯移轉種類的其他資訊。 錯誤代碼會內嵌在錯誤 40197 的訊息錯誤範例包括 40020、40143、40166 和 40540。

重新連線會自動將您連線到資料庫的狀況良好複本。 您的應用程式必須攔截錯誤 40197、記錄訊息中內嵌的錯誤碼 (%d) 以進行疑難排解,並嘗試重新連接到 SQL Database 直到資源可供使用,並再次建立您的連線。 如需相關資訊,請參閱暫時性錯誤
40501 20 The service is currently busy. Retry the request after 10 seconds. Incident ID: %ls. Code: %d.

如需詳細資訊,請參閱:
邏輯 SQL Server 資源限制
針對單一資料庫且以 DTU 為基礎的限制
針對彈性集區且以 DTU 為基礎的限制
針對單一資料庫且以虛擬核心為基礎的限制
針對彈性集區且以虛擬核心為基礎的限制
Azure SQL 受控執行個體資源限制
40613 17 Database '%.*ls' on server '%.*ls' is not currently available. Please retry the connection later. If the problem persists, contact customer support, and provide them with the session tracing ID of '%.*ls'.

如果目前已在資料庫中建立專用管理員連接 (DAC),可能就會發生此錯誤。 如需相關資訊,請參閱暫時性錯誤
49918 16 Cannot process request. Not enough resources to process request. The service is currently busy. Please retry the request later.

如需詳細資訊,請參閱:
邏輯 SQL Server 資源限制
針對單一資料庫且以 DTU 為基礎的限制
針對彈性集區且以 DTU 為基礎的限制
針對單一資料庫且以虛擬核心為基礎的限制
針對彈性集區且以虛擬核心為基礎的限制
Azure SQL 受控執行個體資源限制
49919 16 Cannot process create or update request. Too many create or update operations in progress for subscription "%ld".

服務正忙於處理多個建立或更新您訂用帳戶或伺服器的要求。 要求目前已封鎖以求資源最佳化。 查詢 sys.dm_operation_status 以查看暫止的作業。 等待直到暫止的建立或更新要求完成,或刪除您的其中一個暫止要求,稍後再重試您的要求。 如果您的作業似乎發生停滯,請等候其他進行中的作業完成,或盡可能取消這些作業。 例如,您可以藉由刪除所建立的資料庫或複本,來取消資料庫複本或異地複本建立。 如果無法取消明顯停滯的作業,請向 Microsoft 開啟支援票證
49920 16 Cannot process request. Too many operations in progress for subscription "%ld".

服務正忙於處理這個訂用帳戶的多個要求。 要求目前已封鎖以求資源最佳化。 查詢 sys.dm_operation_status 以查看作業狀態。 等候直到暫止的要求已完成,或刪除您其中一個暫止要求,稍後再重試您的要求。 如果您的作業似乎發生停滯,請等候其他進行中的作業完成,或盡可能取消這些作業。 例如,您可以藉由刪除所建立的資料庫或複本,來取消資料庫複本或異地複本建立。 如果無法取消明顯停滯的作業,請向 Microsoft 開啟支援票證
4221 16 Login to read-secondary failed due to long wait on 'HADR_DATABASE_WAIT_FOR_TRANSITION_TO_VERSIONING'.

無法使用複本進行登入,因為在複本回收時執行中之交易的資料列版本已遺失。 將主要複本上的使用中交易復原或認可,就可以解決問題。 避免在主要伺服器上的冗長寫入交易,就可讓此條件發生率降至最低。
615 21 Could not find database ID %d, name '%.*ls'

這表示記憶體內部快取不會與 SQL Server 執行個體同步,而且查閱正在擷取過時的資料庫識別碼。

SQL 登入會使用記憶體內部快取,來取得資料庫名稱與識別碼的對應。 快取應該會與後端資料庫同步,並且在 SQL Server 執行個體的資料庫連結和中斷連結時進行更新。
當中斷連結工作流程未能準時清除記憶體內部快取,而且後續對資料庫的查閱都指向過時的資料庫識別碼時,就會收到此錯誤。

嘗試重新連線到 SQL Database,直到資源可供使用,然後重新建立連線。 如需相關資訊,請參閱暫時性錯誤

解決暫時性連線問題的步驟

  1. 檢查 Microsoft Azure 服務儀表板,以了解是否有在應用程式回報期間發生的任何已知中斷。
  2. 連線到雲端服務 (例如 Azure SQL 資料庫) 的應用程式應該預期會有定期的重新設定事件,並實作重試邏輯來處理這些錯誤,而不是向使用者顯示應用程式錯誤。
  3. 由於資料庫接近其資源限制,因此似乎是暫時性連線問題。 請參閱資源限制
  4. 如果連線問題繼續發生,或如果您的應用程式發生錯誤的持續時間超過 60 秒,或如果您在一天當中,看到錯誤多次發生,請在 Azure 支援網站上選取 [取得支援],來提出 Azure 支援要求。

如果應用程式無法連線到伺服器,就會發生此問題。

若要解決此問題,請嘗試修復常見連線問題的步驟一節中的步驟 (依顯示的順序)。

找不到或無法存取伺服器/執行個體 (錯誤 26、40、10053)

錯誤 26:尋找指定的伺服器時發生錯誤

System.Data.SqlClient.SqlException: A network-related or instance-specific error occurred while establishing a connection to SQL Server. The server was not found or was not accessible. Verify that the instance name is correct and that SQL Server is configured to allow remote connections.(provider: SQL Network Interfaces, error: 26 – Error Locating Server/Instance Specified)

錯誤 40:無法開啟與伺服器的連線

A network-related or instance-specific error occurred while establishing a connection to SQL Server. The server was not found or was not accessible. Verify that the instance name is correct and that SQL Server is configured to allow remote connections. (provider: Named Pipes Provider, error: 40 - Could not open a connection to SQL Server)

錯誤 10053:在接收來自伺服器的要求時發生傳輸層級的錯誤

10053: A transport-level error has occurred when receiving results from the server. (Provider: TCP Provider, error: 0 - An established connection was aborted by the software in your host machine)

如果應用程式無法連線到伺服器,就會發生這些問題。

若要解決這些問題,請嘗試修復常見連線問題的步驟一節中的步驟 (依顯示的順序)。

因為防火牆問題而無法連線到伺服器

錯誤 40615:無法連線到 < servername >

若要解決此問題,請透過 Azure 入口網站在 SQL Database 上設定防火牆設定

錯誤 5:無法連線到 < servername >

若要解決此問題,請確定已在用戶端與網際網路之間的所有防火牆上開放連接埠 1433,以供輸出連線使用。

無法登入伺服器 (錯誤 18456、40531)

使用者 '< 使用者名稱 >' 登入失敗

Login failed for user '<User name>'.This session has been assigned a tracing ID of '<Tracing ID>'. Provide this tracing ID to customer support when you need assistance. (Microsoft SQL Server, Error: 18456)

若要解決此問題,請洽詢您的服務管理員,以提供有效的使用者名稱和密碼。

一般而言,服務管理員可以使用下列步驟來新增登入認證:

  1. 使用 SQL Server Management Studio (SSMS) 登入伺服器。

  2. 若要檢查登入名稱是否已停用,請在 master 資料庫中執行下列 SQL 查詢:

    SELECT name, is_disabled FROM sys.sql_logins;
    
  3. 如果對應的名稱已停用,您可能會決定使用下列陳述式啟用它:

    ALTER LOGIN <User name> ENABLE;
    
  4. 如果 SQL 登入使用者名稱不存在,請編輯並執行下列 SQL 查詢,以建立新的 SQL 登入:

    CREATE LOGIN <SQL_login_name, sysname, login_name>
    WITH PASSWORD = '<password, sysname, Change_Password>';
    GO
    
  5. 在 [SSMS 物件總管] 中,展開 [資料庫]。

  6. 選取您要授與使用者權限的資料庫。

  7. 以滑鼠右鍵按一下 [安全性],然後依序選取 [新增] 和 [使用者]。

  8. 在產生且含有預留位置的指令碼中,遵循步驟取代 SSMS 範本參數並執行,例如:

    CREATE USER [<user_name, sysname, user_name>]
    FOR LOGIN [<login_name, sysname, login_name>]
    WITH DEFAULT_SCHEMA = [<default_schema, sysname, dbo>];
    GO
    
    -- Add user to the database owner role
    EXEC sp_addrolemember N'db_owner', N'<user_name, sysname, user_name>';
    GO
    

    您也可以使用 sp_addrolemember,將特定使用者對應至特定的資料庫角色。

    注意

    在 Azure SQL Database 中,考慮使用較新的 ALTER ROLE 語法,來管理資料庫角色成員資格。

如需詳細資訊,請參閱管理 Azure SQL Database 中的資料庫和登入

已超過連線逾時的錯誤

System.Data.SqlClient.SqlException (0x80131904):已超過連線逾時

System.Data.SqlClient.SqlException (0x80131904): Connection Timeout Expired. The timeout period elapsed while attempting to consume the pre-login handshake acknowledgement. This could be because the pre-login handshake failed or the server was unable to respond back in time. The duration spent while attempting to connect to this server was - [Pre-Login] initialization=3; handshake=29995;

System.Data.SqlClient.SqlException (0x80131904):已超過逾時

System.Data.SqlClient.SqlException (0x80131904): Timeout expired. The timeout period elapsed prior to completion of the operation or the server is not responding.

System.Data.Entity.Core.EntityException:基礎提供者在開啟上失敗

System.Data.Entity.Core.EntityException: The underlying provider failed on Open. -> System.Data.SqlClient.SqlException: Timeout expired. The timeout period elapsed prior to completion of the operation or the server is not responding. -> System.ComponentModel.Win32Exception: The wait operation timed out

無法連線到 < 伺服器名稱 >

Cannot connect to <server name>.ADDITIONAL INFORMATION:Connection Timeout Expired. The timeout period elapsed during the post-login phase. The connection could have timed out while waiting for server to complete the login process and respond; Or it could have timed out while attempting to create multiple active connections. The duration spent while attempting to connect to this server was - [Pre-Login] initialization=231; handshake=983; [Login] initialization=0; authentication=0; [Post-Login] complete=13000; (Microsoft SQL Server, Error: -2) For help, click: http://go.microsoft.com/fwlink?ProdName=Microsoft%20SQL%20Server&EvtSrc=MSSQLServer&EvtID=-2&LinkId=20476 The wait operation timed out

這些例外狀況可能是因連線或查詢問題而造成。 若要確認連線問題造成此錯誤,請參閱確認錯誤是否因連線問題而造成

因為應用程式無法連線到伺服器而發生連線逾時。 若要解決此問題,請嘗試修復常見連線問題的步驟一節中的步驟 (依顯示的順序)。

網路連線終止錯誤

SQL 用戶端程式庫會使用 TCP 網路通訊協定連線到 Azure SQL Database 和 Azure SQL 受控執行個體。 用戶端程式庫使用稱為 TCP 提供者的較低層級元件來管理 TCP 連線。 當 TCP 提供者偵測到遠端主機意外終止現有的 TCP 連線時,用戶端程式庫會出現錯誤。 因為錯誤是用戶端錯誤,而不是 SQL Server 錯誤,所以不會包含任何 SQL 錯誤號碼。 反之,錯誤號碼為 0,則使用了來自 TCP 提供者的錯誤訊息。

網路連線終止錯誤的範例包括:

A connection was successfully established with the server, but then an error occurred during the pre-login handshake. (provider: TCP Provider, error: 0 - An existing connection was forcibly closed by the remote host.) An existing connection was forcibly closed by the remote host

A transport-level error has occurred when sending the request to the server. (provider: TCP Provider, error: 0 - An existing connection was forcibly closed by the remote host.)

The client was unable to establish a connection because of an error during connection initialization process before login. Possible causes include the following: the client tried to connect to an unsupported version of SQL Server; the server was too busy to accept new connections; or there was a resource limitation (insufficient memory or maximum allowed connections) on the server. (provider: TCP Provider, error: 0 - An existing connection was forcibly closed by the remote host.)

A connection was successfully established with the server, but then an error occurred during the login process. (provider: TCP Provider, error: 0 - An existing connection was forcibly closed by the remote host.)

連線終止錯誤可能會因為資料庫或彈性集區暫時無法使用而發生。 也可能因為資料庫伺服器與用戶端應用程式之間的網路基礎結構發生各種問題,包括防火牆、網路設備等。這些問題可能是暫時性或永久性問題。 一般的方針是,考慮永久失敗前,應用程式應該先針對這些錯誤使用固定數目的重試嘗試。

資源管理錯誤

Azure SQL Database 會根據 Resource Governor 來使用資源治理實作,以強制執行資源限制。 深入了解 Azure SQL Database 中的資源管理

首先會列出最常見的資源治理錯誤及其詳細資料,後面接著資源治理錯誤訊息表。

錯誤 10928 和 10936:資源識別碼:1。 [資料庫/彈性集區] 的要求限制為 %d 並且已達到限制。如需協助

如果達到資料庫層級限制,則此案例中的詳細錯誤訊息會讀取:Resource ID : 1. The request limit for the database is %d and has been reached. See 'http://go.microsoft.com/fwlink/?LinkId=267637' for assistance.

如果達到彈性集區限制,此案例中的詳細錯誤訊息會讀取:Resource ID : 1. The request limit for the elastic pool is %d and has been reached. See 'http://go.microsoft.com/fwlink/?LinkId=267637' for assistance. 彈性集區限制高於資料庫限制,如需詳細資訊,請參閱資源限制。 當集區中的多個資料庫同時使用資源 (例如背景工作角色) 時,可能會遇到這些限制。

此錯誤訊息表示已達到資料庫或彈性集區的背景工作限制。 將出現資料庫或彈性集區服務目標的最大同時背景工作角色值將,而不是預留位置 %d

注意

Azure SQL Database 的初始供應項目僅支援單一執行緒查詢。 在那時,要求數目一律等於背景工作角色數目。 Azure SQL 資料庫中的錯誤訊息 10928 和 10936 包含「資料庫的要求限制 [...] 為 N,且已達到其限制」的描述,以支援回溯相容性。 達到的限制實際上是背景工作角色數目。 如果平行處理原則的最大程度 (MAXDOP) 設定等於零或大於一,則背景工作數目會遠高於要求數目,而且比 MAXDOP 等於一時更快達到限制。

深入了解工作階段、背景工作和要求

視需要使用專用管理員連接 (DAC) 連線

如果在達到背景工作限制的情況下正在進行即時事件,則當您使用 SQL Server Management Studio (SSMS) 或 Azure Data Studio 來連線時,可能會收到錯誤 10928。 即使已達到最大的背景工作閾值,一個工作階段還是可以使用資料庫管理員的診斷連線 (DAC) 來連線。

從 SSMS 建立與 DAC 的連線:

  • 從功能表中,選取 [檔案] > [新增] > [資料庫引擎查詢]
  • 從 [伺服器名稱] 欄位中的 [連線] 對話方塊中,輸入 admin:<fully_qualified_server_name> (例如,admin:servername.database.windows.net)。
  • 選取 [選項 >>]
  • 選取 [連線屬性] 索引標籤
  • 在 [連接到資料庫:] 方塊中,輸入資料庫的名稱
  • 選取 Connect

如果您收到錯誤 40613:Database '%.&#x2a;ls' on server '%.&#x2a;ls' is not currently available. Please retry the connection later. If the problem persists, contact customer support, and provide them the session tracing ID of '%.&#x2a;ls',這可能表示另一個工作階段已連線到 DAC。 一次只能有一個工作階段針對單一資料庫或彈性集區工作階段連線到 DAC。

如果您在選取 [連線] 之後遇到「無法連線到伺服器」錯誤,如果您使用 18.9 之前的 SSMS 版本,則可能仍已成功建立 DAC 工作階段。 早期的 SSMS 版本會嘗試提供 Intellisense 來連線到 DAC。 這會失敗,因為 DAC 只支援單一背景工作,而 Intellisense 需要個別的背景工作。

您無法將 DAC 連線與 SSMS 中的物件總管搭配使用。

檢閱您的 max_worker_percent 使用量

若要針對資料庫尋找 14 天的資源耗用量統計資料,請查詢 sys.resource_stats 系統目錄檢視。 max_worker_percent 資料行顯示相對於資料庫的背景工作限制所使用的背景工作百分比。 連線到您的邏輯伺服器上的 master 資料庫,以查詢 sys.resource_stats

SELECT start_time, end_time, database_name, sku, avg_cpu_percent, max_worker_percent, max_session_percent 
FROM sys.resource_stats;

您也可以從 sys.dm_db_resource_stats 動態管理檢視,查詢過去一小時的資源耗用量統計資料。 直接連線到資料庫,以查詢 sys.dm_db_resource_stats

SELECT end_time, avg_cpu_percent, max_worker_percent, max_session_percent
FROM sys.dm_db_resource_stats;

盡可能降低背景工作使用量

封鎖鏈結可能導致資料庫中的背景工作數目突然激增。 大量並行平行查詢可能導致大量背景工作。 增加平行處理原則的最大程度 (MAXDOP) 或將 MAXDOP 設為零,可以增加使用中背景工作的數目。

依照下列步驟,將沒有足夠背景工作的事件分級:

  1. 調查是否發生封鎖,或者,您是否可以識別大量並行背景工作。 執行下列查詢以檢查目前要求,並在資料庫傳回錯誤 10928 時檢查封鎖。 您可能需要使用專用管理員連接 (DAC) 連線,以執行查詢。

    SELECT
        r.session_id, r.request_id, r.blocking_session_id, r.start_time, 
        r.status, r.command, DB_NAME(r.database_id) AS database_name,
        (SELECT COUNT(*) 
            FROM sys.dm_os_tasks AS t 
            WHERE t.session_id=r.session_id and t.request_id=r.request_id) AS worker_count,
        i.parameters, i.event_info AS input_buffer,
        r.last_wait_type, r.open_transaction_count, r.total_elapsed_time, r.cpu_time,
        r.logical_reads, r.writes, s.login_time, s.login_name, s.program_name, s.host_name
    FROM sys.dm_exec_requests as r
    JOIN sys.dm_exec_sessions as s on r.session_id=s.session_id
    OUTER APPLY sys.dm_exec_input_buffer (r.session_id,r.request_id) AS i
    WHERE s.is_user_process=1;
    GO
    
    1. 尋找包含 blocking_session_id 的資料列,以識別已封鎖的工作階段。 在清單中尋找每個 blocking_session_id,以判斷是否也要封鎖該工作階段。 遵循 blocking_session_idsession_id 值最終會引導您前往前端封鎖程式:未封鎖但正在封鎖的工作階段。 調整源頭封鎖者查詢。

      提示

      如需針對長時間執行或封鎖查詢進行疑難排解的詳細資訊,請參閱了解和解決 Azure SQL Database 封鎖問題

    2. 若要識別大量並行背景工作,請檢閱整體要求數目和每個要求的 worker_count 資料行。 Worker_count 是取樣時的背景工作數目,而且可能會在要求執行時隨時間變更。 如果提高背景工作數的原因是以平行處理原則的最佳程度執行的並行查詢,則調整查詢以降低資源使用率。 如需詳細資訊,請參閱查詢微調/提示

  2. 評估資料庫的平行處理原則的最大程度 (MAXDOP) 設定。

提高背景工作限制

如果儘管解決了封鎖、將查詢最佳化,並驗證您的 MAXDOP 設定,但資料庫始或彈性集區終將達到其限制,則請考慮調整資料庫或彈性集區,以提高背景工作限制。

依服務層級和計算大小尋找 Azure SQL Database 的資源限制:

深入了解背景工作的 Azure SQL Database 資源治理

錯誤 10929:資源識別碼:1

10929: Resource ID: 1. The %s minimum guarantee is %d, maximum limit is %d and the current usage for the database is %d. However, the server is currently too busy to support requests greater than %d for this database. See http://go.microsoft.com/fwlink/?LinkId=267637 for assistance. Otherwise, please try again later.

錯誤 40501:服務目前忙碌中

40501: The service is currently busy. Retry the request after 10 seconds. Incident ID: %ls. Code: %d.

錯誤 40501 是引擎節流錯誤,指出已超過資源限制。

如需資源限制的詳細資訊,請參閱邏輯 SQL 伺服器資源限制

錯誤 40544:資料庫已達到大小配額

40544: The database has reached its size quota. Partition or delete data, drop indexes, or consult the documentation for possible resolutions. Incident ID: <ID>. Code: <code>.

當資料庫已達到大小配額時,就會發生此錯誤。

下列步驟可協助您解決此問題或提供更多選項:

  1. 使用 Azure 入口網站中的儀表板來檢查目前的資料庫大小。

    注意

    若要識別哪些資料表耗用最多空間,因而可能需要加以清除,請執行下列 SQL 查詢:

    SELECT o.name,
     SUM(p.row_count) AS 'Row Count',
     SUM(p.reserved_page_count) * 8.0 / 1024 AS 'Table Size (MB)'
    FROM sys.objects o
    JOIN sys.dm_db_partition_stats p on p.object_id = o.object_id
    GROUP BY o.name
    ORDER BY [Table Size (MB)] DESC;
    GO
    
  2. 如果目前大小未超過您版本支援的大小上限,您可以使用 ALTER DATABASE 來增加 MAXSIZE 設定。

  3. 如果資料庫已經超過您版本支援的大小上限,請嘗試下列一或多個步驟:

    • 執行一般資料庫清除活動。 例如,使用截斷/刪除來清除不想要的資料,或者,使用 SQL Server Integration Services (SSIS) 或大量複製程式 (bcp) 公用程式來移動資料。
    • 資料分割或刪除資料、卸除索引,或參閱可能解決方式的文件。
    • 如需調整資料庫,請參閱調整單一資料庫資源調整彈性集區資源

錯誤 40549:工作階段已終止,因為您有長時間執行的交易

40549: Session is terminated because you have a long-running transaction. Try shortening your transaction.

如果您重複遇到此錯誤,請嘗試遵循下列步驟來解決此問題:

  1. 執行下列查詢,以查看 duration_ms 資料行具有高值的任何開啟的工作階段:

    SELECT
        r.start_time, DATEDIFF(ms,start_time, SYSDATETIME()) as duration_ms, 
        r.session_id, r.request_id, r.blocking_session_id,  
        r.status, r.command, DB_NAME(r.database_id) AS database_name,
        i.parameters, i.event_info AS input_buffer,
        r.last_wait_type, r.open_transaction_count, r.total_elapsed_time, r.cpu_time,
        r.logical_reads, r.writes, s.login_time, s.login_name, s.program_name, s.host_name
    FROM sys.dm_exec_requests as r
    JOIN sys.dm_exec_sessions as s on r.session_id=s.session_id
    OUTER APPLY sys.dm_exec_input_buffer (r.session_id,r.request_id) AS i
    WHERE s.is_user_process=1
    ORDER BY start_time ASC;
    GO
    

    您可以略過 input_buffer 資料行顯示從 sys.fn_MSxe_read_event_stream 讀取之查詢的資料列:這些要求與擴充的事件工作階段相關。

  2. 檢閱 blocking_session_id 資料行,以查看封鎖是否有助於長時間執行的交易。

    注意

    如需針對 Azure SQL Database 中的封鎖進行疑難排解的詳細資訊,請參閱了解和解決 Azure SQL Database 封鎖問題

  3. 請考慮批次處理您的查詢。 如需批次處理的相關資訊,請參閱如何使用批次處理來改善 SQL Database 應用程式效能

錯誤 40551:已終止工作階段,因為它過度使用 tempdb

40551: The session has been terminated because of excessive TEMPDB usage. Try modifying your query to reduce the temporary table space usage.

若要暫時解決此問題,請遵循下列步驟:

  1. 變更查詢,以減少暫存資料表的空間使用量。
  2. 不再需要暫存物件之後,請加以卸除。
  3. 截斷資料表或移除未使用的資料表。

錯誤 40552:已終止工作階段,因為它過度使用交易記錄空間使用量

40552: The session has been terminated because of excessive transaction log space usage. Try modifying fewer rows in a single transaction.

若要解決此問題,請嘗試下列方法:

錯誤 40553:已終止工作階段,因為它過度使用記憶體

40553: The session has been terminated because of excessive memory usage. Try modifying your query to process fewer rows.

若要解決此問題,請嘗試將查詢最佳化。

如需深入了解疑難排解程序,請參閱我的查詢是否在雲端中正常運作?

如需有關其他記憶體不足錯誤和範例查詢的詳細資訊,請參閱針對 Azure SQL Database 中的記憶體不足錯誤進行疑難排解

資源治理錯誤訊息表

錯誤碼 嚴重性 描述
10928 20 Resource ID: %d. The %s limit for the database is %d and has been reached. See 'http://go.microsoft.com/fwlink/?LinkId=267637' for assistance.

資源識別碼可指出已達到限制的資源。 當資源識別碼 = 1 時,這表示已達到背景工作限制。 深入了解錯誤 10928:資源識別碼:1。資料庫的要求限制為 %d 且已達到此限制。當資源識別碼 = 2 時,這表示已達到工作階段限制。

深入了解資源限制:
邏輯 SQL Server 資源限制
針對單一資料庫且以 DTU 為基礎的限制
針對單一資料庫且以虛擬核心為基礎的限制
Azure SQL 受控執行個體資源限制
10936 20 Resource ID: %d. The %s limit for the elastic pool is %d and has been reached. See 'http://go.microsoft.com/fwlink/?LinkId=267637' for assistance.

資源識別碼可指出已達到限制的資源。 當資源識別碼 = 1 時,這表示已達到背景工作限制。 深入了解錯誤 10936:資源識別碼:1。彈性集區的要求限制為 %d 且已達到此限制。。 當資源識別碼 = 2 時,這表示已達到工作階段限制。

深入了解資源限制:
邏輯 SQL Server 資源限制
針對彈性集區且以 DTU 為基礎的限制
針對彈性集區且以虛擬核心為基礎的限制
Azure SQL 受控執行個體資源限制
10929 20 Resource ID: %d. The %s minimum guarantee is %d, maximum limit is %d, and the current usage for the database is %d. However, the server is currently too busy to support requests greater than %d for this database.

資源識別碼可指出已達到限制的資源。 對於背景工作執行緒,資源識別碼 = 1。 對於工作階段,資源識別碼 = 2。 如需詳細資訊,請參閱:
邏輯 SQL Server 資源限制
針對單一資料庫且以 DTU 為基礎的限制
針對彈性集區且以 DTU 為基礎的限制
針對單一資料庫且以虛擬核心為基礎的限制
針對彈性集區且以虛擬核心為基礎的限制
Azure SQL 受控執行個體資源限制
否則,請稍後再試一次。
40544 20 The database has reached its size quota. Partition or delete data, drop indexes, or consult the documentation for possible resolutions.

如需調整資料庫,請參閱調整單一資料庫資源調整彈性集區資源
40549 16 Session is terminated because you have a long-running transaction. Try shortening your transaction.

如需批次處理的相關資訊,請參閱如何使用批次處理來改善 SQL Database 應用程式效能
40550 16 The session has been terminated because it has acquired too many locks. Try reading or modifying fewer rows in a single transaction.

如需批次處理的相關資訊,請參閱如何使用批次處理來改善 SQL Database 應用程式效能
40551 16 The session has been terminated because of excessive tempdb usage. Try modifying your query to reduce the temporary table space usage.

如果您是使用暫存物件,請在工作階段不再需要暫存物件時予以卸除,藉此節省 tempdb 資料庫的空間。 如需 SQL Database 中 tempdb 限制的詳細資訊,請參閱 SQL Database 中的 tempdb 資料庫
40552 16 The session has been terminated because of excessive transaction log space usage. Try modifying fewer rows in a single transaction.

如需批次處理的相關資訊,請參閱如何使用批次處理來改善 SQL Database 應用程式效能

如果您使用 bcp.exe 公用程式或 System.Data.SqlClient.SqlBulkCopy 類別執行大量插入,請嘗試使用 -b batchsizeBatchSize 選項來限制每一筆交易中要複製到伺服器的資料列數目。 如果您要使用 ALTER INDEX 陳述式重建索引,請嘗試使用 REBUILD WITH ONLINE = ON 選項。 如需虛擬核心購買模型的交易記錄大小相關資訊,請參閱:
針對單一資料庫且以虛擬核心為基礎的限制
針對彈性集區且以虛擬核心為基礎的限制
Azure SQL 受控執行個體資源限制
40553 16 The session has been terminated because of excessive memory usage. Try modifying your query to process fewer rows.

減少 Transact-SQL 程式碼中的 ORDER BYGROUP BY 作業數目,從而減少查詢的記憶體需求。 如需調整資料庫,請參閱調整單一資料庫資源調整彈性集區資源。 如需有關記憶體不足錯誤和範例查詢的詳細資訊,請參閱針對 Azure SQL Database 中的記憶體不足錯誤進行疑難排解

彈性集區錯誤

下列錯誤與建立及使用彈性集區有關:

錯誤碼 嚴重性 描述 更正措施
1132 17 The elastic pool has reached its storage limit. The storage usage for the elastic pool cannot exceed (%d) MBs.

彈性集區達到儲存體限制時,嘗試將資料寫入資料庫。 如需資源限制的相關資訊,請參閱:
針對彈性集區且以 DTU 為基礎的限制
針對彈性集區且以虛擬核心為基礎的限制
請考慮盡可能增加彈性集區的 DTU 及/或儲存體,以提高其儲存體限制、減少彈性集區中個別資料庫所使用的儲存體量,或是從彈性集區移除資料庫。 若要調整彈性集區,請參閱調整彈性集區資源。 如需從資料庫移除未使用空間的詳細資訊,請參閱在 Azure SQL Database 中管理資料庫的檔案空間
10929 16 The %s minimum guarantee is %d, maximum limit is %d, and the current usage for the database is %d. However, the server is currently too busy to support requests greater than %d for this database.

如需資源限制的相關資訊,請參閱:
針對彈性集區且以 DTU 為基礎的限制
針對彈性集區且以虛擬核心為基礎的限制
否則,請稍後再試一次。 每個資料庫的最小 DTU / vCore;每個資料庫的最大 DTU / vCore。 彈性集區中的所有資料庫並行背景工作總數試圖超過集區限制。
請考慮盡可能增加彈性集區的 DTU 或 vCore,以提高其背景工作數的限制,或是從彈性集區移除資料庫。
40844 16 Database '%ls' on Server '%ls' is a '%ls' edition database in an elastic pool and cannot have a continuous copy relationship. N/A
40857 16 Elastic pool not found for server: '%ls', elastic pool name: '%ls'. Specified elastic pool does not exist in the specified server. 請提供有效的彈性集區名稱。
40858 16 Elastic pool '%ls' already exists in server: '%ls'. Specified elastic pool already exists in the specified server. 請提供新的彈性集區名稱。
40859 16 Elastic pool does not support service tier '%ls'. Specified service tier is not supported for elastic pool provisioning. 請提供正確的版本,或者將服務層級保留空白,以使用預設的服務層級。
40860 16 Elastic pool '%ls' and service objective '%ls' combination is invalid. Elastic pool and service tier can be specified together only if resource type is specified as 'ElasticPool'. 請指定正確的彈性集區和服務層級組合。
40861 16 The database edition '%.*ls' cannot be different than the elastic pool service tier which is '%.*ls'. The database edition is different than the elastic pool service tier. 請勿指定與彈性集區服務層級不同的資料庫版本。 不需要指定資料庫版本。
40862 16 Elastic pool name must be specified if the elastic pool service objective is specified. Elastic pool service objective does not uniquely identify an elastic pool. 使用彈性集區服務目標時,請指定彈性集區名稱。
40864 16 The DTUs for the elastic pool must be at least (%d) DTUs for service tier '%.*ls'. Attempting to set the DTUs for the elastic pool below the minimum limit. 請重試將彈性集區的 DTU 數至少設為等於最小限制。
40865 16 The DTUs for the elastic pool cannot exceed (%d) DTUs for service tier '%.*ls'. Attempting to set the DTUs for the elastic pool above the maximum limit. 請重試將彈性集區的 DTU 數設為低於最大限制。
40867 16 The DTU max per database must be at least (%d) for service tier '%.*ls'. Attempting to set the DTU max per database below the supported limit. 請考慮使用可支援所需設定的彈性集區服務層級。
40868 16 The DTU max per database cannot exceed (%d) for service tier '%.*ls'. Attempting to set the DTU max per database beyond the supported limit. 請考慮使用可支援所需設定的彈性集區服務層級。
40870 16 The DTU min per database cannot exceed (%d) for service tier '%.*ls'. Attempting to set the DTU min per database beyond the supported limit. 請考慮使用可支援所需設定的彈性集區服務層級。
40873 16 The number of databases (%d) and DTU min per database (%d) cannot exceed the DTUs of the elastic pool (%d). Attempting to specify DTU min for databases in the elastic pool that exceeds the DTUs of the elastic pool. 請考慮增加彈性集區的 DTU 數,或減少每個資料庫的最小 DTU,或是減少彈性集區中的資料庫數目。
40877 16 An elastic pool cannot be deleted unless it does not contain any databases. The elastic pool contains one or more databases and therefore cannot be deleted. 若要刪除彈性集區,請移除其中的資料庫。
40881 16 The elastic pool '%.*ls' has reached its database count limit. The database count limit for the elastic pool cannot exceed (%d) for an elastic pool with (%d) DTUs. Attempting to create or add database to elastic pool when the database count limit of the elastic pool has been reached. 請考慮盡可能增加彈性集區的 DTU 數,以提高其資料庫限制,或是從彈性集區移除資料庫。
40889 16 The DTUs or storage limit for the elastic pool '%.*ls' cannot be decreased since that would not provide sufficient storage space for its databases. Attempting to decrease the storage limit of the elastic pool below its storage usage. 請考慮減少彈性集區中之個別資料庫的儲存體使用量,或從集區中移除資料庫,以便減少集區的 DTU 數或儲存體限制。
40891 16 The DTU min per database (%d) cannot exceed the DTU max per database (%d). Attempting to set the DTU min per database higher than the DTU max per database. 請確定每個資料庫的最小 DTU 並未超過每個資料庫的最大 DTU。
TBD 16 The storage size for an individual database in an elastic pool cannot exceed the max size allowed by '%.*ls' service tier elastic pool. The max size for the database exceeds the max size allowed by the elastic pool service tier. 請將資料庫的大小上限設定在彈性集區服務層級所允許的大小上限內。

無法開啟登入所要求的資料庫 "master"。 登入失敗

發生此問題的原因是帳戶沒有存取 master 資料庫的權限。 但根據預設,SQL Server Management Studio (SSMS) 會嘗試連線到 master 資料庫。

若要解決此問題,請依照下列步驟執行︰

  1. 在 SSMS 的登入畫面上,選取 [選項],然後選取 [連線屬性]。

  2. 在 [連接到資料庫] 欄位中,輸入使用者的預設資料庫名稱作為預設的登入資料庫,然後選取 [連線]。

    SSMS 中 [連線] 對話方塊的螢幕擷取畫面,其中顯示 [連線屬性] 索引標籤。

唯讀錯誤

如果您嘗試寫入唯讀的資料庫,將會收到錯誤。 在某些情況下,資料庫唯讀狀態的原因可能不會立即清除。

錯誤 3906:無法更新資料庫 'databaseName',因為該資料庫是唯讀狀態。

嘗試修改唯讀資料庫時,將會引發下列錯誤。

Msg 3906, Level 16, State 2, Line 1
Failed to update database "%d" because the database is read-only.

資料庫唯讀的原因有多種可能的說明。

手動容錯移轉之後,應用程式仍會連線到舊複本

在 Azure SQL 資料庫中,容錯移轉至其他複本之後,您的應用程式可能仍會因為 DNS 而連線至先前的主要複本。 容錯移轉群組連線路由是使用 DNS 實作的。

可能的根本原因:

  1. 在容錯移轉期間,容錯移轉群組端點會更新為透過變更適當 DNS 項目的目標,指向適當的新的主要伺服器和新的次要伺服器。 依預設,DNS 項目以 30 秒的 TTL 建立,這表示 DNS 用戶端會快取這些項目 30 秒。 因此,DNS 記錄的更新不會立即傳播;項目將會過時,直到所有用戶端和中繼節點都已重新整理快取為止。 因此,在容錯移轉後,登入容錯移轉群組端點可能需要 0 至大約 10 分鐘 (取決於網路拓撲),才能路由至新目標。 排清 DNS 快取可能會也可能不會解決問題,因為回應 DNS 要求的中繼網路節點也會暫時快取 DNS 結果。

    此問題的建議因應措施是只需等到在用戶端上重新整理 DNS 項目。 目前,此因應措施會導致在 10 分鐘內自行解決問題。

  2. 某些 SQL 用戶端程式庫使用稱為「連接共用」的功能,此功能會重複使用與相同資料來源的連接,而不是在需要新的資料庫連接時關閉並重新開啟它們。 特別是,依預設,ADO.NET 中會啟用連接共用。 在結合 1) 中所述的問題時,連接共用可能會導致新開啟的連接重複使用與舊資料庫的連接,從而阻止應用程式無限期地連接至新的主要資料庫。

解決方案:

在容錯移轉群組容錯移轉之後,此 DNS 問題有三種可能的因應措施:

  1. 修改應用程式以在遇到「唯讀」錯誤時呼叫 SQLConnection.ClearAllPoolsSQLConnection.ClearPool(conn)
  2. 在應用程式連接字串中,指定 Pooling=False 以停用連接共用。 應對此進行測試,因為如果應用程式經常開啟和關閉連接,可能會顯著影響效能。
  3. 避免 DNS 複寫/快取延遲的另一個選項是,在遇到 3906 後的一段時間內使用 Azure SQL 資料庫邏輯伺服器名稱 (原始次要伺服器,現在是新的主要伺服器) 直接連接。

您可能已連線至唯讀複本

針對 Azure SQL 資料庫和 Azure SQL 受控執行個體,您可能已連線至唯讀複本上的資料庫。 在此情況下,使用 DATABASEPROPERTYEX() 函式 的下列查詢會傳回 READ_ONLY

SELECT DATABASEPROPERTYEX(DB_NAME(), 'Updateability');
GO

若您正在使用 SQL Server Management Studio 進行連線,請確認是否已在連線選項的 [其他連線參數] 索引標籤中指定 ApplicationIntent=ReadOnly

若連線來自應用程式或使用連接字串的用戶端,請驗證連接字串是否已指定 ApplicationIntent=ReadOnly。 深入了解連線至唯讀複本

資料庫可能已設定為唯讀

如果您正在使用 Azure SQL 資料庫,則資料庫本身可能已設定為唯讀。 您可以使用下列查詢來驗證資料庫的狀態:

SELECT name, is_read_only
FROM sys.databases
WHERE database_id = DB_ID();

您可以使用 ALTER DATABASE Transact-SQL,在 Azure SQL Database 中修改資料庫的唯讀狀態。 您目前無法在受控執行個體中將資料庫設定為唯讀。

確認錯誤是否因為連線問題而造成

若要確認錯誤是否因為連線問題而造成,請檢閱顯示開啟連線呼叫的框架堆疊追蹤,如下列所示 (請注意對 SqlConnection 類別的參考):

System.Data.SqlClient.SqlConnection.TryOpen(TaskCompletionSource`1 retry)
 at System.Data.SqlClient.SqlConnection.Open()
 at AzureConnectionTest.Program.Main(String[] args)
ClientConnectionId:<Client connection ID>

當查詢問題觸發例外狀況時,您將注意到如下的呼叫堆疊 (請注意對 SqlCommand 類別的參考)。 在此情況下,請調整您的查詢

  at System.Data.SqlClient.SqlCommand.ExecuteReader()
  at AzureConnectionTest.Program.Main(String[] args)
  ClientConnectionId:<Client ID>

如需微調效能的詳細資訊,請參閱下列資源: