首页 > 解决方案 > 使用 Docker 映像还原 SQL Server 数据库,导致 SA 登录错误

问题描述

我正在尝试使用 Docker 还原 SQL Server 数据库。我正在使用这个 dockerFile 并运行 build 命令,我已经在我的机器上安装了 SQL Server 2017。

FROM mcr.microsoft.com/mssql/server:2019-latest AS build
ENV ACCEPT_EULA=Y
ENV SA_PASSWORD=Pwd12345
ENV MSSQL_PID=Enterprise

WORKDIR /tmp
COPY AdventureWorksLT2017.bak .
COPY restore-backup.sql .
  
RUN /opt/mssql/bin/sqlservr --accept-eula & sleep 10 \
    && /opt/mssql-tools/bin/sqlcmd -S localhost -U SA -P 'Pwd12345' -i /tmp/restore-backup.sql \
    && pkill sqlservr

FROM mcr.microsoft.com/mssql/server:2019-latest AS release
ENV ACCEPT_EULA=Y
ENV SA_PASSWORD=Pwd12345

COPY --from=build /var/opt/mssql/data /var/opt/mssql/data

但是我在恢复我的 SQL Server 数据库时遇到了这个错误,如下所示

错误:18456,严重性:14,状态:7
。用户“SA”登录失败。原因:评估密码时出错。[客户端:172.17.0.2]
适用于 SQL Server 的 Microsoft ODBC 驱动程序 17:用户“SA”登录失败。
适用于 SQL Server 的 Microsoft ODBC 驱动程序 17:TCP 提供程序:错误代码 0x2749。

以下是执行的完整快照。

PS C:\Users\Dhruv\Downloads\Docker with SQL> docker build -t restored-db .
Sending build context to Docker daemon  7.548MB
Step 1/12 : FROM mcr.microsoft.com/mssql/server:2019-latest AS build
2019-latest: Pulling from mssql/server
5b7339215d1d: Pull complete
14ca88e9f672: Pull complete
a31c3b1caad4: Pull complete
b054a26005b7: Pull complete
59f979819d9b: Pull complete
29eb18117119: Pull complete
e0607dd0db6f: Pull complete
b87654acc43f: Pull complete
Digest: sha256:c7e1ece8ec34938c78d715eb5d2ddc827f0b92debc071dcfca122a6f94ce540d
Status: Downloaded newer image for mcr.microsoft.com/mssql/server:2019-latest
 ---> 8d8453707e16
Step 2/12 : ENV ACCEPT_EULA=Y
 ---> Running in 364206e05ac8
Removing intermediate container 364206e05ac8
 ---> 31c051fd4567
Step 3/12 : ENV SA_PASSWORD=Pwd12345
 ---> Running in be7548fee19f
Removing intermediate container be7548fee19f
 ---> 467c830d396d
Step 4/12 : ENV MSSQL_PID=Enterprise
 ---> Running in 490207525c24
Removing intermediate container 490207525c24
 ---> 251f16905093
Step 5/12 : WORKDIR /tmp
 ---> Running in a1e442d92e9a
Removing intermediate container a1e442d92e9a
 ---> 540549c2f5f7
Step 6/12 : COPY AdventureWorksLT2017.bak .
 ---> 0ba530da54c3
Step 7/12 : COPY restore-backup.sql .
 ---> 1acf41f50e96
Step 8/12 : RUN /opt/mssql/bin/sqlservr --accept-eula & sleep 10        && /opt/mssql-tools/bin/sqlcmd -S localhost -U SA -P 'Pwd12345' -i /tmp/restore-backup.sql      && pkill sqlservr
 ---> Running in 24070118cc8b
2020-09-05 11:53:27.83 Server      The licensing PID was successfully processed. The new edition is [Enterprise Edition].
2020-09-05 11:53:29.52 Server      Setup step is copying system data file 'C:\templatedata\master.mdf' to '/var/opt/mssql/data/master.mdf'.
2020-09-05 11:53:29.56 Server      Did not find an existing master data file /var/opt/mssql/data/master.mdf, copying the missing default master and other system database files. If you have moved the database location, but not moved the database files, startup may fail. To repair: shutdown SQL Server, move the master database to configured location, and restart.
2020-09-05 11:53:29.62 Server      Setup step is copying system data file 'C:\templatedata\mastlog.ldf' to '/var/opt/mssql/data/mastlog.ldf'.
2020-09-05 11:53:29.66 Server      Setup step is copying system data file 'C:\templatedata\model.mdf' to '/var/opt/mssql/data/model.mdf'.
2020-09-05 11:53:29.71 Server      Setup step is copying system data file 'C:\templatedata\modellog.ldf' to '/var/opt/mssql/data/modellog.ldf'.
2020-09-05 11:53:29.75 Server      Setup step is copying system data file 'C:\templatedata\msdbdata.mdf' to '/var/opt/mssql/data/msdbdata.mdf'.
2020-09-05 11:53:29.80 Server      Setup step is copying system data file 'C:\templatedata\msdblog.ldf' to '/var/opt/mssql/data/msdblog.ldf'.
2020-09-05 11:53:29.84 Server      Setup step is FORCE copying system data file 'C:\templatedata\model_replicatedmaster.mdf' to '/var/opt/mssql/data/model_replicatedmaster.mdf'.
2020-09-05 11:53:29.88 Server      Setup step is FORCE copying system data file 'C:\templatedata\model_replicatedmaster.ldf' to '/var/opt/mssql/data/model_replicatedmaster.ldf'.
2020-09-05 11:53:29.91 Server      Setup step is FORCE copying system data file 'C:\templatedata\model_msdbdata.mdf' to '/var/opt/mssql/data/model_msdbdata.mdf'.
2020-09-05 11:53:29.96 Server      Setup step is FORCE copying system data file 'C:\templatedata\model_msdblog.ldf' to '/var/opt/mssql/data/model_msdblog.ldf'.
2020-09-05 11:53:30.27 Server      Microsoft SQL Server 2019 (RTM-CU7) (KB4570012) - 15.0.4063.15 (X64)
        Aug 15 2020 10:48:11
        Copyright (C) 2019 Microsoft Corporation
        Enterprise Edition (64-bit) on Linux (Ubuntu 18.04.5 LTS) <X64>
2020-09-05 11:53:30.31 Server      UTC adjustment: 0:00
2020-09-05 11:53:30.32 Server      (c) Microsoft Corporation.
2020-09-05 11:53:30.33 Server      All rights reserved.
2020-09-05 11:53:30.34 Server      Server process ID is 40.
2020-09-05 11:53:30.35 Server      Logging SQL Server messages in file '/var/opt/mssql/log/errorlog'.
2020-09-05 11:53:30.36 Server      Registry startup parameters:
         -d /var/opt/mssql/data/master.mdf
         -l /var/opt/mssql/data/mastlog.ldf
         -e /var/opt/mssql/log/errorlog
2020-09-05 11:53:30.38 Server      Command Line Startup Parameters:
         --accept-eula
2020-09-05 11:53:30.40 Server      SQL Server detected 1 sockets with 2 cores per socket and 4 logical processors per socket, 4 total logical processors; using 4 logical processors based on SQL Server licensing. This is an informational message; no user action is required.
2020-09-05 11:53:30.43 Server      SQL Server is starting at normal priority base (=7). This is an informational message only. No user action is required.
2020-09-05 11:53:30.45 Server      Detected 5013 MB of RAM. This is an informational message; no user action is required.
2020-09-05 11:53:30.47 Server      Using conventional memory in the memory manager.
2020-09-05 11:53:30.49 Server      Page exclusion bitmap is enabled.
2020-09-05 11:53:30.54 Server      Buffer pool extension is not supported on Linux platform.
2020-09-05 11:53:30.56 Server      Buffer Pool: Allocating 1048576 bytes for 722742 hashPages.
2020-09-05 11:53:31.64 Server      Buffer pool extension is already disabled. No action is necessary.
2020-09-05 11:53:34.87 Server      Successfully initialized the TLS configuration. Allowed TLS protocol versions are ['1.0 1.1 1.2']. Allowed TLS ciphers are ['ECDHE-ECDSA-AES128-GCM-SHA256:ECDHE-ECDSA-AES256-GCM-SHA384:ECDHE-RSA-AES128-GCM-SHA256:ECDHE-RSA-AES256-GCM-SHA384:ECDHE-ECDSA-AES128-SHA256:ECDHE-ECDSA-AES256-SHA384:ECDHE-ECDSA-AES256-SHA:ECDHE-ECDSA-AES128-SHA:AES256-GCM-SHA384:AES128-GCM-SHA256:AES256-SHA256:AES128-SHA256:AES256-SHA:AES128-SHA:!DHE-RSA-AES256-GCM-SHA384:!DHE-RSA-AES128-GCM-SHA256:!DHE-RSA-AES256-SHA:!DHE-RSA-AES128-SHA'].
2020-09-05 11:53:35.01 Server      Query Store settings initialized with enabled = 1,
2020-09-05 11:53:35.06 Server      The maximum number of dedicated administrator connections for this instance is '1'
2020-09-05 11:53:35.06 Server      Node configuration: node 0: CPU mask: 0x000000000000000f:0 Active CPU mask: 0x000000000000000f:0. This message provides a description of the NUMA configuration for this computer. This is an informational message only. No user action is required.
2020-09-05 11:53:35.13 Server      Using dynamic lock allocation.  Initial allocation of 2500 Lock blocks and 5000 Lock Owner blocks per node.  This is an informational message only.  No user action is required.
2020-09-05 11:53:35.21 Server      In-Memory OLTP initialized on lowend machine.
2020-09-05 11:53:35.31 Server      CLR version v4.0.30319 loaded.
2020-09-05 11:53:35.40 Server      [INFO] Created Extended Events session 'hkenginexesession'
2020-09-05 11:53:35.41 Server      Database Instant File Initialization: enabled. For security and performance considerations see the topic 'Database Instant File Initialization' in SQL Server Books Online. This is an informational message only. No user action is required.
ForceFlush is enabled for this instance.
2020-09-05 11:53:35.46 Server      Total Log Writer threads: 2. This is an informational message; no user action is required.
2020-09-05 11:53:35.49 Server      clflushopt is selected for pmem flush operation.
2020-09-05 11:53:35.50 Server      Software Usage Metrics is disabled.
2020-09-05 11:53:35.52 spid9s      [1]. Feature Status: PVS: 0. CTR: 0. ConcurrentPFSUpdate: 1.
2020-09-05 11:53:35.54 spid9s      Starting up database 'master'.
ForceFlush feature is enabled for log durability.
2020-09-05 11:53:35.85 spid9s      The tail of the log for database master is being rewritten to match the new sector size of 4096 bytes.  3584 bytes at offset 393728 in file /var/opt/mssql/data/mastlog.ldf will be written.
2020-09-05 11:53:35.95 Server      Common language runtime (CLR) functionality initialized.
2020-09-05 11:53:36.27 spid9s      Converting database 'master' from version 897 to the current version 904.
2020-09-05 11:53:36.28 spid9s      Database 'master' running the upgrade step from version 897 to version 898.
2020-09-05 11:53:36.35 spid9s      Database 'master' running the upgrade step from version 898 to version 899.
2020-09-05 11:53:36.47 spid9s      Database 'master' running the upgrade step from version 899 to version 900.
2020-09-05 11:53:36.50 spid9s      Database 'master' running the upgrade step from version 900 to version 901.
2020-09-05 11:53:36.53 spid9s      Database 'master' running the upgrade step from version 901 to version 902.
2020-09-05 11:53:36.58 spid9s      Database 'master' running the upgrade step from version 902 to version 903.
2020-09-05 11:53:36.61 spid9s      Database 'master' running the upgrade step from version 903 to version 904.
2020-09-05 11:53:37.06 spid9s      Resource governor reconfiguration succeeded.
2020-09-05 11:53:37.08 spid9s      SQL Server Audit is starting the audits. This is an informational message. No user action is required.
2020-09-05 11:53:37.10 spid9s      SQL Server Audit has started the audits. This is an informational message. No user action is required.
2020-09-05 11:53:37.21 spid9s      SQL Trace ID 1 was started by login "sa".
2020-09-05 11:53:37.27 spid9s      Server name is '66d529291286'. This is an informational message only. No user action is required.
2020-09-05 11:53:37.33 spid36s     Password policy update was successful.
2020-09-05 11:53:37.36 spid9s      [4]. Feature Status: PVS: 0. CTR: 0. ConcurrentPFSUpdate: 1.
2020-09-05 11:53:37.37 spid12s     [32767]. Feature Status: PVS: 0. CTR: 0. ConcurrentPFSUpdate: 1.
2020-09-05 11:53:37.38 spid9s      Starting up database 'msdb'.
2020-09-05 11:53:37.40 spid39s     Always On: The availability replica manager is starting. This is an informational message only. No user action is required.
2020-09-05 11:53:37.42 spid12s     Starting up database 'mssqlsystemresource'.
2020-09-05 11:53:37.44 spid39s     Always On: The availability replica manager is waiting for the instance of SQL Server to allow client connections. This is an informational message only. No user action is required.
2020-09-05 11:53:37.49 spid12s     The resource database build version is 15.00.4063. This is an informational message only. No user action is required.
2020-09-05 11:53:37.58 spid12s     [3]. Feature Status: PVS: 0. CTR: 0. ConcurrentPFSUpdate: 1.
2020-09-05 11:53:37.59 spid12s     Starting up database 'model'.
2020-09-05 11:53:37.81 spid9s      The tail of the log for database msdb is being rewritten to match the new sector size of 4096 bytes.  3072 bytes at offset 50176 in file /var/opt/mssql/data/MSDBLog.ldf will be written.
2020-09-05 11:53:37.87 spid36s     A self-generated certificate was successfully loaded for encryption.
2020-09-05 11:53:37.89 spid36s     Server is listening on [ 'any' <ipv6> 1433].
2020-09-05 11:53:37.91 spid36s     Server is listening on [ 'any' <ipv4> 1433].
2020-09-05 11:53:37.95 Server      Server is listening on [ ::1 <ipv6> 1434].
2020-09-05 11:53:37.96 spid12s     The tail of the log for database model is being rewritten to match the new sector size of 4096 bytes.  512 bytes at offset 73216 in file /var/opt/mssql/data/modellog.ldf will be written.
2020-09-05 11:53:37.98 Server      Server is listening on [ 127.0.0.1 <ipv4> 1434].
2020-09-05 11:53:38.00 Server      Dedicated admin connection support was established for listening locally on port 1434.
2020-09-05 11:53:38.04 spid36s     Server is listening on [ ::1 <ipv6> 1431].
2020-09-05 11:53:38.06 spid36s     Server is listening on [ 127.0.0.1 <ipv4> 1431].
2020-09-05 11:53:38.08 spid36s     SQL Server is now ready for client connections. This is an informational message; no user action is required.
2020-09-05 11:53:38.08 spid9s      Converting database 'msdb' from version 897 to the current version 904.
2020-09-05 11:53:38.11 spid9s      Database 'msdb' running the upgrade step from version 897 to version 898.
2020-09-05 11:53:38.16 spid12s     Converting database 'model' from version 897 to the current version 904.
2020-09-05 11:53:38.18 spid12s     Database 'model' running the upgrade step from version 897 to version 898.
2020-09-05 11:53:38.20 spid9s      Database 'msdb' running the upgrade step from version 898 to version 899.
2020-09-05 11:53:38.24 spid12s     Database 'model' running the upgrade step from version 898 to version 899.
2020-09-05 11:53:38.28 spid9s      Database 'msdb' running the upgrade step from version 899 to version 900.
2020-09-05 11:53:38.30 spid12s     Database 'model' running the upgrade step from version 899 to version 900.
2020-09-05 11:53:38.43 spid9s      Database 'msdb' running the upgrade step from version 900 to version 901.
2020-09-05 11:53:38.43 spid12s     Database 'model' running the upgrade step from version 900 to version 901.
2020-09-05 11:53:38.44 Logon       Error: 18456, Severity: 14, State: 7.
**2020-09-05 11:53:38.44 Logon       Login failed for user 'SA'. Reason: An error occurred while evaluating the password. [CLIENT: 172.17.0.2]
Sqlcmd: Error: Microsoft ODBC Driver 17 for SQL Server : Login failed for user 'SA'..
Sqlcmd: Error: Microsoft ODBC Driver 17 for SQL Server : TCP Provider: Error code 0x2749.
Sqlcmd: Error: Microsoft ODBC Driver 17 for SQL Server : A network-related or instance-specific error has occurred while establishing a connection to SQL Server. Server is not found or not accessible. Check if instance name is correct and if SQL Server is configured to allow remote connections. For more information see SQL Server Books Online..**
The command '/bin/sh -c /opt/mssql/bin/sqlservr --accept-eula & sleep 10        && /opt/mssql-tools/bin/sqlcmd -S localhost -U SA -P 'Pwd12345' -i /tmp/restore-backup.sql      && pkill sqlservr' returned a non-zero code: 1

请帮助理解为什么我在恢复数据库时会遇到这种问题。

当我手动进行恢复过程时,它工作正常。我正在使用以下步骤来恢复我的数据库

Step 1:-


docker run `
   -e "ACCEPT_EULA=Y" `
   -e "MSSQL_SA_PASSWORD=YourStrong!Passw0rd" `
   --name "sqlTechno" `
   -p 1401:1433 `
   -v sql1data:/var/opt/mssql `
   -d mcr.microsoft.com/mssql/server:2019-latest


Step 2:-

docker exec -it sqlTechno mkdir /var/opt/mssql/backup

Step 3:-

docker cp AdventureWorksLT2017.bak sqlTechno:/var/opt/mssql/backup

标签: sql-serverdockerdockerfilecontainerssql-server-2017

解决方案


我已按照 AlwaysLearning 的建议更改了密码,并将睡眠时间更改为 60 而不是 Preben Huybrechts 建议的 10 秒。

最终的工作代码片段如下。

FROM mcr.microsoft.com/mssql/server:2017-latest AS build
ENV ACCEPT_EULA=Y
ENV MSSQL_SA_PASSWORD=YourStrong!Passw0rd

WORKDIR /tmp
COPY AdventureWorksLT2017.bak .
COPY restore-backup.sql .
  
RUN /opt/mssql/bin/sqlservr --accept-eula & sleep 60 \
    && /opt/mssql-tools/bin/sqlcmd -S localhost -U SA -P 'YourStrong!Passw0rd' -i /tmp/restore-backup.sql \
    && pkill sqlservr

FROM mcr.microsoft.com/mssql/server:2017-latest AS release
ENV ACCEPT_EULA=Y

COPY --from=build /var/opt/mssql/data /var/opt/mssql/data

推荐阅读