这两天做SQL Server镜像,碰到很多问题,在网上看了不少文章,终于配置完成,总结一下:
引用文章:http://www.cnblogs.com/mrhgw/p/3514855.html (Sqlserver2008R2配置数据库镜像之我的经验总结 )
a) 主体备份,镜像还原。 这里有一个疑问,镜像还原后,一直显示“正在还原”,访问不了,这个可以不用管它
-- 主机备份
USE master
GO
BACKUP DATABASE [TestSync] TO DISK = N'D:\SqlserverMirrorCer\TestSync.bak'
WITH FORMAT, INIT, NAME = N'TestSync-Full Database Backup', SKIP, NOREWIND, NOUNLOAD, STATS = 10;
GO
BACKUP LOG [TestSync] TO DISK = N'D:\SqlserverMirrorCer\TestSync.bak'
WITH NOFORMAT, NOINIT, NAME = N'TestSync-Transaction Log Backup', SKIP, NOREWIND, NOUNLOAD, STATS = 10;
GO
-- 镜像恢复
USE master
GO
RESTORE DATABASE [TestSync] FROM DISK = N'D:\SqlserverMirrorCer\TestSync.bak'
WITH FILE = 1,
NORECOVERY, NOUNLOAD, REPLACE, STATS = 10
GO
RESTORE LOG [TestSync] FROM DISK = N'D:\SqlserverMirrorCer\TestSync.bak'
WITH FILE = 2, NORECOVERY, NOUNLOAD, STATS = 10
GO
b)因为是不同域,需要用证书进行身份认证,证书创建后需要把主机的证书传到镜像,把镜像的证书传到主体,以备下一步的创建身份认证
------------------------------------------------------------------------
--============================ 主机上执行 ============================--
------------------------------------------------------------------------
USE master
GO
--创建证书,并备份
IF EXISTS(SELECT * FROM sys.databases WHERE name='master' and is_master_key_encrypted_by_server=1)
OPEN MASTER KEY DECRYPTION BY PASSWORD='PWD_DBMirror986252588';
ELSE
CREATE MASTER KEY ENCRYPTION BY PASSWORD='PWD_DBMirror986252588';
GO
IF EXISTS(select * from sys.certificates WHERE name='Cert_Host')
DROP CERTIFICATE Cert_Host;
GO
CREATE CERTIFICATE Cert_Host
WITH SUBJECT=N'Cert_Host Certificate',START_DATE='20120405',EXPIRY_DATE='20990405';
BACKUP CERTIFICATE Cert_Host TO FILE=N'D:\SqlserverMirrorCer\Cert_Host.cer';
GO
--创建镜像端口
IF EXISTS(select * from sys.database_mirroring_endpoints WHERE name='Endpoint_Host')
DROP ENDPOINT Endpoint_Host
GO
CREATE ENDPOINT Endpoint_Host
STATE = STARTED
AS TCP
(
LISTENER_PORT=5022,
LISTENER_IP=ALL
)
FOR DATABASE_MIRRORING
(
AUTHENTICATION=CERTIFICATE Cert_Host,
ENCRYPTION=REQUIRED ALGORITHM AES,
ROLE=PARTNER
)
GO
------------------------------------------------------------------------
--============================ 镜像机上执行 ============================--
------------------------------------------------------------------------
USE master
GO
--创建证书,并备份
IF EXISTS(SELECT * FROM sys.databases WHERE name='master' and is_master_key_encrypted_by_server=1)
OPEN MASTER KEY DECRYPTION BY PASSWORD='PWD_DBMirror986252588';
ELSE
CREATE MASTER KEY ENCRYPTION BY PASSWORD='PWD_DBMirror986252588';
GO
IF EXISTS(select * from sys.certificates WHERE name='Cert_Mirror')
DROP CERTIFICATE Cert_Mirror;
GO
CREATE CERTIFICATE Cert_Mirror
WITH SUBJECT=N'Cert_Mirror Certificate',START_DATE='20120405',EXPIRY_DATE='20990405';
BACKUP CERTIFICATE Cert_Mirror TO FILE=N'D:\SqlserverMirrorCer\Cert_Mirror.cer';
GO
--创建镜像端口
IF EXISTS(select * from sys.database_mirroring_endpoints WHERE name='Endpoint_Mirror')
DROP ENDPOINT Endpoint_Mirror
GO
CREATE ENDPOINT Endpoint_Mirror
STATE = STARTED
AS TCP
(
LISTENER_PORT=5022,
LISTENER_IP=ALL
)
FOR DATABASE_MIRRORING
(
AUTHENTICATION=CERTIFICATE Cert_Mirror,
ENCRYPTION=REQUIRED ALGORITHM AES,
ROLE=PARTNER
)
GO
c) 创建身份认证,让主机和镜像可以互通
------------------------------------------------------------------------
--============================ 主机上执行 ============================--
------------------------------------------------------------------------
USE master
GO
--为镜像机访问主机的镜像端口而创建登录和用户,并授予连接权限
CREATE LOGIN Login_For_Mirror WITH PASSWORD=N'PWD_DBMirror986252588';
CREATE USER User_For_Mirror FOR LOGIN Login_For_Mirror;
CREATE CERTIFICATE Cert_For_Mirror AUTHORIZATION User_For_Mirror FROM FILE=N'D:\SqlserverMirrorCer\Cert_Mirror.cer';
GRANT CONNECT ON ENDPOINT::Endpoint_Host TO Login_For_Mirror;
GO
------------------------------------------------------------------------
--============================ 镜像机上执行 ============================--
------------------------------------------------------------------------
USE master
GO
--为主机访问镜像机的镜像端口而创建登录和用户,并授予连接权限
CREATE LOGIN Login_For_Host WITH PASSWORD=N'PWD_DBMirror986252588';
CREATE USER User_For_Host FOR LOGIN Login_For_Host;
CREATE CERTIFICATE Cert_For_Host AUTHORIZATION User_For_Host FROM FILE =N'D:\SqlserverMirrorCer\Cert_Host.cer';
GRANT CONNECT ON ENDPOINT::Endpoint_Mirror TO Login_For_Host;
GO
d)开始设置主从关系,上面的执行没有顺序关系,但是这里须要依次执行。先执行的为镜像,后执行的为主体
-- 镜像机上执行:
-- 建立 主机 合作
ALTER DATABASE [TestSync] SET PARTNER =N'TCP://10.100.0.33:5022';
-- 主机上执行:
-- 建立 镜像机 合作
ALTER DATABASE [TestSync] SET PARTNER=N'TCP://10.2.0.24:5022';