--场景:

--有服务器SQLNode11,SQLNODE21,SQLNODE31三台在同一故障转移群集SQLNode01中

--的数据库服务器,安装SQL SERVER 2012 并配置启动alwayson高可用性组,服务使

--用localsystem运行,为SQLNode11上数据库DB1和DB2创建高可用性组,并将

--SQLNode21和SQLNode31加入到可用性组中。

--实现步骤概述:在各个节点上创建证书和端点并备份证书,使用备份依次在其他

--两个节点上创建登陆用户,在各个节点创建证书并为将连接权限赋予登陆用户,

--激活alwayson回话服务,在主节点SQLNode11上备份数据库并使用WITH NORECOVERY

--在从节点SQLNode21和SQLNode31上还原数据库,在主节点SQLNode11创建高可用性

--组,在从节点SQLNode21和SQLNode31上加入到可用性组,并在从节点SQLNode21

--和SQLNode31上将数据库添加到可用组中。

--1. 在节点SQLNode11创建和备份证书和创建镜像端点

USE master;

USE master;

GO

--=========================================================================================================

--创建Master key

IF NOT EXISTS(SELECT 1 FROM sys.symmetric_keys k WHERE k.Name='##MS_DatabaseMasterKey##')

BEGIN

CREATE MASTER KEY ENCRYPTION BY PASSWORD ='Master@key'

END

GO

IF NOT EXISTS(SELECT 1 FROM sys.databases db WHERE db.[is_master_key_encrypted_by_server]=1)

BEGIN

ALTER MASTER KEY ADD ENCRYPTION BY SERVICE MASTER KEY

END

GO

--=========================================================================================================

--创建和备份证书

IF NOT EXISTS (SELECT 1 FROM sys.certificates C WHERE C.Name='ServerCert_SQLNode11' )

BEGIN

CREATE CERTIFICATE ServerCert_SQLNode11 WITH SUBJECT ='ServerCert_SQLNode11'

END

GO

BACKUP CERTIFICATE ServerCert_SQLNode11 TO FILE='D:\ServerCert_SQLNode11.cer'

GO

--=========================================================================================================

--创建镜像专用的端点,并使用证书加密

--同一个实例上只能存在一个镜像端点

IF NOT EXISTS(SELECT * FROM sys.endpoints e WHERE e.[Type]=4)

BEGIN

CREATE ENDPOINT DBMirrorEndPoint

STATE=STARTED AS

TCP(LISTENER_PORT=5022)

FOR DATABASE_MIRRORING(AUTHENTICATION=CERTIFICATE ServerCert_SQLNode11, ENCRYPTION=REQUIRED,ROLE=ALL)

END

GO

--2. 在节点SQLNode21创建和备份证书和创建镜像端点

USE master;

GO

--=========================================================================================================

--创建Master key

IF NOT EXISTS(SELECT 1 FROM sys.symmetric_keys k WHERE k.Name='##MS_DatabaseMasterKey##')

BEGIN

CREATE MASTER KEY ENCRYPTION BY PASSWORD ='Master@key'

END

GO

IF NOT EXISTS(SELECT 1 FROM sys.databases db WHERE db.[is_master_key_encrypted_by_server]=1)

BEGIN

ALTER MASTER KEY ADD ENCRYPTION BY SERVICE MASTER KEY

END

GO

--=========================================================================================================

--创建和备份证书

IF NOT EXISTS (SELECT 1 FROM sys.certificates C WHERE C.Name='ServerCert_SQLNode21' )

BEGIN

CREATE CERTIFICATE ServerCert_SQLNode21 WITH SUBJECT ='ServerCert_SQLNode21'

END

GO

BACKUP CERTIFICATE ServerCert_SQLNode21 TO FILE='D:\ServerCert_SQLNode21.cer'

GO

--=========================================================================================================

--创建镜像专用的端点,并使用证书加密

--同一个实例上只能存在一个镜像端点

IF NOT EXISTS(SELECT * FROM sys.endpoints e WHERE e.[Type]=4)

BEGIN

CREATE ENDPOINT DBMirrorEndPoint

STATE=STARTED AS

TCP(LISTENER_PORT=5022)

FOR DATABASE_MIRRORING(AUTHENTICATION=CERTIFICATE ServerCert_SQLNode21, ENCRYPTION=REQUIRED,ROLE=ALL)

END

GO

--3. 在节点SQLNode31创建和备份证书和创建镜像端点

USE master;

GO

--=========================================================================================================

--创建Master key

IF NOT EXISTS(SELECT 1 FROM sys.symmetric_keys k WHERE k.Name='##MS_DatabaseMasterKey##')

BEGIN

CREATE MASTER KEY ENCRYPTION BY PASSWORD ='Master@key'

END

GO

IF NOT EXISTS(SELECT 1 FROM sys.databases db WHERE db.[is_master_key_encrypted_by_server]=1)

BEGIN

ALTER MASTER KEY ADD ENCRYPTION BY SERVICE MASTER KEY

END

GO

--=========================================================================================================

--创建和备份证书

IF NOT EXISTS (SELECT 1 FROM sys.certificates C WHERE C.Name='ServerCert_SQLNode31' )

BEGIN

CREATE CERTIFICATE ServerCert_SQLNode31 WITH SUBJECT ='ServerCert_SQLNode31'

END

GO

BACKUP CERTIFICATE ServerCert_SQLNode31 TO FILE='D:\ServerCert_SQLNode31.cer'

GO

--=========================================================================================================

--创建镜像专用的端点,并使用证书加密

--同一个实例上只能存在一个镜像端点

IF NOT EXISTS(SELECT * FROM sys.endpoints e WHERE e.[Type]=4)

BEGIN

CREATE ENDPOINT DBMirrorEndPoint

STATE=STARTED AS

TCP(LISTENER_PORT=5022)

FOR DATABASE_MIRRORING(AUTHENTICATION=CERTIFICATE ServerCert_SQLNode31, ENCRYPTION=REQUIRED,ROLE=ALL)

END

GO

--4. 在SQLNode11上创建登陆和用户,并赋予连接端点权限

USE master;

GO

--=========================================================================================================

--创建登陆和用户,并赋予连接端点权限

IF NOT EXISTS(SELECT 1 FROM sys.syslogins l WHERE l.[Name] ='ServerLogin_SQLNode21')

BEGIN

CREATE LOGIN ServerLogin_SQLNode21 WITH PASSWORD ='Auto@sql'

END

GO

IF NOT EXISTS(SELECT 1 FROM sys.sysusers u WHERE u.[Name]= 'ServerUser_SQLNode21')

BEGIN

CREATE USER ServerUser_SQLNode21 FOR LOGIN ServerLogin_SQLNode21

END

GO

IF NOT EXISTS(SELECT 1 FROM sys.certificates c WHERE c.[Name]= 'ServerCert_SQLNode21')

BEGIN

CREATE CERTIFICATE ServerCert_SQLNode21 AUTHORIZATION ServerUser_SQLNode21 FROM FILE='D:\ServerCert_SQLNode21.cer'

END

GO

GRANT CONNECT ON ENDPOINT::DBMirrorEndPoint TO ServerLogin_SQLNode21

GO

USE master;

GO

--=========================================================================================================

--创建登陆和用户,并赋予连接端点权限

IF NOT EXISTS(SELECT 1 FROM sys.syslogins l WHERE l.[Name] ='ServerLogin_SQLNode31')

BEGIN

CREATE LOGIN ServerLogin_SQLNode31 WITH PASSWORD ='Auto@sql'

END

GO

IF NOT EXISTS(SELECT 1 FROM sys.sysusers u WHERE u.[Name]= 'ServerUser_SQLNode31')

BEGIN

CREATE USER ServerUser_SQLNode31 FOR LOGIN ServerLogin_SQLNode31

END

GO

IF NOT EXISTS(SELECT 1 FROM sys.certificates c WHERE c.[Name]= 'ServerCert_SQLNode31')

BEGIN

CREATE CERTIFICATE ServerCert_SQLNode31 AUTHORIZATION ServerUser_SQLNode31 FROM FILE='D:\ServerCert_SQLNode31.cer'

END

GO

GRANT CONNECT ON ENDPOINT::DBMirrorEndPoint TO ServerLogin_SQLNode31

GO

--5. 在SQLNode21上创建登陆和用户,并赋予连接端点权限

USE master;

GO

--=========================================================================================================

--创建登陆和用户,并赋予连接端点权限

IF NOT EXISTS(SELECT 1 FROM sys.syslogins l WHERE l.[Name] ='ServerLogin_SQLNode11')

BEGIN

CREATE LOGIN ServerLogin_SQLNode11 WITH PASSWORD ='Auto@sql'

END

GO

IF NOT EXISTS(SELECT 1 FROM sys.sysusers u WHERE u.[Name]= 'ServerUser_SQLNode11')

BEGIN

CREATE USER ServerUser_SQLNode11 FOR LOGIN ServerLogin_SQLNode11

END

GO

IF NOT EXISTS(SELECT 1 FROM sys.certificates c WHERE c.[Name]= 'ServerCert_SQLNode11')

BEGIN

CREATE CERTIFICATE ServerCert_SQLNode11 AUTHORIZATION ServerUser_SQLNode11 FROM FILE='D:\ServerCert_SQLNode11.cer'

END

GO

GRANT CONNECT ON ENDPOINT::DBMirrorEndPoint TO ServerLogin_SQLNode11

GO

USE master;

GO

--=========================================================================================================

--创建登陆和用户,并赋予连接端点权限

IF NOT EXISTS(SELECT 1 FROM sys.syslogins l WHERE l.[Name] ='ServerLogin_SQLNode31')

BEGIN

CREATE LOGIN ServerLogin_SQLNode31 WITH PASSWORD ='Auto@sql'

END

GO

IF NOT EXISTS(SELECT 1 FROM sys.sysusers u WHERE u.[Name]= 'ServerUser_SQLNode31')

BEGIN

CREATE USER ServerUser_SQLNode31 FOR LOGIN ServerLogin_SQLNode31

END

GO

IF NOT EXISTS(SELECT 1 FROM sys.certificates c WHERE c.[Name]= 'ServerCert_SQLNode31')

BEGIN

CREATE CERTIFICATE ServerCert_SQLNode31 AUTHORIZATION ServerUser_SQLNode31 FROM FILE='D:\ServerCert_SQLNode31.cer'

END

GO

GRANT CONNECT ON ENDPOINT::DBMirrorEndPoint TO ServerLogin_SQLNode31

GO

--6. 在SQLNode31上创建登陆和用户,并赋予连接端点权限

USE master;

GO

--=========================================================================================================

--创建登陆和用户,并赋予连接端点权限

IF NOT EXISTS(SELECT 1 FROM sys.syslogins l WHERE l.[Name] ='ServerLogin_SQLNode11')

BEGIN

CREATE LOGIN ServerLogin_SQLNode11 WITH PASSWORD ='Auto@sql'

END

GO

IF NOT EXISTS(SELECT 1 FROM sys.sysusers u WHERE u.[Name]= 'ServerUser_SQLNode11')

BEGIN

CREATE USER ServerUser_SQLNode11 FOR LOGIN ServerLogin_SQLNode11

END

GO

IF NOT EXISTS(SELECT 1 FROM sys.certificates c WHERE c.[Name]= 'ServerCert_SQLNode11')

BEGIN

CREATE CERTIFICATE ServerCert_SQLNode11 AUTHORIZATION ServerUser_SQLNode11 FROM FILE='D:\ServerCert_SQLNode11.cer'

END

GO

GRANT CONNECT ON ENDPOINT::DBMirrorEndPoint TO ServerLogin_SQLNode11

GO

USE master;

GO

--=========================================================================================================

--创建登陆和用户,并赋予连接端点权限

IF NOT EXISTS(SELECT 1 FROM sys.syslogins l WHERE l.[Name] ='ServerLogin_SQLNode21')

BEGIN

CREATE LOGIN ServerLogin_SQLNode21 WITH PASSWORD ='Auto@sql'

END

GO

IF NOT EXISTS(SELECT 1 FROM sys.sysusers u WHERE u.[Name]= 'ServerUser_SQLNode21')

BEGIN

CREATE USER ServerUser_SQLNode21 FOR LOGIN ServerLogin_SQLNode21

END

GO

IF NOT EXISTS(SELECT 1 FROM sys.certificates c WHERE c.[Name]= 'ServerCert_SQLNode21')

BEGIN

CREATE CERTIFICATE ServerCert_SQLNode21 AUTHORIZATION ServerUser_SQLNode21 FROM FILE='D:\ServerCert_SQLNode21.cer'

END

GO

GRANT CONNECT ON ENDPOINT::DBMirrorEndPoint TO ServerLogin_SQLNode21

GO

--7. 在SQLNode11上激活alwayson回话

USE master;

GO

IF EXISTS(SELECT * FROM sys.server_event_sessions WHERE name='AlwaysOn_health')

BEGIN

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

END

IF NOT EXISTS(SELECT * FROM sys.dm_xe_sessions WHERE name='AlwaysOn_health')

BEGIN

ALTER EVENT SESSION [AlwaysOn_health] ON SERVER STATE=START;

END

GO

--8. 在书在SQLNode21上激活alwayson回话

USE master;

GO

IF EXISTS(SELECT * FROM sys.server_event_sessions WHERE name='AlwaysOn_health')

BEGIN

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

END

IF NOT EXISTS(SELECT * FROM sys.dm_xe_sessions WHERE name='AlwaysOn_health')

BEGIN

ALTER EVENT SESSION [AlwaysOn_health] ON SERVER STATE=START;

END

GO

--9. 在SQLNode31上激活alwayson回话

USE master;

GO

IF EXISTS(SELECT * FROM sys.server_event_sessions WHERE name='AlwaysOn_health')

BEGIN

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

END

IF NOT EXISTS(SELECT * FROM sys.dm_xe_sessions WHERE name='AlwaysOn_health')

BEGIN

ALTER EVENT SESSION [AlwaysOn_health] ON SERVER STATE=START;

END

GO

--10.   在SQLNode11上备份数据库

USE master;

GO

BACKUP DATABASE [DB1] TO  DISK = N'D:\DB1_F1.bak' WITH NOFORMAT, NOINIT,  NAME = N'DB1-完整数据库备份', SKIP, NOREWIND, NOUNLOAD,  STATS = 10

GO

BACKUP DATABASE [DB2] TO  DISK = N'D:\DB2_F1.bak' WITH NOFORMAT, NOINIT,  NAME = N'DB2-完整数据库备份', SKIP, NOREWIND, NOUNLOAD,  STATS = 10

GO

--11.   在SQLNode11上创建可用性组

USE [master]

GO

CREATE AVAILABILITY GROUP [SQLNode01_AG001]

WITH (AUTOMATED_BACKUP_PREFERENCE = SECONDARY)

FOR DATABASE [DB1], [DB2]

REPLICA ON N'SQLNODE11' WITH

(

ENDPOINT_URL = N'TCP://SQLNode11.DCDemo.com:5022',

FAILOVER_MODE = AUTOMATIC, AVAILABILITY_MODE = SYNCHRONOUS_COMMIT,

BACKUP_PRIORITY = 50,

SECONDARY_ROLE(ALLOW_CONNECTIONS = NO)

),

N'SQLNODE21' WITH

(

ENDPOINT_URL = N'TCP://SqlNode21.DCDemo.com:5022',

FAILOVER_MODE = AUTOMATIC,

AVAILABILITY_MODE = SYNCHRONOUS_COMMIT,

BACKUP_PRIORITY = 50,

SECONDARY_ROLE(ALLOW_CONNECTIONS = ALL)

),

N'SQLNODE31' WITH

(

ENDPOINT_URL = N'TCP://SqlNode31.DCDemo.com:5022',

FAILOVER_MODE = MANUAL,

AVAILABILITY_MODE = SYNCHRONOUS_COMMIT,

BACKUP_PRIORITY = 50,

SECONDARY_ROLE(ALLOW_CONNECTIONS = READ_ONLY)

);

GO

--12.   在SQLNode21和SQLNode31上加入可用性组

USE master;

GO

ALTER AVAILABILITY GROUP  [SQLNode01_AG001] JOIN;

GO

--13.   将数据库备份拷贝到SQLNode21和SQLNode31上并还原数据库

USE master;

GO

RESTORE DATABASE [DB1] FROM  DISK = N'D:\DB1_F1.bak' WITH  NORECOVERY,  NOUNLOAD,  STATS = 5

GO

RESTORE DATABASE [DB2] FROM  DISK = N'D:\DB2_F1.bak' WITH  NORECOVERY,  NOUNLOAD,  STATS = 5

GO

--14.   在SQLNode11上备份日志

USE master;

GO

BACKUP LOG [DB1] TO  DISK = N'D:\DB1_L1.trn' WITH NOFORMAT, NOINIT, NOSKIP, REWIND, NOUNLOAD, COMPRESSION,  STATS = 5

GO

BACKUP LOG [DB2] TO  DISK = N'D:\DB2_L1.trn' WITH NOFORMAT, NOINIT, NOSKIP, REWIND, NOUNLOAD, COMPRESSION,  STATS = 5

GO

--15.   在SQLNode21和SQLNOde31上还原日志备份

USE master;

GO

RESTORE LOG [DB1] FROM  DISK = N'D:\DB1_L1.trn' WITH  NORECOVERY,  NOUNLOAD,  STATS = 5

GO

GO

RESTORE LOG [DB2] FROM  DISK = N'D:\DB2_L1.trn' WITH  NORECOVERY,  NOUNLOAD,  STATS = 5

GO

--16.   在SQLNode21和SQLNOde31上将DB添加到可用性组

USE master;

GO

ALTER DATABASE [DB1] SET HADR AVAILABILITY GROUP = [SQLNode01_AG001];

GO

ALTER DATABASE [DB2] SET HADR AVAILABILITY GROUP = [SQLNode01_AG001];

GO

最新文章

  1. JavaScript之函数
  2. IOS - 响应者链条
  3. 图像边缘检测——Sobel算子
  4. Codeforces Round #337 (Div. 2) A. Pasha and Stick 数学
  5. Android之View方法
  6. HBase配置&启动脚本分析
  7. 区间dp-zoj3541-The Last Puzzle
  8. MyBatis和SpringMVC集成事务在Junit测试下有效但是在实际项目无效的问题
  9. [hdu4694]Important Sisters
  10. Apigee 简介与简单试用
  11. Maven私服(Nexus)资源上传下载
  12. C#对DataTable里数据筛选排序的方法
  13. python openpyxl 2.5.4 版本 excel常用操作封装
  14. web接口测试中需要测试的几个点
  15. 02 - Unit010:关联映射
  16. 安装python-empy
  17. vue测试安装和配置
  18. 【NOIP】提高组2012 同余方程
  19. iOS UIPrintInteractionController打印
  20. Laravel 开源电商体验与部署

热门文章

  1. 用Python写单向链表和双向链表
  2. 解决selenium不支持firefox低版本的问题
  3. Idea多个module下maven的pom.xml失效的问题
  4. cdoj第13th校赛初赛H - Hug the princess
  5. 使用select实现多并发的socket的功能
  6. centos7下创建mysql5.6多实例
  7. 现代编译原理--第二章(语法分析之LR(1))
  8. 配置 Mysql 支持远程访问 并取消域名解析以提高链接速度
  9. 如何使用webpack打包前端项目
  10. linux磁盘管理(RHEL)