日期:2014-05-16  浏览次数:20423 次

sqlserver 2005 数据库双机热备(灾备)

????? 最近因为一个项目需要,要对数据库进行双机热备,不知道从何下手,于是上网找了很多的资料,然后经过多次尝试,最终成功,以下是实现过程中的总结。

????? 具体的环境如下:

????? 数据库是SqlServer2005,准备三台服务器做热备,分别是a、b、c,其中a是主服务器,b是镜像服务器,c是见证服务器

????? 一、硬件条件:

????? 1.a、b、c三台计算机都必须在一个局域网内

??????2.修改a、b、c三台服务器的计算机名,具体操作如下:我的电脑-->计算机名,点击“更改”后进入到“计算机名称更改”,点击“其它”进入“DNS后缀和NetBIOS计算机名”,输入com,点击“确定”保证(此时会提示重启计算机,不过稍后再启动,因为还有其他文件要修改)。

????? 3.将三台计算机的ip+完整计算机名分别拷贝到主服务器、镜像服务器、见证服务器,具体格式如下:ip+空格+完整计算机名+enter,修改该文件必须是以管理员身份登录系统才可以。修改文件路径是:C:\Windows\System32\drivers\etc下的hosts文件

??????4.?重启三台计算机

?

????? 二、软件条件

????? 1.只有企业版、标准版、开发版才可以建立数据库镜像,其他版本即Express只能作为见证服务器,查看版本语句

? ??? select @@version(其实本人也不知道了解这几种版本之间的具体差别)

??????2.若要对数据库进行数据库镜像,必须将它更改为使用完整恢复模式,具体修改语句为

????? use master;
??????alter database <databasename> set recovery full

????? 3.如果数据库没有打过sp2补丁,要先打补丁,不然后面语句无法成功执行。如果你补丁,的服务器系统是windows2008的,就需要打sp3补丁,这些补丁官网上都有。

?

????? 三、具体步骤:

????? 1.创建证书

?????? --主服务器
???????use master;
???????create master key encryption by password='killkill';
???????create certificate host_a_cert with subject='host_a certificate',start_date='09/02/2012';
???????--镜像服务器
???????use master;
???????create master key encryption by password='killkill';
???????create certificate host_b_cert with subject='host_b certificate',start_date='09/02/2012';
???????--见证服务器
???????use master;
???????create master key encryption by password='killkill';
???????create certificate host_c_cert with subject='host_c certificate',start_date='09/02/2012';

?

?????? 2.创建连接的端点

?????? --主服务器
???????create endpoint endpoint_mirroring
???????state=started
???????as
???????tcp(listener_port=5022,listener_ip=all)
???????for
?????? database_mirroring
???????(authentication=certificate host_a_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_b_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_c_cert,encryption=required algorithm aes,role=all)

?

?????? 注:创建端点的时候可能报错,可能是需要打sp2补丁(也许是其他问题)

?

????? 3.备份证书以备建立互联
???????--主服务器
???????backup certificate host_a_cert to file ='d:\sqlbackup\host_a_cert.cer';
???????--镜像服务器
???????backup certificate host_b_cert to file ='d:\sqlbackup\host_b_cert.cer';
???????--见证服务器
???????backup certificate host_c_cert to file ='d:\sqlbackup\host_c_cert.cer';

?

????? 4.互换证书
???????将主服务器和见证服务器上的备份证书拷贝到镜像服务器的d:\sqlbackup下;
???????将镜像服务器和见证服务器上的备份证书拷贝到主服务器的d:\sqlbackup下;
???????将主服务器和镜像服务器上的备份证书拷贝到见证服务器的d:\sqlbackup下。

?

????? 5.添加登录名、用户
???????--主服务器
???????create login host_b_login with password='killkill';
???????create user host_b_user for login host_b_login;
???????create certificate host_b_cert authorization host_b_user from file='d:\sqlbackup\host_b_cert.cer';
???????grant connect on endpoint::endpoint_mirroring to [host_b_login]
????????
???????create login host_c_login with password='killkill';
???????create user host_c_user for login host_c_login;
???????create certificate host_c_cert authorization host_c_user from file='d:\sqlbackup\host_c_cert.cer';
???????grant connect on endpoint::endpoint_mirroring to [host_c_login]
????????
???????--镜像服务器
???????create login host_a_login with password='killkill';
???????create user host_a_user for login host_a_login;
???????create certificate host_a_cert authorization host_a_use