SQL2005数据库镜像的步骤
--SQL2005数据库镜像的步骤
-------------------------------------------------------------------------------------------
--1、设置镜像数据库为完整恢复模式
alter DATABASE S_C_SC set recovery FULL
--2、创建证书(主备可并行执行)
--主机执行
USE master;
CREATE MASTER KEY ENCRYPTION BY PASSWORD = 'joe';
CREATE CERTIFICATE HOST_A_cert WITH SUBJECT = 'HOST_A certificate' ,
START_DATE = '2012-09-25';
--备机执行
USE master;
CREATE MASTER KEY ENCRYPTION BY PASSWORD = 'joe';
CREATE CERTIFICATE HOST_B_cert WITH SUBJECT = 'HOST_B certificate' ,
START_DATE = '2012-09-25';
--见证执行
USE master;
CREATE MASTER KEY ENCRYPTION BY PASSWORD = 'joe';
CREATE CERTIFICATE HOST_C_cert WITH SUBJECT = 'HOST_C certificate' ,
START_DATE = '2012-09-25';
--3、创建连接的端点(主备可并行执行)
--主机执行:
USE master
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 );
--备机执行:
USE master
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 );
--见证执行:
USE master
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 );
--4、备份证书以备建立互联(主备可并行执行)
--主机执行:
USE master
BACKUP CERTIFICATE HOST_A_cert TO FILE = 'D:\SQLBackup\HOST_A_cert.cer';
--备机执行:
USE master
BACKUP CERTIFICATE HOST_B_cert TO FILE = 'D:\SQLBackup\HOST_B_cert.cer';
--见证执行:
USE master
BACKUP CERTIFICATE HOST_C_cert TO FILE = 'D:\SQLBackup\HOST_C_cert.cer';
--5、互换证书
--
--将备份到D:\SQLBackup\的证书进行互换,即HOST_A_cert.cer复制(是复制不是剪切)
--到备机的D:\SQLBackup\。HOST_B_cert.cer复制(是复制不是剪切)到主机的D:\SQLBackup\
--见证的证书HOST_C_cert.cer复制到主机和备机,主机和备机复制到见证
--6、添加登陆名、用户(主备见证可并行执行)
--以下操作只能通过命令行运行,通过图形界面无法完成。(截至文档编写结束,SQL Server2005的版本号为SP2)
--主机执行:
USE master
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 = 'D:\SQLBackup\HOST_B_cert.cer';
CREATE CERTIFICATE HOST_B_cert AUTHORIZATION HOST_B_user FROM FILE = 'D:\HOST_B_cert.cer';
GRANT CONNECT ON ENDPOINT::Endpoint_Mirroring TO [HOST_B_login]
USE master
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 = 'D:\HOST_C_cert.cer';
GRANT CONNECT ON ENDPOINT::Endpoint_Mirroring TO [HOST_C_login];
GO
--备机执行:
USE master
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 = 'D:\SQLBackup\HOST_A_cert.cer';
CREATE CERTIFICATE HOST_A_cert AUTHORIZATION HOST_A_user FROM FILE = 'D:\HOST_A_cert.cer';
GRANT CONNECT ON ENDPOINT::Endpoint_Mirroring TO [HOST_A_login];
USE master
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 = 'D:\HOST_C_cert.cer';
GRANT CONNECT ON ENDPOINT::Endpoint_Mirroring TO [HOST_C_login];
GO
--见证执行:
USE master
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 = 'D:\SQLBackup\HOST_A_cert.cer';
CREATE CERTIFICATE HOST_A_cert AUTHORIZATION HOST_A_user FROM FILE = 'D:\HOST_A_cert.cer';
GRANT CONNECT ON ENDPOINT::Endpoint_Mirroring TO [HOST_A_login];
USE master
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 = 'D:\SQLBackup\HOST_B_cert.cer';
CREATE CERTIFICATE HOST_B_cert AUTHORIZATION HOST_B_user FROM FILE = 'D:\HOST_B_cert.cer';
GRANT CONNECT ON ENDPOINT::Endpoint_Mirroring TO [HOST_B_login];
-------------------------------------------------------------------------------------------------------
--建立镜像关系---------------------------------------------------------------------------------------
--以下步骤是针对每个数据库进行的,例如:现有主机中有5个数据库以下过程就要执行5次。
--7、 手工同步登录名和密码
--在前面提到数据库镜像的缺点之一是无法维护登录名,所以需要我们手工维护登录解决孤立用户。
--在主数据库中执行如下语句:
USE master;
select sid,name,dbname from syslogins WHERE dbname='S_C_SC'
--查找出要做镜像的那个数据库里面有哪些用户名和sid,例如:上述的’myuser’
--在备库中执行如下语句:
USE master;
exec sp_addlogin
@loginame = '<LoginName>',
@passwd = '<Password>',
@sid = <sid> ;
--8、准备备机数据库
--主库要备份两个bak文件
--第一个:完整备份
--第二个:事务日志备份 ,截断事务日志
--(1)先在备机还原完整备份,“restore with norecovery”和 覆盖现有数据库
--(2)再还原事务日志,“restore with norecovery” 和时间点:最近状态
--由于是实验,没有为服务器配置双网卡,IP地址与图有点不一样,但是原理一样。
--9、必须要在镜像数据库中先设置好伙伴后,才能在主体服务器执行
--再在主体服务器实例上,将 备机上的服务器实例设置为伙伴
--备机执行
USE master
ALTER DATABASE S_C_SC SET PARTNER = 'TCP://192.168.1.100:5022'
--主机执行
USE master
ALTER DATABASE S_C_SC SET PARTNER = 'TCP://192.168.1.103:5022'
--10、在主机执行设置见证服务器
ALTER DATABASE S_C_SC SET WITNESS = 'TCP://192.168.1.101:5022';
GO
-------------------------测试------------------------------
--1、主备互换
--主机停掉SQL服务
--2、主服务器Down掉,备机紧急启动并且开始服务
--备机执行:
USE master;
ALTER DATABASE S_C_SC SET PARTNER FORCE_SERVICE_ALLOW_DATA_LOSS;
ALTER DATABASE S_C_SC SET ONLINE
--3、开启主机的SQL服务,原来的主服务器恢复,可以继续工作,需要重新设定镜像
--备机执行:
USE master;
ALTER DATABASE S_C_SC SET PARTNER RESUME; --恢复镜像
ALTER DATABASE S_C_SC SET PARTNER FAILOVER; --切换到主机
--4、原来的主服务器恢复,可以继续工作
-------------------------------------------------------------------------------------------
--1、设置镜像数据库为完整恢复模式
alter DATABASE S_C_SC set recovery FULL
--2、创建证书(主备可并行执行)
--主机执行
USE master;
CREATE MASTER KEY ENCRYPTION BY PASSWORD = 'joe';
CREATE CERTIFICATE HOST_A_cert WITH SUBJECT = 'HOST_A certificate' ,
START_DATE = '2012-09-25';
--备机执行
USE master;
CREATE MASTER KEY ENCRYPTION BY PASSWORD = 'joe';
CREATE CERTIFICATE HOST_B_cert WITH SUBJECT = 'HOST_B certificate' ,
START_DATE = '2012-09-25';
--见证执行
USE master;
CREATE MASTER KEY ENCRYPTION BY PASSWORD = 'joe';
CREATE CERTIFICATE HOST_C_cert WITH SUBJECT = 'HOST_C certificate' ,
START_DATE = '2012-09-25';
--3、创建连接的端点(主备可并行执行)
--主机执行:
USE master
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 );
--备机执行:
USE master
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 );
--见证执行:
USE master
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 );
--4、备份证书以备建立互联(主备可并行执行)
--主机执行:
USE master
BACKUP CERTIFICATE HOST_A_cert TO FILE = 'D:\SQLBackup\HOST_A_cert.cer';
--备机执行:
USE master
BACKUP CERTIFICATE HOST_B_cert TO FILE = 'D:\SQLBackup\HOST_B_cert.cer';
--见证执行:
USE master
BACKUP CERTIFICATE HOST_C_cert TO FILE = 'D:\SQLBackup\HOST_C_cert.cer';
--5、互换证书
--
--将备份到D:\SQLBackup\的证书进行互换,即HOST_A_cert.cer复制(是复制不是剪切)
--到备机的D:\SQLBackup\。HOST_B_cert.cer复制(是复制不是剪切)到主机的D:\SQLBackup\
--见证的证书HOST_C_cert.cer复制到主机和备机,主机和备机复制到见证
--6、添加登陆名、用户(主备见证可并行执行)
--以下操作只能通过命令行运行,通过图形界面无法完成。(截至文档编写结束,SQL Server2005的版本号为SP2)
--主机执行:
USE master
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 = 'D:\SQLBackup\HOST_B_cert.cer';
CREATE CERTIFICATE HOST_B_cert AUTHORIZATION HOST_B_user FROM FILE = 'D:\HOST_B_cert.cer';
GRANT CONNECT ON ENDPOINT::Endpoint_Mirroring TO [HOST_B_login]
USE master
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 = 'D:\HOST_C_cert.cer';
GRANT CONNECT ON ENDPOINT::Endpoint_Mirroring TO [HOST_C_login];
GO
--备机执行:
USE master
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 = 'D:\SQLBackup\HOST_A_cert.cer';
CREATE CERTIFICATE HOST_A_cert AUTHORIZATION HOST_A_user FROM FILE = 'D:\HOST_A_cert.cer';
GRANT CONNECT ON ENDPOINT::Endpoint_Mirroring TO [HOST_A_login];
USE master
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 = 'D:\HOST_C_cert.cer';
GRANT CONNECT ON ENDPOINT::Endpoint_Mirroring TO [HOST_C_login];
GO
--见证执行:
USE master
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 = 'D:\SQLBackup\HOST_A_cert.cer';
CREATE CERTIFICATE HOST_A_cert AUTHORIZATION HOST_A_user FROM FILE = 'D:\HOST_A_cert.cer';
GRANT CONNECT ON ENDPOINT::Endpoint_Mirroring TO [HOST_A_login];
USE master
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 = 'D:\SQLBackup\HOST_B_cert.cer';
CREATE CERTIFICATE HOST_B_cert AUTHORIZATION HOST_B_user FROM FILE = 'D:\HOST_B_cert.cer';
GRANT CONNECT ON ENDPOINT::Endpoint_Mirroring TO [HOST_B_login];
-------------------------------------------------------------------------------------------------------
--建立镜像关系---------------------------------------------------------------------------------------
--以下步骤是针对每个数据库进行的,例如:现有主机中有5个数据库以下过程就要执行5次。
--7、 手工同步登录名和密码
--在前面提到数据库镜像的缺点之一是无法维护登录名,所以需要我们手工维护登录解决孤立用户。
--在主数据库中执行如下语句:
USE master;
select sid,name,dbname from syslogins WHERE dbname='S_C_SC'
--查找出要做镜像的那个数据库里面有哪些用户名和sid,例如:上述的’myuser’
--在备库中执行如下语句:
USE master;
exec sp_addlogin
@loginame = '<LoginName>',
@passwd = '<Password>',
@sid = <sid> ;
--8、准备备机数据库
--主库要备份两个bak文件
--第一个:完整备份
--第二个:事务日志备份 ,截断事务日志
--(1)先在备机还原完整备份,“restore with norecovery”和 覆盖现有数据库
--(2)再还原事务日志,“restore with norecovery” 和时间点:最近状态
--由于是实验,没有为服务器配置双网卡,IP地址与图有点不一样,但是原理一样。
--9、必须要在镜像数据库中先设置好伙伴后,才能在主体服务器执行
--再在主体服务器实例上,将 备机上的服务器实例设置为伙伴
--备机执行
USE master
ALTER DATABASE S_C_SC SET PARTNER = 'TCP://192.168.1.100:5022'
--主机执行
USE master
ALTER DATABASE S_C_SC SET PARTNER = 'TCP://192.168.1.103:5022'
--10、在主机执行设置见证服务器
ALTER DATABASE S_C_SC SET WITNESS = 'TCP://192.168.1.101:5022';
GO
-------------------------测试------------------------------
--1、主备互换
--主机停掉SQL服务
--2、主服务器Down掉,备机紧急启动并且开始服务
--备机执行:
USE master;
ALTER DATABASE S_C_SC SET PARTNER FORCE_SERVICE_ALLOW_DATA_LOSS;
ALTER DATABASE S_C_SC SET ONLINE
--3、开启主机的SQL服务,原来的主服务器恢复,可以继续工作,需要重新设定镜像
--备机执行:
USE master;
ALTER DATABASE S_C_SC SET PARTNER RESUME; --恢复镜像
ALTER DATABASE S_C_SC SET PARTNER FAILOVER; --切换到主机
--4、原来的主服务器恢复,可以继续工作