You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Azure Data Studio连接Docker中MSSQL失败:sa用户登录报错求助

Docker部署SQL Server 2019后Azure Data Studio无法登录sa账户问题

我通过Docker创建了MSSQL Server 2019容器,容器状态为运行中,创建命令如下:

docker run -e "ACCEPT_EULA=Y" -e "MSSQL_SA_PASSWORD=Passw0rd!12" -p 1433:1433 -d mcr.microsoft.com/mssql/server:2019-latest

尝试使用Azure Data Studio连接该SQL Server时,出现错误:Login failed for user 'sa'

错误详情如下:

Microsoft.Data.SqlClient.SqlException (0x80131904): Login failed for user 'sa'.
   at Microsoft.Data.SqlClient.SqlInternalConnection.OnError(SqlException exception, Boolean breakConnection, Action`1 wrapCloseInAction)
   at Microsoft.Data.SqlClient.TdsParser.ThrowExceptionAndWarning(TdsParserStateObject stateObj, Boolean callerHasConnectionLock, Boolean asyncClose)
   at Microsoft.Data.SqlClient.TdsParser.TryRun(RunBehavior runBehavior, SqlCommand cmdHandler, SqlDataReader dataStream, BulkCopySimpleResultSet bulkCopyHandler, TdsParserStateObject stateObj, Boolean& dataReady)
   at Microsoft.Data.SqlClient.TdsParser.Run(RunBehavior runBehavior, SqlCommand cmdHandler, SqlDataReader dataStream, BulkCopySimpleResultSet bulkCopyHandler, TdsParserStateObject stateObj)
   at Microsoft.Data.SqlClient.SqlInternalConnectionTds.CompleteLogin(Boolean enlistOK)
   at Microsoft.Data.SqlClient.SqlInternalConnectionTds.AttemptOneLogin(ServerInfo serverInfo, String newPassword, SecureString newSecurePassword, Boolean ignoreSniOpenTimeout, TimeoutTimer timeout, Boolean withFailover)
   at Microsoft.Data.SqlClient.SqlInternalConnectionTds.LoginNoFailover(ServerInfo serverInfo, String newPassword, SecureString newSecurePassword, Boolean redirectedUserInstance, SqlConnectionString connectionOptions, SqlCredential credential, TimeoutTimer timeout)
   at Microsoft.Data.SqlClient.SqlInternalConnectionTds.OpenLoginEnlist(TimeoutTimer timeout, SqlConnectionString connectionOptions, SqlCredential credential, String newPassword, SecureString newSecurePassword, Boolean redirectedUserInstance)
   at Microsoft.Data.SqlClient.SqlInternalConnectionTds..ctor(DbConnectionPoolIdentity identity, SqlConnectionString connectionOptions, SqlCredential credential, Object providerInfo, String newPassword, SecureString newSecurePassword, Boolean redirectedUserInstance, SqlConnectionString userConnectionOptions, SessionData reconnectSessionData, Boolean applyTransientFaultHandling, String accessToken, DbConnectionPool pool)
   at Microsoft.Data.SqlClient.SqlConnectionFactory.CreateConnection(DbConnectionOptions options, DbConnectionPoolKey poolKey, Object poolGroupProviderInfo, DbConnectionPool pool, DbConnection owningConnection, DbConnectionOptions userOptions)
   at Microsoft.Data.ProviderBase.DbConnectionFactory.CreateNonPooledConnection(DbConnection owningConnection, DbConnectionPoolGroup poolGroup, DbConnectionOptions userOptions)
   at Microsoft.Data.ProviderBase.DbConnectionFactory.<>c__DisplayClass48_0.<CreateReplaceConnectionContinuation>b__0(Task`1 _)
   at System.Threading.Tasks.ContinuationResultTaskFromResultTask`2.InnerInvoke()
   at System.Threading.ExecutionContext.RunInternal(ExecutionContext executionContext, ContextCallback callback, Object state)
--- End of stack trace from previous location ---
   at System.Threading.Tasks.Task.ExecuteWithThreadLocal(Task& currentTaskSlot, Thread threadPoolThread)
--- End of stack trace from previous location ---
   at Microsoft.SqlTools.ServiceLayer.Connection.ReliableConnection.ReliableSqlConnection.<>c__DisplayClass30_0.<<OpenAsync>b__0>d.MoveNext() in D:\a\_work\1\s\src\Microsoft.SqlTools.ManagedBatchParser\ReliableConnection\ReliableSqlConnection.cs:line 312
--- End of stack trace from previous location ---
   at Microsoft.SqlTools.ServiceLayer.Connection.ConnectionService.TryOpenConnection(ConnectionInfo connectionInfo, ConnectParams connectionParams) in D:\a\_work\1\s\src\Microsoft.SqlTools.ServiceLayer\Connection\ConnectionService.cs:line 666
ClientConnectionId:7f82dc23-5d48-4820-9f58-4ccf08416472
Error Number:18456,State:1,Class:14

已尝试重启容器、多次重新输入密码、创建新容器,但均未解决问题,期望成功登录本地服务器。


解决步骤

  • 验证容器内sa密码有效性
    执行以下命令进入容器并尝试登录SQL Server:

    docker exec -it <容器ID或名称> /opt/mssql-tools/bin/sqlcmd -S localhost -U SA -P "Passw0rd!12"
    

    如果能成功进入SQL命令行,说明密码本身没问题;如果登录失败,需要重新创建容器并确保密码符合复杂度要求(至少8字符,包含大小写字母、数字、特殊字符)。

  • 检查Azure Data Studio连接配置

    • 服务器地址填写localhost,1433(明确指定端口)
    • 身份验证选择SQL登录,用户名输入sa
    • 密码确保输入准确,注意特殊字符!和数字的输入,避免输入法切换导致的字符不一致
    • 勾选信任服务器证书选项(容器内SQL Server使用自签名证书,默认不被信任)
  • 确认sa账户状态
    进入容器的SQL命令行后,执行以下查询检查sa账户是否被禁用:

    SELECT name, is_disabled FROM sys.server_principals WHERE name = 'sa';
    

    如果is_disabled值为1,执行以下命令启用账户:

    ALTER LOGIN sa ENABLE;
    
  • 检查SQL Server身份验证模式
    在容器的SQL命令行中执行以下查询:

    SELECT name, authentication_mode_desc FROM sys.server_properties WHERE name = 'IsIntegratedSecurityOnly';
    

    如果返回的authentication_mode_desc是WINDOWS_ONLY,说明当前仅启用Windows身份验证,需要切换为混合模式:

    EXEC sp_configure 'show advanced options', 1;
    RECONFIGURE;
    EXEC sp_configure 'mixed authentication mode', 1;
    RECONFIGURE;
    

    执行完成后重启容器:

    docker restart <容器ID或名称>
    

内容的提问来源于stack exchange,提问作者Christine Nguyễn

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.09 16:50:25