很多时候,我们面临的问题是镜像与身份验证设置,所以在本文中,我们通过建立终端身份验证和授权所需的证书来配置数据库镜像。 按照以下步骤设置数据库与SQL Server镜像 一个主服务器和 一个镜像服务器 。本文不包括 witness服务器。所以如果你想自动故障切换设置请
阅读这篇文章,为角色 witness服务器
第1步.设置 end-points 和证书
有了第一步,我们将两个服务器上创建端点和配置身份验证和授权证书。
创建证书和end-points(主实例服务器):
use master;
GO
CREATE MASTER KEY ENCRYPTION BY PASSWORD = 'Secure_Password';
GO
CREATE CERTIFICATE principal_cert WITH SUBJECT = 'Principal Server Certificate', START_DATE = '2013/01/01', EXPIRY_DATE = '2020/01/01';
GO
CREATE ENDPOINT Mirroring STATE = STARTED
AS TCP (LISTENER_PORT = 5022, LISTENER_IP = ALL)
FOR DATABASE_MIRRORING ( AUTHENTICATION = CERTIFICATE principal_cert, ENCRYPTION = disabled, ROLE = ALL);
GO
BACKUP CERTIFICATE principal_cert TO FILE = 'D:certificatesprincipal_cert.cer'
GO
创建证书和end-points(在镜像实例服务器):
USE master
GO
CREATE MASTER KEY ENCRYPTION BY PASSWORD = 'Secure_Password';
GO
CREATE CERTIFICATE mirror_cert WITH SUBJECT = 'Mirror Server Certificate', START_DATE = '2013/01/01', EXPIRY_DATE = '2020/01/01';
GO
CREATE ENDPOINT Mirroring STATE = STARTED
AS TCP (LISTENER_PORT = 5023, LISTENER_IP = ALL)
FOR DATABASE_MIRRORING ( AUTHENTICATION = CERTIFICATE mirror_cert, ENCRYPTION = disabled, ROLE = ALL );
GO
BACKUP CERTIFICATE mirror_cert
TO FILE = 'D:certificatesmirror_cert.cer';
GO
创建用户和证书(主实例):
在这个阶段,我们需要交换证书到其他实例。镜像服务器和镜像实例证书,主体服务器上复制主体实例证书。
USE MASTER
GO
CREATE LOGIN mirror_login WITH PASSWORD = 'Secure_Password';
GO
CREATE USER mirror_user FOR LOGIN mirror_login;
GO
CREATE CERTIFICATE mirror_cert
AUTHORIZATION mirror_user
FROM FILE = 'D:certificatemirror_cert.cer';
GO
GRANT CONNECT ON ENDPOINT::Mirroring TO [mirror_login];
GO
创建用户和证书(在镜像实例):
USE MASTER
GO
CREATE LOGIN principal_login WITH PASSWORD = 'Secure_Password';
go
CREATE USER principal_user FROM LOGIN principal_login;
go
CREATE CERTIFICATE principal_cert
AUTHORIZATION Principal_user
FROM FILE = 'D:certificateprincipal_cert.cer';
Go
GRANT CONNECT ON Endpoint::Mirroring TO [Principal_Login];
go
第2步.镜像服务器上复制数据库
配置身份验证后,请按照下面的屏幕截图给出的步骤。
主要更新数据库完全恢复模式:
ALTER DATABASE MirrorDBName SET RECOVERY FULL;
GO
转储主体服务器上数据库:
USE MASTER
GO
BACKUP DATABASE MirrorDBName
TO DISK = 'D:BackupsMirrorDBName_full.bak'
GO
主体服务器上转储登录:
USE MASTER
GO
BACKUP LOG MirrorDBName
TO DISK = 'D:BackupsMirrorDBName_log.trn'
GO
以备份后复制数据库的备份和手动登录备份到镜像服务器。然后进入下一步骤之后。
镜像服务器上还原备份无恢复选项 :
USE MASTER
GO
RESTORE DATABASE MirrorDBName
FROM DISK = 'D:BackupsMirrorDBName_full.bak'
WITH NORECOVERY
GO
镜像服务器上还原日志无恢复选项 :
USE MASTER
GO
RESTORE LOG MirrorDBName
FROM DISK = 'D:BackupsMirrorDBName_log.trn'
WITH NORECOVERY
GO
第3步.启用数据库在SQL Server镜像服务器
在完成第1步和2之后,现在我们就可以启用使用下面的命令数据库之间的镜像。
主数据库实例执行此。
ALTER DATABASE MirrorDBName
SET PARTNER = 'TCP://MIRROR-INSTANCE:5023'
GO
在镜像数据库实例上执行。
ALTER DATABASE MirrorDBName
SET PARTNER = 'TCP://PRINCIPAL-INSTANCE:5022'
GO