ICode9

精准搜索请尝试: 精确搜索
首页 > 数据库> 文章详细

【MSSQL】镜像维护

2022-01-11 10:35:33  阅读:227  来源: 互联网

标签:database -- DB 节点 TEST 镜像 维护 MSSQL


参考

概述:https://blog.csdn.net/qq_33656602/article/details/86492019

参考:http://blog.itpub.net/30126024/viewspace-2643859/

搭建:https://blog.csdn.net/elie_yang/article/details/88024042 (注意配置镜像时按下面逻辑配置)

启用GUEST:https://jingyan.baidu.com/article/f54ae2fc57c5371e92b849a3.html

启用共享免密:https://www.cnblogs.com/ymj126/p/6106899.html

使用证书搭建镜像:https://blog.csdn.net/jojoliny/article/details/103990314

使用证书搭建镜像:https://docs.microsoft.com/zh-cn/sql/database-engine/database-mirroring/use-certificates-for-a-database-mirroring-endpoint-transact-sql?view=sql-server-ver15

 

清理(收缩)事务日志并恢复镜像

语法:https://docs.microsoft.com/en-us/sql/t-sql/statements/alter-database-transact-sql-database-mirroring

-- 以下内容库名以 DB_TEST 代替
-- 1、主节点 DB_TEST 库属性停止镜像

-- 2、停止主节点 DB_TEST 库的备份类型完整模式(FULL)
alter database DB_TEST set recovery simple;

-- 3、收缩文件
dbcc shrinkdatabase(DB_TEST);

-- 4、恢复主节点 DB_TEST 库的备份类型完整模式(FULL)
alter database DB_TEST set recovery full;

-- 5、备份主节点 DB_TEST

-- 6、恢复从节点 restore DB_TEST files and filegroups,注意使用 restore with norecovery

-- 7、恢复从节点 restore DB_TEST transaction log,注意使用 restore with norecovery

-- 8、设置从节点镜像源
alter database DB_TEST set partner = 'TCP://主:5022';

-- 8.1、如果从节点已经注册一个镜像
alter database DB_TEST set partner off;

-- 9、设置主节点中的从节点
alter database DB_TEST set partner = 'TCP://从:5022';

-- 10、如果库是 restoring 状态无法删除
restore database DB_TEST with recovery;

 

The mirror database, “XX”, has insufficient transaction log data to preserve the log backup chain of the principal database. This may happen if a log backup from the principal database has not been taken or has not been restored on the mirror database.

针对上面报错,再单独备份并恢复事务日志

 

The remote copy of database "XX" has not been rolled forward to a point in time that is encompassed in the local copy of the database log.

上面的报错会出现在做了一次完整备份,并把备份文件还原到镜像库中,然后还没开始镜像配置时又在主体服务器做了一次日志备份,导致事务日志中找不到位置。解决方法是再单独备份并恢复事务日志

 

计划内切换

ALTER DATABASE DB_TEST SET SAFETY FULL
USE master;
ALTER DATABASE DB_TEST SET PARTNER FAILOVER;

 

计划外切换

  • 假设原 A->B,A 是 PRIMARY
  • 确保 A 断电或已确认停止服务
  • 接受 A 重启后部分未同步的数据可能存在丢失风险

 

强制切换

-- 在 B 上执行
USE master;
ALTER DATABASE DB_TEST SET PARTNER FORCE_SERVICE_ALLOW_DATA_LOSS;

 

回切

-- A 恢复后,在 B 执行
USE master;
ALTER DATABASE DB_TEST SET PARTNER RESUME;

-- 此时 A 作为镜像
-- B 作为 PRIMARY,回切需要在 B 上执行
ALTER DATABASE DB_TEST SET PARTNER FAILOVER;

 

 

使用证书配置镜像

SSMS 提示报错

The ATER DATABASE command could not be sent to the remote server instance 'TCP://:5022'. The database mirroring configuration was not changed. Verify that the server is connected, and try again.

这个报错需要查看服务日志,找到操作不成功的具体问题。

 

报错1

-- 执行添加镜像节点时,例如
alter database DB_TEST set witness = 'TCP://:5022'

Database mirroring connection error5'Connection handshake faild. There is no compatible authentication protocol. State 21.' for 'TCP://:5022'.

节点间镜像同步的认证方式不同,确认 endpoint 的连接认证方式和加密方式是否相同。

select * from sys.database_mirroring_endpoints

 

报错2

-- 执行添加镜像节点时,例如
alter database DB_TEST set witness = 'TCP://:5022'

Database mirroring connection error4'10053(An established connection was aborted by the software in your host machine)' for 'TCP://:5022'

部分节点缺少证书,确认各个节点都有其他所有节点的证书。

select * from sys.certificates

 

报错3

-- 导入证书时
create certificate host_1 from file='';

-- 创建本地证书时
create certificate host_1 with subject='host_1_cert', expiry_date='2099-01-01';

Please create a master key in the database or open themaster key in the session before performing this operation.

本地没有创建 master key,所以不允许创建本地证书和导入外部证书,只需要执行:

create master key encryption by password='';

-- 然后就可以创建本地证书并导出证书
backup certificate host_w to file='';

 

报错4

create certificate host_1 from file='';

The certificate, asymmetric key, or private key file is not valid or does not exist; or you do not have permissions for it.

确认文件位置,确认是否是最大权限用户。

 

标签:database,--,DB,节点,TEST,镜像,维护,MSSQL
来源: https://www.cnblogs.com/jiangxu67/p/15787172.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有