mongodb通过oplog还原数据及Failed: restore error: error applying oplog: applyOps: not authorized on admin to execute command { applyOps...解决方法

一、配置环境

在192.168.3.71的虚机上搭建一主两从复制环境。

1、mongdb基本信息

①版本:3.2.8
②端口:
Primary:28010
Secondary:28011、28012

2、目录信息

PrimarySecondarySecondary
DATA/data/mongodb1/data/data/mongodb2/data/data/mongodb3/data
LOG/data/mongodb1/log/data/mongodb2/log/data/mongodb3/log
KEY/data/mongodb1/key/data/mongodb2/key/data/mongodb3/key

二、测试

测试通过oplog指定时间点恢复和全量恢复,同时观察从库上数据的变化,下面记录了成功和失败的两次测试结果。

1、在主从初始化完成后,在主库上插入数据测试(未成功)

①插入数据

rs1:PRIMARY> use wr
rs1:PRIMARY> db.createCollection('test1')
rs1:PRIMARY> db.createCollection('test2')
rs1:PRIMARY>for(i=0;i<=10000;i++)(db.test1.insert({id:i,name:'test',date:newDate()}))
WriteResult({ "nInserted" : 1 })
rs1:PRIMARY>for(i=0;i<1000;i++)(db.test2.insert({id:i,name:'test2',date:newDate()}))
WriteResult({ "nInserted" : 1 })

②进行全备

mongodump -uroot -proot --port 28010 --oplog -o /data/backup/full

《mongodb通过oplog还原数据及Failed: restore error: error applying oplog: applyOps: not authorized on admin to execute command { applyOps...解决方法》

③再次插入数据,备份local下的oplog.rs

a、再次插入数据
rs1:PRIMARY> for(i=1000;i<=2000;i++)(db.test2.insert({id:i,name:'test2',date:new Date()}))
WriteResult({ "nInserted" : 1 })

b、备份oplog.rs
在用超级用户root备份时出错:
mongodump --port 28010 -uroot -proot -d local -c oplog.rs -o /data/backup/
2018-06-14T00:26:45.777+0800    Failed: error connecting to db server: server returned error on SASL authentication step: Authentication failed.

需要添加--authenticationDatabase admin
[mongod@mgtest full]$ mongodump -h 192.168.3.71 --port 28010 -uroot -proot --authenticationDatabase admin -d local -c oplog.rs -o /data/backup/oplog
2018-06-14T02:21:11.382+0800    writing local.oplog.rs to 
2018-06-14T02:21:11.455+0800    done dumping local.oplog.rs (11025 documents)

④在主库上进行还原

a、[mongod@mgtest full]$ mongorestore --port 28010 -uroot -proot --oplogReplay --drop /data/backup/full

《mongodb通过oplog还原数据及Failed: restore error: error applying oplog: applyOps: not authorized on admin to execute command { applyOps...解决方法》

b、查看主库上数据变化

《mongodb通过oplog还原数据及Failed: restore error: error applying oplog: applyOps: not authorized on admin to execute command { applyOps...解决方法》
和第一次插入数据一致

c、查看从库上数据变化

《mongodb通过oplog还原数据及Failed: restore error: error applying oplog: applyOps: not authorized on admin to execute command { applyOps...解决方法》
和主库数据一致

⑤在主库还原第二次备份得oplog文件

a、mongorestore --port 28010 -uroot -proot --oplogReplay  /data/backup/oplog/local/  在直接进行还原时报错

《mongodb通过oplog还原数据及Failed: restore error: error applying oplog: applyOps: not authorized on admin to execute command { applyOps...解决方法》

b、mv oplog.rs.bson oplog.bson
c、mongorestore --port 28010 -uroot -proot --oplogReplay  /data/backup/oplog/local/

《mongodb通过oplog还原数据及Failed: restore error: error applying oplog: applyOps: not authorized on admin to execute command { applyOps...解决方法》

《mongodb通过oplog还原数据及Failed: restore error: error applying oplog: applyOps: not authorized on admin to execute command { applyOps...解决方法》

《mongodb通过oplog还原数据及Failed: restore error: error applying oplog: applyOps: not authorized on admin to execute command { applyOps...解决方法》

d、还原后查看主库上数据发现并没有还原到第二次插入数据,查看从库上也没有新增数据(和主库保持一致)

主:
《mongodb通过oplog还原数据及Failed: restore error: error applying oplog: applyOps: not authorized on admin to execute command { applyOps...解决方法》
从:
《mongodb通过oplog还原数据及Failed: restore error: error applying oplog: applyOps: not authorized on admin to execute command { applyOps...解决方法》
通过观察数据发现并没有还原第二次插入的数据,测试没有成功

⑥查看在mongorestore时的报错

在执行完mongorestore --port 28010 -uroot -proot --oplogReplay  /data/backup/oplog/local/后,会输出大量信息,捕捉不到开始的报错信息,后重新测试还原少量数据,发现报Failed: restore error: error applying oplog: applyOps: not authorized on admin to execute command { applyOps...

解决方法(在admin数据库中执行):
db.createRole({role:'sysadmin',roles:[], privileges:[ {resource:{anyResource:true},actions:['anyAction']}]})
db.grantRolesToUser( "root" , [ { role: "sysadmin", db: "admin" } ]) 

 ##2、第二次测试

①插入数据,进行全备

rs1:PRIMARY> for(i=0;i<10000;i++)(db.a.insert({id:i,name:'test'}))
WriteResult({ "nInserted" : 1 })

进行全备:

mongodump --port 28010 -uroot -proot --oplog -o /data/backup/full

②修改数据,备份oplog

插入数据

rs1:PRIMARY> for(i=0;i<10000;i++)(db.b.insert({id:i,name:'test'}))
WriteResult({ "nInserted" : 1 })

查看当前时间戳:

rs.status()

《mongodb通过oplog还原数据及Failed: restore error: error applying oplog: applyOps: not authorized on admin to execute command { applyOps...解决方法》

再次插入数据

rs1:PRIMARY> db.a.insert({id:20001,name:'wangrui'})
WriteResult({ "nInserted" : 1 })

《mongodb通过oplog还原数据及Failed: restore error: error applying oplog: applyOps: not authorized on admin to execute command { applyOps...解决方法》

③备份oplog

mongodump --port 28010 -uroot -proot --authenticationDatabase admin -d local -c oplog.rs -o /data/backup/oplog

④进行还原全备数据

mongorestore --port 28010 -uroot -proot --oplogReplay --drop /data/backup/full 

《mongodb通过oplog还原数据及Failed: restore error: error applying oplog: applyOps: not authorized on admin to execute command { applyOps...解决方法》

查看数据,和第二次插入数据前一致

《mongodb通过oplog还原数据及Failed: restore error: error applying oplog: applyOps: not authorized on admin to execute command { applyOps...解决方法》

⑤进行增量还原

cp oplog/local/oplog.rs.bson ./full/oplog.bson
mongorestore --port 28010 -uroot -proot --oplogReplay --oplogLimit “1529577364:135” /data/backup/full 

《mongodb通过oplog还原数据及Failed: restore error: error applying oplog: applyOps: not authorized on admin to execute command { applyOps...解决方法》

查看数据,在时间戳之后的数据没有还原,其他和修改后数据一致,主从数据一致。
《mongodb通过oplog还原数据及Failed: restore error: error applying oplog: applyOps: not authorized on admin to execute command { applyOps...解决方法》

《mongodb通过oplog还原数据及Failed: restore error: error applying oplog: applyOps: not authorized on admin to execute command { applyOps...解决方法》

⑥不指定oplogLimit进行增量还原,看test库a中id为20001的数据是否存在

mongorestore --port 28010 -uroot -proot --oplogReplay /data/backup/full 

《mongodb通过oplog还原数据及Failed: restore error: error applying oplog: applyOps: not authorized on admin to execute command { applyOps...解决方法》

查看数据,发现a中id为20001的数据已被还原

《mongodb通过oplog还原数据及Failed: restore error: error applying oplog: applyOps: not authorized on admin to execute command { applyOps...解决方法》

点赞