一 概述
数据库镜像是sql server 2005用于提高数据库可用性的新技术。数据库镜像将事务日志记录直接从一台服务器传输到另一台服务器,并且能够在出现故障时快速转移到备用服务器。可以编写客户端程序自动重定向连接信息,这样一旦出现故障转移就可以自动连接到备用服务器和数据库。
优势:数据库镜像可以在不丢失已提交数据的前提下进行快速故障转移,无须专门的硬件,并且易于配置和管理。
二 环境准备
操作系统:window 2003 enterprise sp2(至少两台,如要启用自动故障转移,必需三台)
sql版本:mssql server 2005 sp3
检查sql server版本:
exec xp_msver
select serverproperty(‘productlevel’)
数据库准备:准备一个数据库:ccerp_jzt ,备份此数据库还原到另外一台机器上,另外一台必须是with no recovery
这里我假设服务器a,b,c
a为主体服务器,b为镜像服务器,c为见证服务器
a服务器
use master
go
restore filelistonly from disk=n’f:\databak\ccerp_jzt_backup_200911250100.bak’
restore database ccerp_jzt from disk=n’f:\databak\ccerp_jzt_backup_200911250100.bak’ with replace,recovery,
move ‘ccerp_ydswzip_data’ to ‘d:\data\ccerp_jzt.mdf’,
move ‘ccerp_ydswzip_log’ to ‘d:\data\ccerp_jzt_log.ldf’
exec sp_helpdb ‘ccerp_jzt’
backup database ccerp_jzt to disk =n’f:\databak\sk.bak’ with init
–更改恢复模式
alter database ccerp_jzt set recovery full
b服务器:
create database ccerp_jzt
on
( name = sales_dat,
filename = ‘d:\data\ccerp_jzt.mdf’,
size = 10
)
log on
( name = ‘ccerp_jzt_log’,
filename = ‘d:\data\ccerp_jzt_log.ldf’,
size = 5mb
)
go
restore filelistonly from disk=n’f:\xxzx\data\sk.bak’
use master
go
restore database ccerp_jzt from disk=n’f:\xxzx\data\sk.bak’ with replace,norecovery,
exec sp_helpdb ‘ccerp_jzt’
c服务器只要装上sql server 2005就可以,无需其他准备
准备完成后如下图所示:
三 三种模式的搭建
数据库镜像要建立必需得建立信任关系,那么在win环境下建立信任关系可以通过三种方式:域帐户,证书信任,windows 匿名登陆,现就前两种模式做配置说明.
3.1 域帐户模式:
3.1.1 更改mssqlserver服务的的登陆方式为域帐户登陆方式:
进入windows服务管理控制台,更改服务登陆帐户,使域账户有更改mssql server服务状态的权限.三台机器都做同样设置
将域帐户赋予sysadmin角色
3.1.2 建立端点:
通过图形界面建立端点:
启动sqlwb,按图一直下一步
用域帐户登陆
如果成功则:
3.2 证书模式
3.2.1建立证书&端点
参与数据库镜像会话的服务器必须彼此信任。对于本地通信而言,例如一个域内的通信,信任意味着sql server实例登陆账号必须有权限连接到其他镜像服务器,也包括endpoints。首先在每个服务器上使用create login命令,然后使用grant connect on endpoint命令.非信任域之间的通信必须使用证书。如果使用create certificate语句创建自签名的证书,基本上所有数据镜像证书的要求都可以满足。确认在create certificate语句中将证书标记为active for begin_dialog。
一 建立证书:
镜像服务器上执行:
use master;
create master key encryption by password = ‘test’;
create certificate host_a_cert with subject=’host_a certificate’, start_date=’2010-03-10′;
主体服务器上执行:
use master;
create master key encryption by password = ‘test’;
create certificate host_b_cert with subject=’host_b certificate’, start_date=’2010-03-10′;
见证服务器上执行:
use master;
create master key encryption by password = ‘test’;
create certificate host_c_cert with subject=’host_c certificate’, start_date=’2010-03-10′;
二 建立端点:
镜像服务器上执行:
–create mirror endpoint on primary a
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 on mirror server b
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 on witness server c
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 = witness );
select * from sys.database_mirroring_endpoints;
证书互备:
镜像服务器上执行:
–backup certificate
backup certificate host_a_cert to file = ‘e:\host_a_cert.cer’
主体服务器上执行
–backup certificate
backup certificate host_b_cert to file = ‘e:\host_b_cert.cer’
见证服务器上执行:
backup certificate host_c_cert to file = ‘e:\host_c_cert.cer’
将备份到的证书进行互换,即host_a_cert.cer复制到b机的e:\ 将host_b_cert.cer复制到a机的e:\,也就是每台服务器有三个证书
三:建立登陆用户:
镜像服务器上执行:
–create user
create login host_b_login with password = ‘test’;
create user host_b_user for login host_b_login;
create certificate host_b_cert authorization host_b_user from file = ‘e:\host_b_cert.cer’;
grant connect on endpoint::endpoint_mirroring to [host_b_login];
create login host_c_login with password = ‘test’;
create user host_c_user for login host_c_login;
create certificate host_c_cert authorization host_c_user from file = ‘e:\host_c_cert.cer’;
grant connect on endpoint::endpoint_mirroring to [host_c_login];
grant connect on endpoint::endpoint_mirroring to [host_a_login];
–query user sid
select loginname,name,sid from syslogins
主体服务器上执行:
–create user
create login host_a_login with password = ‘test’;
create user host_a_user for login host_a_login;
create certificate host_a_cert authorization host_a_user from file = ‘e:\host_a_cert.cer’;
grant connect on endpoint::endpoint_mirroring to [host_a_login];
— add witness user
create login host_c_login with password = ‘test’;
create user host_c_user for login host_c_login;
create certificate host_c_cert authorization host_c_user from file = ‘e:\host_c_cert.cer’;
grant connect on endpoint::endpoint_mirroring to [host_c_login];
grant connect on endpoint::endpoint_mirroring to [host_b_login];
–query sid
select loginname,name,sid from syslogins
见证服务器上执行:
–create user
create login host_a_login with password = ‘test’;
create user host_a_user for login host_a_login;
create certificate host_a_cert authorization host_a_user from file = ‘e:\host_a_cert.cer’;
grant connect on endpoint::endpoint_mirroring to [host_a_login];
–add user host_b_login to have pemission to access witness
create login host_b_login with password = ‘test’;
create user host_b_user for login host_b_login;
create certificate host_b_cert authorization host_b_user from file = ‘e:\host_b_cert.cer’;
grant connect on endpoint::endpoint_mirroring to [host_b_login];
grant connect on endpoint::endpoint_mirroring to host_c_login
use master;
exec sp_addlogin
@loginame = ‘host_b_login’,
@passwd = ‘test’,
@sid = 0x1a914ca3d1d00c4793ebc96e4c4f4352 ;
alter database ccerp_jzt set partner = ‘tcp://192.168.137.32:5022’;
四.建立镜像:
先在镜像服务器上执行:
alter database ccerp_jzt set partner = ‘tcp://192.168.137.44:5022’;
接着主体服务器执行:
alter database ccerp_jzt set partner = ‘tcp://192.168.137.32:5022’;
alter database ccerp_jzt set witness = ‘tcp://192.168.137.49:5022’;
至此引证书建立完毕
四、测试操作
1、主备互换
–主机执行:
1use master;
2alter database <databasename> set partner failover;
2、主服务器down掉,备机紧急启动并且开始服务
–备机执行:
1use master;
2alter database <databasename> set partner force_service_allow_data_loss;
3、原来的主服务器恢复,可以继续工作,需要重新设定镜像
1–备机执行:
2use master;
3alter database <databasename> set partner resume; –恢复镜像
4alter database <databasename> set partner failover; –切换主备
4、原来的主服务器恢复,可以继续工作
–默认情况下,事务安全级别的设置为 full,即同步运行模式,而且sql server 2005 标准版只支持同步模式。
–关闭事务安全可将会话切换到异步运行模式,该模式可使性能达到最佳。
1use master;
2alter database <databasename> set partner safety full; –事务安全,同步模式
3alter database <databasename> set partner safety off; –事务不安全,异步模式
错误说明:
消息1498,级别16,状态3,第1 行
默认情况下,数据库镜像是被禁用的。当前提供的数据库镜像仅供评估使用,并不应使用于生产环境中。若要以评估为目的启用数据库镜像,请在启动过程中使用跟踪标志1400。有关跟踪标志和启动选项的详细信息,请参阅sql server 联机丛书。
解决办法:没打sp1以上补丁.强烈建议打sp3
消息1475,级别16,状态2,第1 行
由于”ccerp_jzt” 数据库可能有尚未备份的大容量日志记录更改,所以无法启用数据库镜像。必须在镜像上还原主体数据库的上一次日志备份。
主体上:backup log ccerp_jzt to disk =’e:\log.trn’ with no_truncate
镜像上:restore log ccerp_jzt from disk=’e:\log.trn’ with norecovery