Sql server版本:Microsoft SQL Server 2019 (CTP2.4) - 15.0.1400.75 (X64) Mar 16 2019 11:53:26 Copyright (C) 2019 Microsoft Corporation Enterprise Edition (64-bit) on Linux (CentOS Linux 7 (Core)) <X64>
1 激活Always On Availability Group功能Bash(all):
sudo /opt/mssql/bin/mssql-conf set hadr.hadrenabled 1
sudo systemctl restart mssql-server
1.1所有服务器:为 SQL Server AlwaysOn AG 安装Linux资源代理 群集资源代理程序 mssql-server-ha 是 Pacemaker 和 SQL Server 之间的接口 Bash(all) yum install -y mssql-server-hayum info mssql-server-ha
2 启用AlwaysOn_health事件会话Sql(all):
ALTER EVENT SESSION AlwaysOn_health ON SERVER WITH (STARTUP_STATE=ON);
GO
3 在所有节点上创建书库镜像端点(endPoint)用户(linux上需要创建一个登录名(login)为dbm_login和用户名(user)为dbm_user的用户,它专用于端点,注意使用强密码)
Sql(all):
CREATE LOGIN dbm_login WITH PASSWORD = \'1111.aaa\';
CREATE USER dbm_user FOR LOGIN dbm_login;
4 在主节点创建证书Sql(主):
CREATE MASTER KEY ENCRYPTION BY PASSWORD = \'1111.aaa\';
CREATE CERTIFICATE dbm_certificate WITH SUBJECT = \'dbm\';
BACKUP CERTIFICATE dbm_certificate
TO FILE = \'/var/opt/mssql/data/dbm_certificate.cer\'
WITH PRIVATE KEY (
FILE = \'/var/opt/mssql/data/dbm_certificate.pvk\',
ENCRYPTION BY PASSWORD = \'1111.aaa\'
);
4.1 此时,在主上面的数据目录下会产生两个文件:Bash(主):
dbm_certificate.cer ##证书
dbm_certificate.pvk ##私钥
4.2 然后将这两个文件复制到所有从服务器上相同目录中Bash(主):
cd /var/opt/mssql/data/
scp dbm_certificate.* 192.168.1.192:/var/opt/mssql/data/
scp dbm_certificate.* 192.168.1.193:/var/opt/mssql/data/
4.3 对从服务器上的证书进行授权Bash(从);
cd /var/opt/mssql/data/
chown mssql.mssql dbm_certificate.*
5 在辅助节点上创建证书Sql(从):
CREATE MASTER KEY ENCRYPTION BY PASSWORD = \'1111.aaa\';
CREATE CERTIFICATE dbm_certificate
AUTHORIZATION dbm_user
FROM FILE = \'/var/opt/mssql/data/dbm_certificate.cer\'
WITH PRIVATE KEY (
FILE = \'/var/opt/mssql/data/dbm_certificate.pvk\',
DECRYPTION BY PASSWORD = \'1111.aaa\'
);
6 创建数据库镜像端点Sql(all);
CREATE ENDPOINT [Hadr_endpoint]
AS TCP (LISTENER_PORT = 5022)
FOR DATABASE_MIRRORING (
ROLE = ALL,
AUTHENTICATION = CERTIFICATE dbm_certificate,
ENCRYPTION = REQUIRED ALGORITHM AES
);
ALTER ENDPOINT [Hadr_endpoint] STATE = STARTED;
GRANT CONNECT ON ENDPOINT::[Hadr_endpoint] TO [dbm_login];
执行完毕之后在(all)上面查看是否启用了对应的端口并开放端口5022
sudo firewall-cmd --zone=public --add-port=5022/tcp --permanent
sudo firewall-cmd --reload
7 在主节点上创建AG 7.1 检查sqlserver主机名成Sql(all);
select @@SERVERNAME;
正确的话会显示:
不正确的话用命令进行修改;
8.1.1 select * from Sys.SysServers; //查看所有系统名称
8.1.2 sp_dropserver \'localhost\' //删除servername-localhost
8.1.3 sp_addserver \'m192\',\'LOCAL\' //修改主机名为m190、
修改完毕之后一定要重启sqlserver服务才会生效。
7.2 在主节点上创建AGSql(主):
CREATE AVAILABILITY GROUP [AG1]
WITH (DB_FAILOVER = ON, CLUSTER_TYPE = EXTERNAL)
FOR REPLICA ON
N\'m191\'
WITH (
ENDPOINT_URL = N\'tcp://m192:5022\',
AVAILABILITY_MODE = SYNCHRONOUS_COMMIT,
FAILOVER_MODE = EXTERNAL,
SEEDING_MODE = AUTOMATIC,
SECONDARY_ROLE (ALLOW_CONNECTIONS = ALL)
),
N\'m192\'
WITH (
ENDPOINT_URL = N\'tcp://m192:5022\',
AVAILABILITY_MODE = SYNCHRONOUS_COMMIT,
FAILOVER_MODE = EXTERNAL,
SEEDING_MODE = AUTOMATIC,
SECONDARY_ROLE (ALLOW_CONNECTIONS = ALL)
),
N\'m193\'
WITH (
ENDPOINT_URL = N\'tcp://m193:5022\',
AVAILABILITY_MODE = SYNCHRONOUS_COMMIT,
FAILOVER_MODE = EXTERNAL,
SEEDING_MODE = AUTOMATIC,
SECONDARY_ROLE (ALLOW_CONNECTIONS = ALL)
);
ALTER AVAILABILITY GROUP [AG1] GRANT CREATE ANY DATABASE;
7.3 将从节点添加进去Sql(从):
ALTER AVAILABILITY GROUP [AG1] JOIN WITH (CLUSTER_TYPE = EXTERNAL);
ALTER AVAILABILITY GROUP [AG1] GRANT CREATE ANY DATABASE
7.4 测试创建数据库db1Sql(主):
CREATE DATABASE [db1];
ALTER DATABASE [db1] SET RECOVERY FULL;
BACKUP DATABASE [db1]
TO DISK = N\'/var/opt/mssql/data/db1.bak\';
ALTER AVAILABILITY GROUP [AG1] ADD DATABASE [db1];
7.5 在从节点上面再次点击db1,发现可以点开