使用证书配置的镜像基本安装微软次序做就可以了

http://msdn.microsoft.com/zh-cn/library/ms191140.aspx

备份还原首先要转换成完全备份模式没什么好多说的

 USE master;
GO
ALTER DATABASE
SET RECOVERY FULL;
GO

直接备份数据库不能直接写C盘,目录自己建

 BACKUP DATABASE [AdventureWorks2012]
TO DISK = 'C:\HOSTA\AdventureWorks2012.bak'
WITH FORMAT
GO

同样还原也需要指定,注意一定要使用WITH NORECOVERY

 USE [master]
RESTORE DATABASE [AdventureWorks2012]
FROM DISK = N'C:\HOSTB\AdventureWorks2012.bak'
WITH NORECOVERY,
MOVE N'AdventureWorks2012_Data' TO N'C:\Program Files\Microsoft SQL Server\MSSQL11.SQL2\MSSQL\DATA\AdventureWorks2012_Data.mdf',
MOVE N'AdventureWorks2012_Log' TO N'C:\Program Files\Microsoft SQL Server\MSSQL11.SQL2\MSSQL\DATA\AdventureWorks2012_log.ldf'
GO

证书创建和对倒部分其实就一步步做就可以了,注意端口号,路径用刚才的路径就成。密码偷懒也可以不改,原则上机器用的密码不要设置过期日期

HOSTA上

 USE master;
CREATE MASTER KEY ENCRYPTION BY PASSWORD = '<1_Strong_Password!>';
GO
USE master;
CREATE CERTIFICATE HOST_A_cert
WITH SUBJECT = 'HOST_A certificate';
GO
CREATE ENDPOINT Endpoint_Mirroring
STATE = STARTED
AS TCP (
LISTENER_PORT=7024
, LISTENER_IP = ALL
)
FOR DATABASE_MIRRORING (
AUTHENTICATION = CERTIFICATE HOST_A_cert
, ENCRYPTION = REQUIRED ALGORITHM AES
, ROLE = ALL
);
GO
BACKUP CERTIFICATE HOST_A_cert TO FILE = 'C:\HOSTA\HOST_A_cert.cer';
GO

HOSTB上

 USE master;
CREATE MASTER KEY ENCRYPTION BY PASSWORD = '<Strong_Password_#2>';
GO
CREATE CERTIFICATE HOST_B_cert
WITH SUBJECT = 'HOST_B certificate for database mirroring';
GO
CREATE ENDPOINT Endpoint_Mirroring
STATE = STARTED
AS TCP (
LISTENER_PORT=7024
, LISTENER_IP = ALL
)
FOR DATABASE_MIRRORING (
AUTHENTICATION = CERTIFICATE HOST_B_cert
, ENCRYPTION = REQUIRED ALGORITHM AES
, ROLE = ALL
);
GO
BACKUP CERTIFICATE HOST_B_cert TO FILE = 'C:\HOSTB\HOST_B_cert.cer';
GO

HOSTA上的证书copy到HOSTB上,HOSTB上的证书copy到HOSTA上,随后创建用户导入证书

HOSTA

 USE master;
CREATE LOGIN HOST_B_login WITH PASSWORD = '1Sample_Strong_Password!@#';
GO
CREATE USER HOST_B_user FOR LOGIN HOST_B_login;
GO
CREATE CERTIFICATE HOST_B_cert
AUTHORIZATION HOST_B_user
FROM FILE = 'C:\HOSTA\HOST_B_cert.cer'
GO
GRANT CONNECT ON ENDPOINT::Endpoint_Mirroring TO [HOST_B_login];
GO

HOSTB

 USE master;
CREATE LOGIN HOST_A_login WITH PASSWORD = '=Sample#2_Strong_Password2';
GO
CREATE USER
FOR LOGIN HOST_A_login;
GO
CREATE CERTIFICATE HOST_A_cert
AUTHORIZATION HOST_A_user
FROM FILE = 'C:\HOSTB\HOST_A_cert.cer'
GO
GRANT CONNECT ON ENDPOINT::Endpoint_Mirroring TO [HOST_A_login];
GO

在HOSTB上首先运行, 可以使用IP地址或者主机名

 --At HOST_B, set server instance on HOST_A as partner (principal server):
ALTER DATABASE [AdventureWorks2012]
SET PARTNER = 'TCP://192.168.100.101:7024';
GO

随后在HOSTA上运行

 --At HOST_A, set server instance on HOST_B as partner (mirror server).
ALTER DATABASE [AdventureWorks2012]
SET PARTNER = 'TCP://192.168.100.102:7024';
GO

主库上应该显示为,主体已同步,备库上显示为正在恢复。

强制关闭HOSTA,在HOSTB使用以下语句进行切换,需要等待几秒。数据库显示为主体已断开连接

 USE master;
alter database [AdventureWorks2012] set partner FORCE_SERVICE_ALLOW_DATA_LOSS;

http://msdn.microsoft.com/zh-cn/library/bb522476.aspx

此时恢复HOSTA,数据库状态为镜像,挂起/正在还原。。。在HOSTB中操作

 USE master;
alter database [AdventureWorks2012] set partner resume;

这样HOSTB 就成为了主体,假如要将HOSTA在设置为主体,在HOSTB上的运行一下语句,切换为HOSTA为主题

 USE master;
alter database [AdventureWorks2012] set partner resume;

Reference

http://msdn.microsoft.com/zh-cn/library/ms189852(v=sql.110).aspx

http://msdn.microsoft.com/zh-cn/library/ms189053.aspx

http://msdn.microsoft.com/zh-cn/library/ms189921.aspx

http://www.mssqltips.com/sqlservertip/2464/configure-sql-server-database-mirroring-using-ssms/

05-11 13:28