β

Configuring Always On Availability Groups in SQL S

Channel [K] 26 阅读

在之前的预览版中,Public preview of the next release of SQL Server on Linux,Always On Availability Groups还是不支持的功能,但是在最新的SQL Server 2017 on Linux中,该功能已经引入。

准备测试环境的服务器

在Always On AG中如果需要自动Failover至少需要集群中有3台服务器,但是我只是测试功能,因此只使用了两台服务器。并且本文不涉及任何Pacemaker的设置,完全是数据库层面的AG配置。

我使用的是Google Compute Engine的2台VM,最低配的1vCPU,3.75GB内存。

如果要通过远程客户端配置SQL Server,则需要在VPC network的Firewall rules中将1433端口开放,如果是在虚拟机本地的sqlcmd中操作,则无需配置。

操作系统:CentOS7

$ cat /etc/centos-release
CentOS Linux release 7.4.1708 (Core)

在/etc/hosts中配置双方服务器的名称和IP地址的解析,以保证两台机器可以通过服务器名称互相访问。

重要!服务器主机的hostname必须少于等于15个字符,否则在配置过程会出现各种莫名其妙的权限报错。

安装SQL Server

SQL Server for Linux的安装非常简单,可以参考我之前的这篇文章。

Public preview of the next release of SQL Server on Linux

启用AlwaysOn AG功能

执行范围:在所有机器上执行

安装完的SQL Server,默认是没有启用AlwaysOn AG功能的,需要手工开启,开启的方法很简单。开启该功能需要重启数据库实例。

sudo /opt/mssql/bin/mssql-conf set hadr.hadrenabled  1
sudo systemctl restart mssql-server

启用AlwaysOn_health事件

执行范围:在所有机器上执行

这一步不是必须的。

ALTER EVENT SESSION  AlwaysOn_health ON SERVER WITH (STARTUP_STATE=ON);

创建数据库复制的用户

执行范围:在所有机器上执行

CREATE LOGIN dbm_login WITH PASSWORD = ‘YourPassword’;
CREATE USER dbm_user FOR LOGIN dbm_login;

创建认证

执行范围:在Primary Replica机器上执行

CREATE MASTER KEY ENCRYPTION BY PASSWORD = 'YourPassword';
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 = 'YourPassword'
   );

将生成的dbm_certificate.cer和dbm_certificate.pvk文件scp到另外一台服务器的相同位置并修改属主,这台服务器就是Secondary Replica。

cd /var/opt/mssql/data
chown mssql:mssql dbm_certificate.*

然后在这台服务器上导入认证。

执行范围:在Secondary Replica机器上执行

CREATE MASTER KEY ENCRYPTION BY PASSWORD = 'YourPassword';
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 = 'YourPassword'
);

创建数据库复制的Endpoint

执行范围:在所有机器上执行

CREATE ENDPOINT [Hadr_endpoint]
AS TCP (LISTENER_PORT = 5022)
FOR DATA_MIRRORING (
ROLE = ALL,
AUTHENTICATION = CERTIFICATE dbm_certificate,
ENCRYPTION = REQUIRED ALGORITHM AES
);
ALTER ENDPOINT [Hadr_endpoint] STATE = STARTED;
use master
GRANT CONNECT ON ENDPOINT::[Hadr_endpoint] TO [dbm_login];

创建Availability Groups

执行范围:在Primary Replica机器上执行

CREATE AVAILABILITY GROUP [ag1]
   WITH (CLUSTER_TYPE = EXTERNAL)
   FOR REPLICA ON
   N'centos1' WITH (
  ENDPOINT_URL = N'tcp://centos1:5022',
  AVAILABILITY_MODE = SYNCHRONOUS_COMMIT,
  FAILOVER_MODE = EXTERNAL,
  SEEDING_MODE = AUTOMATIC
   ),
   N'centos2' WITH (
  ENDPOINT_URL = N'tcp://centos2:5022',
  AVAILABILITY_MODE = SYNCHRONOUS_COMMIT,
  FAILOVER_MODE = EXTERNAL,
  SEEDING_MODE = AUTOMATIC
   );
ALTER AVAILABILITY GROUP [ag1] GRANT CREATE ANY DATABASE;

在主库上创建了AG之后,备库需要加入AG。

执行范围:在Secondary Replica机器上执行

ALTER AVAILABILITY GROUP [ag1] JOIN WITH (CLUSTER_TYPE = EXTERNAL);
ALTER AVAILABILITY GROUP [ag1] GRANT CREATE ANY DATABASE;

将数据库加入AG

这里新建一个数据库db1,将它加入到ag1中。由于上面设置的SEEDING_MODE参数为AUTOMATIC,因此这个db1数据库将会在备库实例中自动创建,后续对于该库进行的任何操作也会自动复制到备库中。

执行范围:在Primary Replica机器上执行

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];

允许Secondary Replica可以被只读访问

在以上的创建过程中创建出来的AG中的备库是不允许被访问的,如果要访问将会遇到以下错误。

The target database, ‘db1’, is participating in an availability group and is currently not accessible for queries. Either data movement is suspended or the availability replica is not enabled for read access. To allow read-only access to this and other databases in the availability group, enable read access to one or more secondary availability replicas in the group. For more information, see the ALTER AVAILABILITY GROUP statement in SQL Server Books Online.

执行范围:在Primary Replica机器上执行,立刻生效。

use master
ALTER AVAILABILITY GROUP ag1
   MODIFY REPLICA ON
   N'centos2' WITH (
  SECONDARY_ROLE (ALLOW_CONNECTIONS = ALL )
   );

测试

在主库中随便创建一张新表,再插入几条记录。

> use db1
> select * into t_test from sys.databases;
> insert into t_test select * from t_test;
> GO
Changed database context to 'db1'.
(5 rows affected)
(5 rows affected)

在备库中查询,这张表已经复制成功。

> use db1
> select count(*) from t_test;
> GO
Changed database context to 'db1'.
-------

(1 rows affected)

如果在备库中尝试更新数据,将会遇到以下错误。

> delete from t_test;
> GO
Msg 3906, Level 16, State 2, Server centos2, Line 1
Failed to update database "db1" because the database is read-only.

监控AG状态

通过以下这些视图可以监控AG中各个部分的状态。

group的监控

select * from sys.availability_groups;
select * from sys.availability_groups_cluster;
select * from sys.dm_hadr_availability_group_states;

replica的监控

select * from sys.availability_replicas;
select * from sys.dm_hadr_availability_replica_states;
select * from sys.dm_hadr_availability_replica_cluster_nodes;
select * from sys.dm_hadr_availability_replica_cluster_states;

在AG中的database的监控

select * from sys.availability_databases_cluster;
select * from sys.dm_hadr_database_replica_states;
select * from sys.dm_hadr_database_replica_cluster_states;
select name,database_id,replica_id,group_database_id from sys.databases;

参考文档

本文配置步骤的参考文档为:

https://docs.microsoft.com/en-us/sql/linux/sql-server-linux-availability-group-configure-ha?view=sql-server-linux-2017

Share

Related posts:

  1. My little poor server
  2. SQL Server 2008 VS. Oracle Database 11g
  3. How to change I/O scheduler in Linux
作者:Channel [K]
面朝大海,春暖花开
原文地址:Configuring Always On Availability Groups in SQL S, 感谢原作者分享。

发表评论