合 在Linux中安装MSSQL 2017 Always On Availability Group
Tags: LinuxMSSQLAlways OnAvailability Group可用性组MSSQL2017
- 一、架构
- 安装配置环境
- 安装MSSQL
- 二、SQL Server 证书及权限配置
- 1.所有服务器:启用 hadr 功能(Always On Availability Groups)
- 启用 AlwaysOn_health 事件会话
- 2.所有服务器:为 SQL Server AlwaysOn AG 安装Linux资源代理
- 3.所有服务器:启用防火墙上的 AlwaysOn AG 端点默认端口5022 (或关闭防火墙!)
- 跑脚本完成第4-7步
- 8.各服务器上的证书相互传给其他服务器
- 9.所有服务器:授予证书文件所有者权限
- 10.各实例证书还原及账号授权
- 三、AG配置
- 脚本运行
- 创建AG
- 将次要副本联接到 AG
- 将数据库添加到可用性组
- 图形运行
- 1. 在节点 lhrmssql100 实例上创建可用性组,名为 LINUX_SQLAG
- 2. 选择可用性数据库
- 3. 添加副本并设置
- 四、Linux 集群资源管理器 Pacemaker 安装配置
- 五、配置监听器测试故障转移
- 配置读写分离
- 故障切换
- 自动故障转移测试
- 手动故障转移测试
- 数据字典
- 参考
SQL Server 2017 最多支持9个副本(1个主副本和8个辅助副本),其中最多支持3个自动故障转移和3个同步提交的可用性副本。Linux 环境的AlwaysOn AG 不依赖于 Active Directory,可以在没有 Windows Server故障转移群集(WSFC)和 Pacemaker(Linux集群资源管理器)情况下搭建 Always On Availability Groups。以前是先配置 WSFC 再配置 Always On AG。Linux 环境下可以先配置 Always On AG ,再配置群集资源管理器。
Linux 上安装 SQL Server AG 步骤:
- 在 Linux 群集节点上安装和配置 SQL Server 2017
- 创建 SQL Server 2017 Always On Availability Group
- 配置 Pacemaker - Linux 集群资源管理器
- 将 SQL Server 2017 Always On Availability Group 添加为群集中的资源
相关包目录:https://packages.microsoft.com/
- 搭建MSSQL 2008R2高可用之发布订阅(数据库复制):https://www.dbaup.com/dajianmssql-2008r2gaokeyongzhifabudingyueshujukufuzhi.html
- 搭建MSSQL 2008R2高可用之日志传输(Log Shipping):https://www.dbaup.com/dajianmssql-2008r2gaokeyongzhirizhichuanshulog-shipping.html
- 在非域环境下搭建MSSQL 2016高可用之镜像传输:https://www.dbaup.com/zaifeiyuhuanjingxiadajianmssql-2016gaokeyongzhijingxiangchuanshu.html
- 在Windows非域环境中安装使用MSSQL 2016 Always On Availability Group:https://www.dbaup.com/zaiwindowsfeiyuhuanjingzhonganzhuangshiyongmssql-2016-always-on-availability-group.html
- 在Windows域环境中安装MSSQL 2016 Always On Availability Group:https://www.dbaup.com/zaiwindowsyuhuanjingzhonganzhuangmssql-2016-always-on-availability-group.html
一、架构
安装配置环境
现在创建三台CentOS 7 的服务器: 172.72.9.100,172.72.9.101,172.72.9.102
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 | -- 创建专用网络 docker network create --subnet=172.72.9.0/24 mssql-network docker rm -f lhrmssql100 docker run -d --name lhrmssql100 -h lhrmssql100 \ --net=mssql-network --ip 172.72.9.100 \ -p 61100:1433 \ -v /sys/fs/cgroup:/sys/fs/cgroup \ --privileged=true lhrbest/lhrcentos76:8.5 \ /usr/sbin/init docker rm -f lhrmssql101 docker run -d --name lhrmssql101 -h lhrmssql101 \ --net=mssql-network --ip 172.72.9.101 \ -p 61101:1433 \ -v /sys/fs/cgroup:/sys/fs/cgroup \ --privileged=true lhrbest/lhrcentos76:8.5 \ /usr/sbin/init docker rm -f lhrmssql102 docker run -d --name lhrmssql102 -h lhrmssql102 \ --net=mssql-network --ip 172.72.9.102 \ -p 61102:1433 \ -v /sys/fs/cgroup:/sys/fs/cgroup \ --privileged=true lhrbest/lhrcentos76:8.5 \ /usr/sbin/init |
安装MSSQL
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 | cat >> /etc/hosts <<"EOF" 172.72.9.100 lhrmssql100 172.72.9.101 lhrmssql101 172.72.9.102 lhrmssql102 EOF # 下载 SQL Server repository curl -o /etc/yum.repos.d/mssql-server.repo https://packages.microsoft.com/config/rhel/7/mssql-server-2017.repo # 下载并安装 SQL Server(211MB) yum install -y mssql-server systemctl status mssql-server # 启动配置(包括sa密码,此处设置为:lhr@xxt123) /opt/mssql/bin/mssql-conf setup # 客户端 curl -o /etc/yum.repos.d/msprod.repo https://packages.microsoft.com/config/rhel/7/prod.repo yum install -y mssql-tools unixODBC-devel mssql-cli echo "export PATH=/opt/mssql-tools/bin/:$PATH" >> /root/.bashrc source /root/.bashrc sqlcmd -S 127.0.0.1 -U SA -P 'lhr@xxt123' exec sp_helpdb; GO |
配置过程:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 | [root@lhrmssql101 /]# /opt/mssql/bin/mssql-conf setup Choose an edition of SQL Server: 1) Evaluation (free, no production use rights, 180-day limit) 2) Developer (free, no production use rights) 3) Express (free) 4) Web (PAID) 5) Standard (PAID) 6) Enterprise (PAID) 7) Enterprise Core (PAID) 8) I bought a license through a retail sales channel and have a product key to enter. Details about editions can be found at https://go.microsoft.com/fwlink/?LinkId=852748&clcid=0x409 Use of PAID editions of this software requires separate licensing through a Microsoft Volume Licensing program. By choosing a PAID edition, you are verifying that you have the appropriate number of licenses in place to install and run this software. Enter your edition(1-8): 6 The license terms for this product can be found in /usr/share/doc/mssql-server or downloaded from: https://go.microsoft.com/fwlink/?LinkId=855862&clcid=0x409 The privacy statement can be viewed at: https://go.microsoft.com/fwlink/?LinkId=853010&clcid=0x409 Do you accept the license terms? [Yes/No]:yes Enter the SQL Server system administrator password: Confirm the SQL Server system administrator password: Configuring SQL Server... The licensing PID was successfully processed. The new edition is [Enterprise Edition]. ForceFlush is enabled for this instance. ForceFlush feature is enabled for log durability. Created symlink from /etc/systemd/system/multi-user.target.wants/mssql-server.service to /usr/lib/systemd/system/mssql-server.service. Setup has completed successfully. SQL Server is now starting. |
二、SQL Server 证书及权限配置
既然没有了域认证,需要配置证书认证,步骤:
- 启用SQL Server Always On Availability Groups功能
- 在所有服务器上为SQL Server Always On Availability Groups安装Linux资源代理
- 在所有Linux服务器启用防火墙上的Always On Availability Group端点端口
- 在主副本上创建数据库主密钥
- 创建将用于加密可用性组端点的证书
- 使用证书为主副本创建Always On Availability Group端点进行身份验证
- 将证书导出到文件
- 将证书文件复制到辅助副本
- 授予证书文件的SQL Server帐户权限
- 在主副本上创建登录账号以供辅助副本使用
- 为登录账号创建用户
- 将步骤5中创建的证书与用户关联
- 授予登录账号的CONNECT权限
1.所有服务器:启用 hadr 功能(Always On Availability Groups)
在托管 SQL Server 实例的每个节点上启用 AlwaysOn 可用性组。 然后重新启动 mssql-server
。 运行以下脚本:
1 2 | /opt/mssql/bin/mssql-conf set hadr.hadrenabled 1 systemctl restart mssql-server |
启用 AlwaysOn_health 事件会话
可选择性地启用 AlwaysOn 可用性组的扩展事件,以便在对可用性组进行故障排除时帮助诊断根本原因。 在每个 SQL Server 实例上运行以下命令:
1 2 | ALTER EVENT SESSION AlwaysOn_health ON SERVER WITH (STARTUP_STATE=ON); GO |
2.所有服务器:为 SQL Server AlwaysOn AG 安装Linux资源代理
群集资源代理程序 mssql-server-ha 是 Pacemaker 和 SQL Server 之间的接口
1 2 | yum install -y mssql-server-ha yum info mssql-server-ha |
结果:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 | [root@lhrmssql100 /]# yum info mssql-server-ha Loaded plugins: fastestmirror, ovl Loading mirror speeds from cached hostfile * base: mirrors.aliyun.com * extras: mirrors.aliyun.com * updates: mirrors.aliyun.com Installed Packages Name : mssql-server-ha Arch : x86_64 Version : 14.0.3421.10 Release : 2 Size : 12 M Repo : installed From repo : packages-microsoft-com-mssql-server-2017 Summary : High Availability support for Microsoft SQL Server Relational Database Engine License : Commercial Description : The mssql-server-ha package contains high availability support for the Microsoft SQL Server Relational Database Engine. |
3.所有服务器:启用防火墙上的 AlwaysOn AG 端点默认端口5022 (或关闭防火墙!)
1 2 3 4 | systemctl status firewalld.service firewall-cmd --zone=public --add-port=5022/tcp --permanent firewall-cmd --reload |
跑脚本完成第4-7步
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 | ---------------------------------------------------------------------- -- #【 172.72.9.100 】 ---------------------------------------------------------------------- USE master GO -- 4. 创建数据库主密钥 -- (数据库主密钥是对称密钥,用于保护数据库中存在的证书和非对称密钥的私钥) CREATE MASTER KEY ENCRYPTION BY PASSWORD = 'Master@123456'; GO -- 5. 创建将用于加密可用性组端点的证书 CREATE CERTIFICATE LINUXHA_SQLAG1_cert WITH SUBJECT = 'server172.72.9.100 certificate for Availability Group' GO -- 6. 主副本:使用证书创建 AlwaysOn AG 端点并进行身份验证 CREATE ENDPOINT Endpoint_AvailabilityGroup STATE = STARTED AS TCP ( LISTENER_PORT = 5022, LISTENER_IP = ALL ) FOR DATABASE_MIRRORING ( AUTHENTICATION = CERTIFICATE LINUXHA_SQLAG1_cert, ENCRYPTION = REQUIRED ALGORITHM AES, ROLE = ALL ); GO -- 7. 主副本:将证书导出到文件 BACKUP CERTIFICATE LINUXHA_SQLAG1_cert TO FILE = '/var/opt/mssql/data/LINUXHA_SQLAG1_cert.cer'; GO ---------------------------------------------------------------------- -- #【 172.72.9.101 】 ---------------------------------------------------------------------- USE master GO CREATE MASTER KEY ENCRYPTION BY PASSWORD = 'Master@123456'; GO CREATE CERTIFICATE LINUXHA_SQLAG2_cert WITH SUBJECT = 'server172.72.9.101 certificate for Availability Group' GO CREATE ENDPOINT Endpoint_AvailabilityGroup STATE = STARTED AS TCP ( LISTENER_PORT = 5022, LISTENER_IP = ALL ) FOR DATABASE_MIRRORING ( AUTHENTICATION = CERTIFICATE LINUXHA_SQLAG2_cert, ENCRYPTION = REQUIRED ALGORITHM AES, ROLE = ALL ); GO BACKUP CERTIFICATE LINUXHA_SQLAG2_cert TO FILE = '/var/opt/mssql/data/LINUXHA_SQLAG2_cert.cer'; GO ---------------------------------------------------------------------- -- 【 server172.72.9.102 】 ---------------------------------------------------------------------- USE master GO CREATE MASTER KEY ENCRYPTION BY PASSWORD = 'Master@123456'; GO CREATE CERTIFICATE LINUXHA_SQLAG3_cert WITH SUBJECT = 'server113 certificate for Availability Group' GO CREATE ENDPOINT Endpoint_AvailabilityGroup STATE = STARTED AS TCP ( LISTENER_PORT = 5022, LISTENER_IP = ALL ) FOR DATABASE_MIRRORING ( AUTHENTICATION = CERTIFICATE LINUXHA_SQLAG3_cert, ENCRYPTION = REQUIRED ALGORITHM AES, ROLE = ALL ); GO BACKUP CERTIFICATE LINUXHA_SQLAG3_cert TO FILE = '/var/opt/mssql/data/LINUXHA_SQLAG3_cert.cer'; GO |
8.各服务器上的证书相互传给其他服务器
1 2 3 4 5 6 7 8 9 10 11 | # lhrmssql100(172.72.9.100) 上执行 scp /var/opt/mssql/data/LINUXHA_SQLAG1_cert.cer root@lhrmssql101:/var/opt/mssql/data/ scp /var/opt/mssql/data/LINUXHA_SQLAG1_cert.cer root@lhrmssql102:/var/opt/mssql/data/ # lhrmssql101(172.72.9.101) 上执行 scp /var/opt/mssql/data/LINUXHA_SQLAG2_cert.cer root@lhrmssql100:/var/opt/mssql/data/ scp /var/opt/mssql/data/LINUXHA_SQLAG2_cert.cer root@lhrmssql102:/var/opt/mssql/data/ # lhrmssql102(172.72.9.102) 上执行 scp /var/opt/mssql/data/LINUXHA_SQLAG3_cert.cer root@lhrmssql100:/var/opt/mssql/data/ scp /var/opt/mssql/data/LINUXHA_SQLAG3_cert.cer root@lhrmssql101:/var/opt/mssql/data/ |
3台查询:
1 2 3 4 | [root@lhrmssql102 /]# ll /var/opt/mssql/data/*.cer -rw-r----- 1 root root 767 Dec 17 10:47 /var/opt/mssql/data/LINUXHA_SQLAG1_cert.cer -rw-r----- 1 root root 767 Dec 17 10:48 /var/opt/mssql/data/LINUXHA_SQLAG2_cert.cer -rw-rw---- 1 mssql mssql 749 Dec 17 10:37 /var/opt/mssql/data/LINUXHA_SQLAG3_cert.cer |
9.所有服务器:授予证书文件所有者权限
1 2 | chown mssql:mssql /var/opt/mssql/data/LINUXHA_SQLAG* ll /var/opt/mssql/data/LINUXHA_SQLAG* |
10.各实例证书还原及账号授权
SSMS 连接到任意一个实例,查询选项启用[SQLCMD模式],批量执行以下脚本
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 | ---------------------------------------------------------------------- --#【 172.72.9.100 】 ---------------------------------------------------------------------- USE master GO -- 11 创建登录账号以供辅助副本使用 CREATE LOGIN login_ag WITH PASSWORD = 'AG@123456'; GO -- 12. 为登录账号创建用户 CREATE USER login_ag FOR LOGIN login_ag GO -- 13. 创建其他副本的证书 CREATE CERTIFICATE LINUXHA_SQLAG2_cert AUTHORIZATION login_ag FROM FILE = '/var/opt/mssql/data/LINUXHA_SQLAG2_cert.cer' GO CREATE CERTIFICATE LINUXHA_SQLAG3_cert AUTHORIZATION login_ag FROM FILE = '/var/opt/mssql/data/LINUXHA_SQLAG3_cert.cer' GO -- 14. 授予登录账号的CONNECT权限 GRANT CONNECT ON ENDPOINT::Endpoint_AvailabilityGroup TO [login_ag]; GO ---------------------------------------------------------------------- --#【 172.72.9.101 副本】 ---------------------------------------------------------------------- USE master GO -- 11 创建登录账号以供辅助副本使用 CREATE LOGIN login_ag WITH PASSWORD = 'AG@123456'; GO -- 12. 为登录账号创建用户 CREATE USER login_ag FOR LOGIN login_ag GO -- 13. 创建其他副本的证书 CREATE CERTIFICATE LINUXHA_SQLAG1_cert AUTHORIZATION login_ag FROM FILE = '/var/opt/mssql/data/LINUXHA_SQLAG1_cert.cer' GO CREATE CERTIFICATE LINUXHA_SQLAG3_cert AUTHORIZATION login_ag FROM FILE = '/var/opt/mssql/data/LINUXHA_SQLAG3_cert.cer' GO -- 14. 授予登录账号的CONNECT权限 GRANT CONNECT ON ENDPOINT::Endpoint_AvailabilityGroup TO [login_ag]; GO ---------------------------------------------------------------------- -- #【 172.72.9.102 副本】 ---------------------------------------------------------------------- USE master GO -- 11 创建登录账号以供辅助副本使用 CREATE LOGIN login_ag WITH PASSWORD = 'AG@123456'; GO -- 12. 为登录账号创建用户 CREATE USER login_ag FOR LOGIN login_ag GO -- 13. 创建其他副本的证书 CREATE CERTIFICATE LINUXHA_SQLAG1_cert AUTHORIZATION login_ag FROM FILE = '/var/opt/mssql/data/LINUXHA_SQLAG1_cert.cer' GO CREATE CERTIFICATE LINUXHA_SQLAG2_cert AUTHORIZATION login_ag FROM FILE = '/var/opt/mssql/data/LINUXHA_SQLAG2_cert.cer' GO -- 14. 授予登录账号的CONNECT权限 GRANT CONNECT ON ENDPOINT::Endpoint_AvailabilityGroup TO [login_ag]; GO |
三、AG配置
现可在 Windows 上使用 SSMS 连接 Linux 上的 SQL Server 配置 AlwaysOn AG。
脚本运行
创建AG
创建具有三个同步副本的 AG,只在lhrmssql100上运行:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 | SELECT * FROM sys.dm_xe_sessions; ALTER ENDPOINT [Endpoint_AvailabilityGroup] STATE = STARTED ALTER EVENT SESSION [AlwaysOn_health] ON SERVER WITH (STARTUP_STATE=ON); ALTER EVENT SESSION [AlwaysOn_health] ON SERVER STATE=START; CREATE AVAILABILITY GROUP [LINUX_SQLAG] WITH (DB_FAILOVER = ON, CLUSTER_TYPE = EXTERNAL, AUTOMATED_BACKUP_PREFERENCE = SECONDARY, DTC_SUPPORT = NONE, REQUIRED_SYNCHRONIZED_SECONDARIES_TO_COMMIT = 0 ) FOR REPLICA ON N'lhrmssql100' WITH ( ENDPOINT_URL = N'tcp://lhrmssql100:5022', AVAILABILITY_MODE = SYNCHRONOUS_COMMIT, FAILOVER_MODE = EXTERNAL, SEEDING_MODE = AUTOMATIC, PRIMARY_ROLE ( ALLOW_CONNECTIONS = ALL ), SECONDARY_ROLE (ALLOW_CONNECTIONS = ALL ) ), N'lhrmssql101' WITH ( ENDPOINT_URL = N'tcp://lhrmssql101:5022', AVAILABILITY_MODE = SYNCHRONOUS_COMMIT, FAILOVER_MODE = EXTERNAL, SEEDING_MODE = AUTOMATIC, PRIMARY_ROLE ( ALLOW_CONNECTIONS = ALL ), SECONDARY_ROLE (ALLOW_CONNECTIONS = ALL ) ), N'lhrmssql102' WITH( ENDPOINT_URL = N'tcp://lhrmssql102:5022', AVAILABILITY_MODE = SYNCHRONOUS_COMMIT, FAILOVER_MODE = EXTERNAL, SEEDING_MODE = AUTOMATIC, PRIMARY_ROLE ( ALLOW_CONNECTIONS = ALL ), SECONDARY_ROLE (ALLOW_CONNECTIONS = ALL ) ); ALTER AVAILABILITY GROUP [LINUX_SQLAG] GRANT CREATE ANY DATABASE; /* CREATE AVAILABILITY GROUP [LINUX_SQLAG] WITH (AUTOMATED_BACKUP_PREFERENCE = SECONDARY, DB_FAILOVER = ON, DTC_SUPPORT = NONE, CLUSTER_TYPE = EXTERNAL, REQUIRED_SYNCHRONIZED_SECONDARIES_TO_COMMIT = 0) FOR DATABASE [Demo] REPLICA ON N'server111' WITH ( ENDPOINT_URL = N'TCP://server111:5022', FAILOVER_MODE = EXTERNAL, AVAILABILITY_MODE = SYNCHRONOUS_COMMIT, BACKUP_PRIORITY = 50, SEEDING_MODE = AUTOMATIC, PRIMARY_ROLE(READ_ONLY_ROUTING_LIST = (N'server112')), SECONDARY_ROLE(READ_ONLY_ROUTING_URL = N'TCP://server111:1433', ALLOW_CONNECTIONS = ALL) ), N'server112' WITH ( ENDPOINT_URL = N'TCP://server112:5022', FAILOVER_MODE = EXTERNAL, AVAILABILITY_MODE = SYNCHRONOUS_COMMIT, BACKUP_PRIORITY = 50, SEEDING_MODE = AUTOMATIC, PRIMARY_ROLE(READ_ONLY_ROUTING_LIST = (N'server111')), SECONDARY_ROLE(READ_ONLY_ROUTING_URL = N'TCP://server112:1433', ALLOW_CONNECTIONS = ALL) ), N'server113' WITH ( ENDPOINT_URL = N'TCP://server113:5022', FAILOVER_MODE = EXTERNAL, AVAILABILITY_MODE = ASYNCHRONOUS_COMMIT, BACKUP_PRIORITY = 50, SEEDING_MODE = AUTOMATIC, PRIMARY_ROLE(READ_ONLY_ROUTING_LIST = ((N'server111',N'server112'),N'server113')), SECONDARY_ROLE(READ_ONLY_ROUTING_URL = N'TCP://server113:1433', ALLOW_CONNECTIONS = ALL) ); GO */ USE [master] GO ALTER AVAILABILITY GROUP [LINUX_SQLAG] ADD LISTENER N'LINUX_LSNAG' ( WITH IP ((N'172.72.9.105', N'255.255.255.0')), PORT=1433 ); GO |
将次要副本联接到 AG
在lhrmssql101和lhrmssql102上运行:
1 2 3 | USE MASTER; ALTER AVAILABILITY GROUP [LINUX_SQLAG] JOIN WITH (CLUSTER_TYPE = EXTERNAL); ALTER AVAILABILITY GROUP [LINUX_SQLAG] GRANT CREATE ANY DATABASE; |
运行完成后:
将数据库添加到可用性组
确保添加到可用性组的数据库处于完全恢复模式,并具有有效的日志备份。 如果是测试数据库或新建的数据库,请执行数据库备份。
只在主服务器上运行:
1 2 3 4 5 6 7 8 | mkdir /bk chown mssql.mssql /bk create database lhrdb; ALTER DATABASE [lhrdb] SET RECOVERY FULL; backup database lhrdb TO DISK = N'/bk/lhrdb_full.bak' with format,stats=5,compression; ALTER AVAILABILITY GROUP [LINUX_SQLAG] ADD DATABASE [lhrdb]; |
验证是否已在辅助服务器上创建了数据库:
1 2 3 | SELECT * FROM sys.databases WHERE name = 'lhrdb'; GO SELECT DB_NAME(database_id) AS 'database', synchronization_state_desc FROM sys.dm_hadr_database_replica_states; |
1 | select * from sys. availability_group_listener_ip_addresses; |
侦听器状态:
图形运行
1. 在节点 lhrmssql100 实例上创建可用性组,名为 LINUX_SQLAG
- Windows Server 故障转移群集:当可用性组托管在属于 Windows Server 故障转移群集的 SQL Server 的实例上时使用,以实现高可用性和灾难恢复。 适用于所有受支持的 SQL Server 版本。
- EXTERNAL :当可用性组托管在由外部群集技术(例如 Linux 上的 Pacemaker)管理的 SQL Server 的实例上时使用,以实现高可用性和灾难恢复。 适用于 SQL Server 2017 (14.x) 及更高版本。
- NONE :当可用性组托管在不由群集技术管理的 SQL Server 的实例上时使用,以实现读取缩放和负载均衡。 适用于 SQL Server 2017 (14.x) 及更高版本。
选择 EXTERNAL,Linux 环境下,创建并配置Always On Availability Group后,将安装和配置Pacemaker。“数据库级别运行状况检测” 勾选此框,为可用性组启用数据库级别运行状况检测 (DB_FAILOVER)。 数据库运行状况检测会说明数据库何时不再处于联机状态、何时出错以及何时触发可用性组的自动故障转移。(更多参考:Always On 可用性组的“指定可用性组选项”页)
2. 选择可用性数据库
先创建数据库,然后添加到可用性组里。
1 2 3 4 5 | chown mssql.mssql /bk create database lhrdb; backup database lhrdb TO DISK = N'/bk/lhrdb_full.bak' with format,stats=5,compression; |
3. 添加副本并设置
可以看到,故障转移模式从以前的“手动” 和“自动”,变为 “外部(External)”。故障转移模式值 External 用于与 Pacemaker 等Linux集群资源管理器一起处理故障转移。
其他选项配置都差不多:
4. 侦听器配置
必须手动将侦听器名称添加为DNS记录: LINUX_LSNAG , IP为 192.168.2.119
创建侦听器:
5. 只读路由配置
6. 选择数据库同步
这里选择 “自动种子设定” ,因为数据库较小。SQL Server 自动为此组中的每个数据库创建次要副本。 自动种子设定要求数据和日志文件路径在参与此组的每个 SQL Server 实例上均相同。(更多参考:“选择初始数据同步”页(AlwaysOn 可用性组向导))
7. 验证及检查
完成后结果:
但此时侦听器的状态是离线的:
虽然 SQL Server Always On Availability Group 安装成功了,但是现在无法进行故障转移