--
--6.5.3节示例
--
--在A数据库创建证书
USE master;
GO
CREATE MASTER KEY ENCRYPTION BY PASSWORD = 'your password’ --创建主密钥
GO
CREATE CERTIFICATE MIR_A_cert --创建证书
WITH SUBJECT = 'MIR_A certificate for database mirroring',
start_date = '01/01/2008',
EXPIRY_DATE = '10/31/2099' ;
GO
--使用证书为A数据库创建镜像端点
USE master;
GO
CREATE ENDPOINT Endpoint_Mirroring --镜像端点的名字
STATE = STARTED
AS TCP (
LISTENER_PORT=5024 --镜像通讯中所使用的端口
, LISTENER_IP = ALL --允许所有IP
)
FOR DATABASE_MIRRORING (
AUTHENTICATION = CERTIFICATE MIR_A_cert --使用了第1步创建的证书
, ENCRYPTION = REQUIRED ALGORITHM RC4
, ROLE = ALL
);
--备份A服务器上创建的证书
USE master;
GO
BACKUP CERTIFICATE MIR_A_cert --备份证书
TO FILE = 'C:\MIR_A_cert.cer';
--为B服务器创建证书、镜像端点并备份证书
USE master;
GO
CREATE MASTER KEY ENCRYPTION BY PASSWORD = 'your password’ --创建主密钥
GO
CREATE CERTIFICATE MIR_B_cert --创建证书
WITH SUBJECT = 'MIR_B certificate for database mirroring',
start_date = '01/01/2008',
EXPIRY_DATE = '10/31/2099' ;
GO
CREATE ENDPOINT Endpoint_Mirroring --镜像端点的名字
STATE = STARTED
AS TCP (
LISTENER_PORT=5024 --镜像通讯中所使用的端口
, LISTENER_IP = ALL --允许所有IP
)
FOR DATABASE_MIRRORING (
AUTHENTICATION = CERTIFICATE MIR_B_cert --使用前面创建的证书
, ENCRYPTION = REQUIRED ALGORITHM RC4
, ROLE = ALL
);
GO
BACKUP CERTIFICATE MIR_B_cert
TO FILE = 'C:\MIR_B_cert.cer';
--在A服务器上创建用于连接B服务器的登录名和用户
USE master;
GO
CREATE LOGIN MIR_B_login WITH PASSWORD = 'your password'; --创建登录名
GO
CREATE USER MIR_B_user FOR LOGIN MIR_B_login; --创建用户
--在A服务器上还原证书
CREATE CERTIFICATE MIR_B_cert
AUTHORIZATION MIR_B_user --将用户账号与证书关联
FROM FILE = 'C:\MIR_B_cert.cer' --B服务器上备份过来的证书
--在B服务器上创建登录名、用户并还原证书、授予权限
USE master;
GO
CREATE LOGIN MIR_A_login WITH PASSWORD = 'your password';
GO
CREATE USER MIR_A_user FOR LOGIN MIR_A_login
GO
CREATE CERTIFICATE MIR_A_cert --还原证书
AUTHORIZATION MIR_A_user
FROM FILE = 'C:\MIR_A_cert.cer';
GO
GRANT CONNECT ON ENDPOINT::Endpoint_Mirroring TO MIR_A_login
--备份A服务器上的数据库并在B服务器还原
--A服务器
BACKUP DATABASE Credit --备份
TO DISK = 'C:\Credit.bak'
--B服务器
RESTORE DATABASE Credit --还原
FROM DISK = 'C:\Credit.bak '
WITH NORECOVERY
--配置镜像伙伴
--B服务器
ALTER DATABASE Credit
SET PARTNER = 'TCP://ms-zy:5024'; --设置镜像伙伴
--A服务器
ALTER DATABASE Credit
SET PARTNER = 'TCP://ms-zy2:5024'; --设置镜像伙伴
--镜像角色互换
USE master;
ALTER DATABASE [Credit]
SET PARTNER FAILOVER --角色互换