如果主机坏了断开连接就用备机的镜像数据库
--主备互换,备机sql命令
USE master;
ALTER DATABASE test SET PARTNER FORCE_SERVICE_ALLOW_DATA_LOSS;
之后会恢复两分钟,备机的数据库就能用了
如果主机恢复了就
--原来的主服务器恢复,可以继续工作,需要重新设定镜像,第一次可能会报个错
USE master;
ALTER DATABASE test SET PARTNER RESUME; --恢复镜像
ALTER DATABASE test SET PARTNER FAILOVER; --切换主备
建立镜像的步骤在这:https://www.cnblogs.com/xuanlanbinfen/p/10308079.html
也就是重新建立镜像,主机 建之前先删除 证书,端点,证书的登录名,用户,和备机的证书;备机也重新操作一遍
--删除证书
DROP CERTIFICATE HOST_A_cert --创建证书(主备可并行执行)
USE master;
CREATE MASTER KEY ENCRYPTION BY PASSWORD ='www.wbsbj000.com';
CREATE CERTIFICATE HOST_A_cert WITH SUBJECT ='HOST_A ccertificate',
START_DATE ='01/22/2019';
--删除端点 DROP ENDPOINT Endpoint_Mircroring
--创建连接的端点(主备可并行执行)
--删除端点
DROP ENDPOINT Endpoint_Mircroring --创建连接的端点(主备可并行执行)
CREATE ENDPOINT Endpoint_Mircroring
STATE = STARTED
AS
TCP ( LISTENER_PORT=5022 , LISTENER_IP = ALL )
FOR
DATABASE_MIRRORING
( AUTHENTICATION = CERTIFICATE HOST_A_cert , ENCRYPTION = REQUIRED ALGORITHM AES , ROLE = ALL );
--备份证书以备建立互联(主备可并行执行)
BACKUP CERTIFICATE HOST_A_cert TO FILE = 'E:\SQLBackup\HOST_A_cert.cer'; --4、互换证书 --删除登录名
DROP LOGIN HOST_B_login --删除证书
DROP CERTIFICATE HOST_B_cert --删除用户
DROP USER HOST_B_user --5、添加登陆名、用户(主备可并行执行)
CREATE LOGIN HOST_B_login WITH PASSWORD ='www.wbsbj000.com';
CREATE USER HOST_B_user FOR LOGIN HOST_B_login;
CREATE CERTIFICATE HOST_B_cert AUTHORIZATION HOST_B_user FROM FILE ='E:\SQLBackup\HOST_B_cert.cer';
GRANT CONNECT ON ENDPOINT::Endpoint_Mirroring TO [HOST_B_login]; --删除登录名
DROP LOGIN HOST_B_login --删除证书
DROP CERTIFICATE HOST_B_cert --删除用户
DROP USER HOST_B_user --关闭镜像
alter database test set partner OFF --开启镜像
ALTER DATABASE test
SET PARTNER = N'TCP://192.168.1.103:5022'-- boat主机名
GO