SQL Server AlwaysOn可用性

时间:2017-09-12 12:15:03

标签: sql-server alwayson availability

我有两个服务器SQL Server 2016,我想实现AlwaysOn可用性。

第一台服务器是Read_Write

第二台服务器是Read_Only

我将AlwaysOn可用性配置如下:

CREATE AVAILABILITY GROUP ag
WITH (AUTOMATED_BACKUP_PREFERENCE = SECONDARY,
DB_FAILOVER = OFF,
DTC_SUPPORT = NONE,
FAILURE_CONDITION_LEVEL = 3,
HEALTH_CHECK_TIMEOUT = 30000)
FOR DATABASE DATABASE_NAME
REPLICA ON N'server1' WITH (ENDPOINT_URL = N'TCP://server1_adress:port', FAILOVER_MODE = AUTOMATIC, AVAILABILITY_MODE = SYNCHRONOUS_COMMIT, SESSION_TIMEOUT = 10, BACKUP_PRIORITY = 50, SECONDARY_ROLE (READ_ONLY_ROUTING_URL = N'TCP://server1_adress:port')),
N'server2' WITH (ENDPOINT_URL = N'TCP://server2_adress:5022', FAILOVER_MODE = AUTOMATIC, AVAILABILITY_MODE = SYNCHRONOUS_COMMIT, SESSION_TIMEOUT = 10, BACKUP_PRIORITY = 50, PRIMARY_ROLE(READ_ONLY_ROUTING_LIST=('server1')))
LISTENER N'listener_server' (
WITH IP
((N'ip', N'mask')
)
, PORT=port);

我的问题是所有查询都被路由到第一台服务器。

如何配置AlwaysOn以平衡两个服务器之间的读取查询?

感谢。

1 个答案:

答案 0 :(得分:2)

首先,您必须定义一个特殊的连接字符串,以告诉SQL Server连接到只读副本(称为"应用程序意图"):

Server=tcp:MyAgListener,1433;Database=Db1;IntegratedSecurity=SSPI;ApplicationIntent=ReadOnly;MultiSubnetFailover=True  

在第二步中,如果是次要的,则必须允许对所有节点进行只读访问:

ALTER AVAILABILITY GROUP [ag]
MODIFY REPLICA ON N'server1' WITH (SECONDARY_ROLE(ALLOW_CONNECTIONS = 
READ_ONLY))
GO
ALTER AVAILABILITY GROUP [ag]
MODIFY REPLICA ON N'server2' WITH (SECONDARY_ROLE(ALLOW_CONNECTIONS = 
READ_ONLY))
GO

此外,向server2添加只读路由URL:

ALTER AVAILABILITY GROUP [ag]
MODIFY REPLICA ON N'server2' WITH (SECONDARY_ROLE (READ_ONLY_ROUTING_URL = 
N'TCP://server2_adress:port'));
GO

在下一步中,您必须定义一个路由列表,当特定服务器是主服务器时,该服务器应连接到该路由列表。您已经定义了READ_ONLY_ROUTING_LIST。但只有一台服务器。 server1中断时会发生什么?您应该为每个可能的主节点定义路由列表。

ALTER AVAILABILITY GROUP [ag] 
MODIFY REPLICA ON
N'server1' WITH 
(PRIMARY_ROLE (READ_ONLY_ROUTING_LIST=('server2','server1')));

ALTER AVAILABILITY GROUP [ag] 
MODIFY REPLICA ON
N'server2' WITH 
(PRIMARY_ROLE (READ_ONLY_ROUTING_LIST=('server1','server2')));
GO

对于SQL Server 2016中AlwaysOn的新负载平衡功能,您至少需要第三个只读节点,该节点允许只读连接并配置"嵌套"路由列表:

ALTER AVAILABILITY GROUP ag 
MODIFY REPLICA ON N'server1' WITH (PRIMARY_ROLE (READ_ONLY_ROUTING_LIST=
(('server2', 'server3'), 'server1')));