下面我来简单介绍一下Docker部署SQL Server 2019 Always On集群的实现攻略。
一、概述
SQL Server 2019 Always On集群是一种高可用的解决方案,可以保证数据库服务的连续性和可靠性。而采用Docker部署SQL Server 2019 Always On集群,则可以更容易地进行部署和管理。
二、实现步骤
- 准备Docker环境
首先需要在服务器上安装并启动Docker服务,具体安装过程可以参考官方文档进行配置。
- 编写Dockerfile文件
在准备好Docker环境之后,需要编写Dockerfile文件,以创建包含SQL Server 2019的Docker映像。以下是一份示例的Dockerfile代码:
#基础镜像
FROM mcr.microsoft.com/mssql/server:2019-latest
#设置环境变量
ENV MSSQL_SA_PASSWORD=<YourStrong!Passw0rd>
ENV ACCEPT_EULA=Y
#运行SQL Server安装脚本
COPY install.sql /var/opt/mssql/init/
RUN /opt/mssql/bin/sqlservr & sleep 10 \
&& /opt/mssql-tools/bin/sqlcmd -S localhost -U SA -P '<YourStrong!Passw0rd>' \
-Q 'CREATE DATABASE testdb;' \
&& /opt/mssql-tools/bin/sqlcmd -S localhost -U SA -P '<YourStrong!Passw0rd>' \
-Q 'USE testdb; CREATE TABLE testtable (id INT, name VARCHAR(200));'
其中,"
- 构建Docker映像
编写好Dockerfile文件后,执行以下命令构建Docker映像:
docker build -t sql2019 .
其中,“sql2019”是构建的Docker映像名称。
- 创建Docker容器
在Docker映像构建完成后,需要创建Docker容器来部署SQL Server 2019 Always On集群。以下是示例的Docker容器创建命令:
docker run --name sql1 -e "ACCEPT_EULA=Y" -e "SA_PASSWORD=<YourStrong!Passw0rd>" \
-p 1433:1433 -d sql2019
其中,“sql1”是创建的Docker容器名称,“-e”参数用于设置环境变量,“-p”参数用于设置端口映射。
- 配置SQL Server 2019 Always On集群
在创建好Docker容器后,需要进一步配置SQL Server 2019 Always On集群。以下是示例的配置命令:
CREATE LOGIN [login] WITH PASSWORD = '<YourPassword>';
CREATE AVAILABILITY GROUP [testag] WITH (CLUSTER_TYPE = EXTERNAL);
ALTER AVAILABILITY GROUP [testag] JOIN REPLICA ON 'sql1' WITH (ENDPOINT_URL = 'TCP://sql1:5022', AVAILABILITY_MODE = SYNCHRONOUS_COMMIT);
ALTER AVAILABILITY GROUP [testag] GRANT CREATE ANY DATABASE;
ALTER AVAILABILITY GROUP [testag] GRANT VIEW ANY DATABASE;
ALTER AVAILABILITY GROUP [testag] GRANT VIEW SERVER STATE;
CREATE AVAILABILITY GROUP [testag] GRANT CONNECT SQL TO [login];
CREATE AVAILABILITY GROUP [testag] GRANT VIEW SERVER STATE TO [login];
CREATE AVAILABILITY GROUP [testag] GRANT VIEW ANY DATABASE TO [login];
这些命令用于创建登录帐户、创建可用性组,以及向可用性组添加副本。
- 启动SQL Server 2019 Always On集群
完成配置后,需要启动SQL Server 2019 Always On集群。以下是示例的命令:
ALERT AVAILABILITY GROUP [testag] WITH (AUTOMATED_BACKUP_PREFERENCE = SECONDARY_ONLY);
ALTER AVAILABILITY GROUP [testag] GRANT CREATE ANY DATABASE;
ALTER AVAILABILITY GROUP [testag] GRANT VIEW SERVER STATE;
ALTER AVAILABILITY GROUP [testag] SET (CLUSTER_TYPE = EXTERNAL);
ALTER AVAILABILITY GROUP [testag] SET (DB_FAILOVER = ON);
ALTER AVAILABILITY GROUP [testag] SET (DTC_SUPPORT = NONE);
ALTER AVAILABILITY GROUP [testag] SET (FAILURE_CONDITION_LEVEL = 3);
ALTER AVAILABILITY GROUP [testag] SET (HEALTH_CHECK_TIMEOUT = 30000);
ALTER AVAILABILITY GROUP [testag] SET (KBAG_ENABLED = ON);
ALTER AVAILABILITY GROUP [testag] SET (REQUIRED_SYNCHRONIZED_SECONDARIES_TO_COMMIT = 2);
ALTER AVAILABILITY GROUP [testag] SET (SEEDING_MODE = AUTOMATIC);
ALTER AVAILABILITY GROUP [testag] SET (VERSION = 10);
三、示例说明
示例一
假设你希望在Docker容器中创建一个名为“testdb”的数据库,并创建一个名为“testtable”的表。
首先需要写一份名为“install.sql”的SQL Server安装脚本,其内容如下:
CREATE DATABASE testdb;
USE testdb;
CREATE TABLE testtable (id INT, name VARCHAR(200));
接下来,在Dockerfile中添加以下代码:
COPY install.sql /var/opt/mssql/init/
RUN /opt/mssql/bin/sqlservr & sleep 10 \
&& /opt/mssql-tools/bin/sqlcmd -S localhost -U SA -P '<YourStrong!Passw0rd>' \
-i /var/opt/mssql/init/install.sql
这样,当Docker容器启动时,会自动执行名为“install.sql”的SQL Server安装脚本。
示例二
假设你希望在Docker容器中创建一个名为“testag”的SQL Server 2019 Always On集群,并向其添加两个副本,一个在“sql1”服务器上,另一个在“sql2”服务器上。
首先需要创建一个名为“sql2”的Docker容器,其命令如下:
docker run --name sql2 -e "ACCEPT_EULA=Y" -e "SA_PASSWORD=<YourStrong!Passw0rd>" \
-p 1434:1433 -d sql2019
接下来,在数据库服务器上执行以下命令:
CREATE LOGIN [login] WITH PASSWORD = '<YourPassword>';
CREATE AVAILABILITY GROUP [testag] WITH (CLUSTER_TYPE = EXTERNAL);
ALTER AVAILABILITY GROUP [testag] JOIN REPLICA ON 'sql1' WITH (ENDPOINT_URL = 'TCP://sql1:5022', AVAILABILITY_MODE = SYNCHRONOUS_COMMIT);
ALTER AVAILABILITY GROUP [testag] JOIN REPLICA ON 'sql2' WITH (ENDPOINT_URL = 'TCP://sql2:5022', AVAILABILITY_MODE = SYNCHRONOUS_COMMIT);
ALTER AVAILABILITY GROUP [testag] GRANT CREATE ANY DATABASE;
ALTER AVAILABILITY GROUP [testag] GRANT VIEW ANY DATABASE;
ALTER AVAILABILITY GROUP [testag] GRANT VIEW SERVER STATE;
CREATE AVAILABILITY GROUP [testag] GRANT CONNECT SQL TO [login];
CREATE AVAILABILITY GROUP [testag] GRANT VIEW SERVER STATE TO [login];
CREATE AVAILABILITY GROUP [testag] GRANT VIEW ANY DATABASE TO [login];
这些命令用于向可用性组添加两个副本。最后,启动SQL Server 2019 Always On集群:
ALERT AVAILABILITY GROUP [testag] WITH (AUTOMATED_BACKUP_PREFERENCE = SECONDARY_ONLY);
ALTER AVAILABILITY GROUP [testag] GRANT CREATE ANY DATABASE;
ALTER AVAILABILITY GROUP [testag] GRANT VIEW SERVER STATE;
ALTER AVAILABILITY GROUP [testag] SET (CLUSTER_TYPE = EXTERNAL);
ALTER AVAILABILITY GROUP [testag] SET (DB_FAILOVER = ON);
ALTER AVAILABILITY GROUP [testag] SET (DTC_SUPPORT = NONE);
ALTER AVAILABILITY GROUP [testag] SET (FAILURE_CONDITION_LEVEL = 3);
ALTER AVAILABILITY GROUP [testag] SET (HEALTH_CHECK_TIMEOUT = 30000);
ALTER AVAILABILITY GROUP [testag] SET (KBAG_ENABLED = ON);
ALTER AVAILABILITY GROUP [testag] SET (REQUIRED_SYNCHRONIZED_SECONDARIES_TO_COMMIT = 2);
ALTER AVAILABILITY GROUP [testag] SET (SEEDING_MODE = AUTOMATIC);
ALTER AVAILABILITY GROUP [testag] SET (VERSION = 10);
本站文章如无特殊说明,均为本站原创,如若转载,请注明出处:Docker部署SQL Server 2019 Always On集群的实现 - Python技术站