1. 配制环境
OS:Win7 DB:SQL Server R2
2. 基本配制
2.1, 开启sqlServer服务如下 图1
图1
2.2, 开启sqlServer的tcp/ip协议,在Start -> program -> sqlServerR2 -> ConfigTool -> ConfigManagement,如下图2
图2
注:开启此服务后,需要重启sqlServer服务才能生效。
2.3, 在需要做备份的机器(主、从和可选的见证)上建立相同的登录用户名密码。
2.4, 打开镜像用到的5022端口。在win窗口中输入控制面板\系统和安全\Windows 防火墙,在弹出的窗口中点击[高级设置],在入站和出站 规则中加入5022端口。
2.5, 开启数据库RemoteDacEnabled属性。右键点击[数据库实例] - > 选择方面,在弹出的窗口中选择[外围应用配制器] 把RemoteDacEnabled属性设置为true。如图3
图3
2.6, 在三台机器上建立同一个登录名和密码
点击数据库实例的下面的安全性,右键点击新建登录名,在三台机器上建相同的登录名和密码,主要是为了应用程序在连接数据中使用。
3.备份还原数据库
将主服务器中的数据库完全备份与日志备份到同一目录同一文件中。右键[数据库名称] -> 点击[任务],选择备份,先做完全备份,再做日志备份。在备库中还原主库的备份内容。右键[数据库名称] ->点击[任务],选择还原。如图4
4. 开始热备
实现互通可以使用域或证书来实现,考虑实现的简单,以下选取证书的方式实现。注意:实现“主备数据库实例互通”的操作只需要做一次,例如为了将两个 SQL Server 2008的实例中的3个数据库建成镜像关系,则只需要做一次以下操作就可以了;或者这样理解:每一对主备实例(不是数据库)做一次互通。
4.1、创建证书(主备可并行执行)
--主机执行:
USE master; CREATE MASTER KEY ENCRYPTION BY PASSWORD = 'masterWord'; CREATE CERTIFICATE HOST_pri_cert WITH SUBJECT = 'HOST_primary certificate' , START_DATE = '01/11/2011', EXPIRY_DATE = '01/11/2013';
--备机执行:
USE master; CREATE MASTER KEY ENCRYPTION BY PASSWORD ='masterWord'; CREATE CERTIFICATE HOST_min_cert WITH SUBJECT ='HOST_minor certificate' , START_DATE = '01/11/2011', EXPIRY_DATE = '01/11/2013';
--见证服务器执行
USE master; CREATE MASTER KEY ENCRYPTION BY PASSWORD ='masterWord'; CREATE CERTIFICATE HOST_pro_cert WITH SUBJECT = 'HOST_Witness certificate' , START_DATE = '01/11/2011', EXPIRY_DATE = '01/11/2013';
4.2, 创建连接的端点(主备可并行执行)
--主机执行:
CREATE ENDPOINT Endpoint_Mirroring
STATE = STARTED
AS
TCP ( LISTENER_PORT=5022 , LISTENER_IP = ALL )
FOR
DATABASE_MIRRORING
( AUTHENTICATION = CERTIFICATE HOST_pri_cert , ENCRYPTION = REQUIRED ALGORITHM AES , ROLE = ALL );
--备机执行:
CREATE ENDPOINT Endpoint_Mirroring
STATE = STARTED
AS
TCP ( LISTENER_PORT=5022 , LISTENER_IP = ALL )
FOR
DATABASE_MIRRORING
( AUTHENTICATION = CERTIFICATE HOST_min_cert , ENCRYPTION = REQUIRED ALGORITHM AES , ROLE = ALL )
--见证服务器上执行
CREATE ENDPOINT Endpoint_Mirroring
STATE = STARTED
AS
TCP ( LISTENER_PORT=5022 , LISTENER_IP = ALL )
FOR
DATABASE_MIRRORING
( AUTHENTICATION = CERTIFICATE HOST_pro_cert , ENCRYPTION = REQUIRED ALGORITHM AES , ROLE = ALL )
4.3, 备份证书以备建立互联(主备可并行执行)
--主机执行:
BACKUP CERTIFICATE HOST_pri_cert TO FILE = 'D:\HOST_pri_cert.cer';
--备机执行:
BACKUP CERTIFICATE HOST_min_cert TO FILE = 'D:\HOST_min_cert.cer';
--见证服务器上执行
BACKUP CERTIFICATE HOST_pro_cert TO FILE = 'D:\HOST_pro_cert.cer';
4.4, 互换证书
将备份到D:\的证书进行互换,即把HOST_pri_cert.cer,HOST_pro_cer.cert复制到备机的D:\,把HOST_min_cert.cer, HOST_pro_cer.cert复制到主机的D:\,把HOST_pri_cert.cer,HOST_min_cert.cer复制到见证机的D:\
4.5, 添加登陆名、用户(主备可并行执行)
--主机执行:
CREATE LOGIN HOST_min_login WITH PASSWORD ='masterWord';
CREATE USER HOST_min_user FOR LOGIN HOST_min_login;
CREATE CERTIFICATE HOST_min_cert AUTHORIZATION HOST_min_user FROM FILE ='D:\HOST_min_cert.cer';
GRANT CONNECT ON ENDPOINT::Endpoint_Mirroring TO [HOST_min_login];
CREATE LOGIN HOST_pro_login WITH PASSWORD ='masterWord';
CREATE USER HOST_pro_user FOR LOGIN HOST_pro_login;
CREATE CERTIFICATE HOST_pro_cert AUTHORIZATION HOST_pro_user FROM FILE ='D:\HOST_pro_cert.cer';
GRANT CONNECT ON ENDPOINT::Endpoint_Mirroring TO [HOST_pro_login];
--备机执行:
CREATE LOGIN HOST_pri_login WITH PASSWORD ='masterWord';
CREATE USER HOST_pri_user FOR LOGIN HOST_pri_login;
CREATE CERTIFICATE HOST_pri_cert AUTHORIZATION HOST_pri_user FROM FILE ='D:\HOST_pri_cert.cer';
GRANT CONNECT ON ENDPOINT::Endpoint_Mirroring TO [HOST_pri_login];
CREATE LOGIN HOST_pro_login WITH PASSWORD ='masterWord';
CREATE USER HOST_pro_user FOR LOGIN HOST_pro_login;
CREATE CERTIFICATE HOST_pro_cert AUTHORIZATION HOST_pro_user FROM FILE ='D:\HOST_pro_cert.cer';
GRANT CONNECT ON ENDPOINT::Endpoint_Mirroring TO [HOST_pro_login];
--见证服务器上执行
CREATE LOGIN HOST_min_login WITH PASSWORD ='masterWord';
CREATE USER HOST_min_user FOR LOGIN HOST_min_login;
CREATE CERTIFICATE HOST_min_cert AUTHORIZATION HOST_min_user FROM FILE ='D:\HOST_min_cert.cer';
GRANT CONNECT ON ENDPOINT::Endpoint_Mirroring TO [HOST_min_login];
CREATE LOGIN HOST_pri_login WITH PASSWORD ='masterWord';
CREATE USER HOST_pri_user FOR LOGIN HOST_pri_login;
CREATE CERTIFICATE HOST_pri_cert AUTHORIZATION HOST_pri_user FROM FILE ='D:\HOST_pri_cert.cer';
GRANT CONNECT ON ENDPOINT::Endpoint_Mirroring TO [HOST_pri_login];
4.6、 配制镜像
右键点击需要镜像的数据库,点击[任务] ->选择[镜像],点击【配置安全性】,如下图:
会出现一个配置数据库安全性的向导
然后依次点击下一步,由于是在三台机器上做镜像,因此镜像端口都按默认的5022,如果在一台机器上做,需要使用不同的端口号
由于使用的是证书的方式,不是使用域名的方式,因此下图中的服务账户名都为空
继续下一步
点击完成,然后点击开始镜像就完成了数据库镜像的设置
5. 测试
6. 参考
http://www.cnblogs.com/killkill/archive/2008/05/23/1205792.html
http://msdn.microsoft.com/zh-cn/library/ms187798.aspx
http://liulike.blog.51cto.com/1355103/339183/