--==================================================================--该文档主要用于内部配置模板--场景:--主服务器:192.168.3.32--镜像服务器:192.168.3.33--镜像数据库:DemoDB--=================================================================
开始配置
1. 在主服务器和镜像服务器上创建Master Key
--=========================================================================================================--创建Master key--在主服务器和镜像服务器上运行USE master;GOIF NOT EXISTS(SELECT 1 FROM sys.symmetric_keys k WHERE k.Name='##MS_DatabaseMasterKey##')BEGINCREATE MASTER KEY ENCRYPTION BY PASSWord ='master@key123'ENDGOIF NOT EXISTS(SELECT 1 FROM sys.databases db WHERE db.[is_master_key_encrypted_by_server]=1)BEGINALTER MASTER KEY ADD ENCRYPTION BY SERVICE MASTER KEYENDGO
2. 在主服务器上创建证书并备份
--=========================================================================================================--创建和备份证书--在主服务器上运行IF NOT EXISTS (SELECT 1 FROM sys.certificates C WHERE C.Name='HOST_3_32_cert' )BEGINCREATE CERTIFICATE HOST_3_32_cert WITH SUBJECT ='HOST_3_32_cert',START_DATE = '01/01/2010' , EXPIRY_DATE = '01/01/2199';ENDGOBACKUP CERTIFICATE HOST_3_32_cert TO FILE='D:/HOST_3_32_cert.cer'GO
3. 在镜像服务器上创建证书并备份
--=========================================================================================================--创建和备份证书--在镜像服务器上运行IF NOT EXISTS (SELECT 1 FROM sys.certificates C WHERE C.Name='HOST_3_33_cert' )BEGINCREATE CERTIFICATE HOST_3_33_cert WITH SUBJECT ='HOST_3_33_cert',START_DATE = '01/01/2010' , EXPIRY_DATE = '01/01/2199';ENDGOBACKUP CERTIFICATE HOST_3_33_cert TO FILE='D:/HOST_3_33_cert.cer'GO
4. 在主服务器上创建镜像端点
--=========================================================================================================--创建镜像专用的端点,并使用证书加密--同一个实例上只能存在一个镜像端点--在主服务器上运行IF NOT EXISTS(SELECT * FROM sys.endpoints e WHERE e.[Type]=4)BEGINCREATE ENDPOINT [Endpoint_Mirroring]STATE=STARTED ASTCP(LISTENER_PORT=5022)FOR DATABASE_MIRRORING(AUTHENTICATION=CERTIFICATE HOST_3_32_cert, ENCRYPTION=REQUIRED,ROLE=ALL)ENDGO
5. 在镜像服务器上创建镜像端点
--=========================================================================================================--创建镜像专用的端点,并使用证书加密--同一个实例上只能存在一个镜像端点--在主服务器上运行IF NOT EXISTS(SELECT * FROM sys.endpoints e WHERE e.[Type]=4)BEGINCREATE ENDPOINT [Endpoint_Mirroring]STATE=STARTED ASTCP(LISTENER_PORT=5022)FOR DATABASE_MIRRORING(AUTHENTICATION=CERTIFICATE HOST_3_33_cert, ENCRYPTION=REQUIRED,ROLE=ALL)ENDGO
6. 将主服务器和镜像服务器上备份的证书相互复制到对方服务器上
7. 在主服务器上创建登陆和用户以及证书,并授予连接镜像端点的权限
-----------------------------------------------------------------------在主服务器上创建登陆和用户以及证书,并授予连接镜像端点的权限--在主服务器上运行USE master;GOIF NOT EXISTS(SELECT 1 FROM sys.syslogins l WHERE l.[Name] ='HOST_3_33_login')BEGINCREATE LOGIN HOST_3_33_login WITH PASSWORD ='Auto@sql'ENDGOIF NOT EXISTS(SELECT 1 FROM sys.sysusers u WHERE u.[Name]= 'HOST_3_33_User')BEGINCREATE USER HOST_3_33_User FOR LOGIN HOST_3_33_loginENDGOIF NOT EXISTS(SELECT 1 FROM sys.certificates c WHERE c.[Name]= 'HOST_3_33_cert')BEGINCREATE CERTIFICATE HOST_3_33_cert AUTHORIZATION HOST_3_33_User FROM FILE='D:/HOST_3_33_cert.cer'ENDGOGRANT CONNECT ON ENDPOINT::[Endpoint_Mirroring] TO HOST_3_33_loginGO
8.在镜像服务器上创建登陆和用户以及证书,并授予连接镜像端点的权限
-----------------------------------------------------------------------在镜像服务器上创建登陆和用户以及证书,并授予连接镜像端点的权限--在镜像服务器上运行USE master;GOIF NOT EXISTS(SELECT 1 FROM sys.syslogins l WHERE l.[Name] ='HOST_3_33_login')BEGINCREATE LOGIN HOST_3_33_login WITH PASSWORD ='Auto@sql'ENDGOIF NOT EXISTS(SELECT 1 FROM sys.sysusers u WHERE u.[Name]= 'HOST_3_33_User')BEGINCREATE USER HOST_3_33_User FOR LOGIN HOST_3_33_loginENDGOIF NOT EXISTS(SELECT 1 FROM sys.certificates c WHERE c.[Name]= 'HOST_3_33_cert')BEGINCREATE CERTIFICATE HOST_3_33_cert AUTHORIZATION HOST_3_33_User FROM FILE='D:/HOST_3_33_cert.cer'ENDGOGRANT CONNECT ON ENDPOINT::[Endpoint_Mirroring] TO HOST_3_33_loginGO
9. 在主服务器和镜像服务器上配置防火墙,开放5022端口
10. 在镜像服务器上使用Telent来检查端口情况
telnet 192.168.3.33 5022
11. 在主服务器上使用Telent来检查端口情况
telnet 192.168.3.32 5022
12. 在主服务器上备份数据库和日志
--数据库完整备份和日志备份BACKUP DATABASE DemoDB TO DISK='DemoDB_FULL.BAK' WITH COMPRESSIONBACKUP LOG DemoDB TO DISK='DemoDB_Log.BAK' WITH COMPRESSION
13. 将主服务器上的备份复制到镜像服务器上,使用WITH NORECOVERY选项还原数据库备份和日志备份/
--使用数据库完整备份和日志备份还原--注意修改还原目录RESTORE DATABASE DemoDB FROM DISK='DemoDB_FULL.BAK' WITH NORECOVERYRESTORE LOG DemoDB FROM DISK='DemoDB_Log.BAK' WITH NORECOVERY
14. 在镜像服务器上配置伙伴
--------------------------------------------------------------------从服务器设置镜像USE [master]GOALTER DATABASE DemoDB SET PARTNER='TCP://192.168.3.32:5022'GO
15.在主服务器上配置伙伴
--------------------------------------------------------------------主服务器设置镜像--如果提示日志不足,请继续备份还原日志USE [master]GOALTER DATABASE DemoDB SET PARTNER='TCP://192.168.3.33:5022'GO
16. 如果希望日志运行在异步模式下,修改同步模式
--------------------------------------------------------------------在主服务器上运行USE [master]GOALTER DATABASE DemoDB SET PARTNER SAFETY OFFGO
配置结束后,可以使用镜像监视器来查看
--===================================================================
惯例妹子镇邪
新闻热点
疑难解答