MongoDB - Mongorestore 未按預期工作
非常美好的一天,
我在 mongodb 中有如下具有超級使用者權限的使用者(我使用
show users
命令確認了角色設置){ "_id" : "admin.mongoadmin", "user" : "mongoadmin", "db" : "admin", "roles" : [ { "role" : "readWrite", "db" : "admin" }, { "role" : "root", "db" : "admin" } ], "mechanisms" : [ "SCRAM-SHA-1", "SCRAM-SHA-256" ] }
當我嘗試使用 mongorestore 恢復 oplog 時,出現錯誤:
失敗:恢復錯誤:錯誤應用oplog:applyOps:管理員未授權執行命令{applyOps:[{ts:時間戳(1552828309,1),h:4632811839329880092,v:2,op:“c”,ns:“admin .$cmd", o: { create: “system.keys”, idIndex: { v: 2, key: { _id: 1 }, name: " id “, ns: “admin.system.keys” } }, o2 : {} } ], $db: “admin” }
mongorestore -u admin -p password --authenticationDatabase=admin --oplogFile 0000000000_0_oplog.bson --oplogReplay --oplogLimit=1552828432 --dir='/oplog/temp' 2019-03-17T13:47:36.945+0000 preparing collections to restore from 2019-03-17T13:47:36.945+0000 replaying oplog 2019-03-17T13:47:36.962+0000 Failed: restore error: error applying oplog: applyOps: not authorized on admin to execute command { applyOps: [ { ts: Timestamp(1552828309, 1), h: 4632811839329880092, v: 2, op: "c", ns: "admin.$cmd", o: { create: "system.keys", idIndex: { v: 2, key: { _id: 1 }, name: "_id_", ns: "admin.system.keys" } }, o2: {} } ], $db: "admin" }
注意:我用從 bsondump 獲得的最後一個值指定了 oploglimit (–oplogLimit=1552828432) 這是正確的嗎?還是我錯過了什麼?
(IE)
{"ts":{"$timestamp":{"t":1552828432,"i":79}},"t":{"$numberLong":"1"},"h":{"$numberLong":"-2072015676601300967"},"v":2,"op":"i","ns":"inventory.hari","ui":{"$binary":"avdlGH8AS1eBPXRytlO1Yg==","$type":"04"},"wall":{"$date":"2019-03-17T13:13:52.139Z"},"o":{"_id":"79","name":"Hari","role":"Developer","isEmployee":true}} {"ts":{"$timestamp":{"t":1552828432,"i":80}},"t":{"$numberLong":"1"},"h":{"$numberLong":"-6279494628130059002"},"v":2,"op":"u","ns":"inventory.hari","ui":{"$binary":"avdlGH8AS1eBPXRytlO1Yg==","$type":"04"},"o2":{"_id":"79"},"wall":{"$date":"2019-03-17T13:13:52.139Z"},"o":{"_id":"79","name":"WD_Userjava.util.Random@9a7504c","role":"Developer","isEmployee":true}}
編輯:
我做瞭如下的恢復
mongorestore -u mongoadmin -p password --authenticationDatabase admin -d itsm_inventory -c hari 0000000000_0_oplog.bson
好像很成功
我這樣插入
{ "_id" : "0", "name" : "WD_Userjava.util.Random@2bbf4b8b", "role" : "Developer", "isEmployee" : true } { "_id" : "1", "name" : "WD_Userjava.util.Random@7a765367", "role" : "Developer", "isEmployee" : true }
但它恢復如下
{ "_id" : ObjectId("5c91eac58fe872c878289d83"), "ts" : Timestamp(1552888901, 2), "t" : NumberLong(1), "h" : NumberLong("-3584326290393879639"), "v" : 2, "op" : "i", "ns" : "itsm_inventory.hari", "ui" : UUID("b5f574f1-c55a-4a5b-8729-4483f56f5c9d"), "wall" : ISODate("2019-03-18T06:01:41.062Z"), "o" : { "_id" : "1", "name" : "WD_Userjava.util.Random@7a765367", "role" : "Developer", "isEmployee" : true } } { "_id" : ObjectId("5c91eac58fe872c878289d84"), "ts" : Timestamp(1552888901, 3), "t" : NumberLong(1), "h" : NumberLong("-3618415120025822301"), "v" : 2, "op" : "i", "ns" : "itsm_inventory.hari", "ui" : UUID("b5f574f1-c55a-4a5b-8729-4483f56f5c9d"), "wall" : ISODate("2019-03-18T06:01:41.062Z"), "o" : { "_id" : "2", "name" : "WD_Userjava.util.Random@76b0bfab", "role" : "Developer", "isEmployee" : true } }
Mani的回答編輯:
mongorestore -u mongoadmin -p password --authenticationDatabase=admin --oplogFile 0000000000_0_oplog.bson --oplogReplay --oplogLimit=1553063755 --dir='/oplog/temp' 2019-03-21T10:46:42.435+0000 preparing collections to restore from 2019-03-21T10:46:42.435+0000 replaying oplog 2019-03-21T10:46:42.457+0000 Failed: restore error: error applying oplog: applyOps: not authorized on admin to execute command { applyOps: [ { ts: Timestamp(1552888562, 3), h: -8964353497436574374, v: 2, op: "c", ns: "admin.$cmd", o: { create: "system.keys", idIndex: { v: 2, key: { _id: 1 }, name: "_id_", ns: "admin.system.keys" } }, o2: {} } ], $db: "admin" }
任何幫助表示讚賞。提前致謝。
根據 MongoDB jira 部落格,此處
mongorestore
--oplog
不會應用在mongodump
操作期間擷取的 oplog 條目,並在嘗試使用具有還原角色的 mongorestore 和使用訪問控制的 MongoDB 部署時退出並出現錯誤。有關其他資訊,請查看有關還原角色的文件。因為它會影響 MongoDB 版本,例如
2.6.10
,3.04
。為避免此類錯誤,需要使用目前版本更新 MongoDB。恢復角色不為使用者提供對該applyOps
命令的訪問權限,該命令mongorestore
與 –oplogReplay
選項一起使用。解決方法
要使用該選項恢復數據
mongorestore
--oplogReplay
,使用者可以創建一個授予anyResource上的anyAction的角色。不要將此角色分配給任何其他使用者。此角色提供對系統的完全不受限制的訪問。或者,使用者可以在恢復數據時禁用身份驗證,方法是在安裝期間不帶選項啟動 mongod 實例,然後在完成恢復後
--auth
重新啟用。--auth
與 OP (Harry) 交談後 & 編輯
如何在 MongoDB 中恢復“.bson”文件?
C:\Program Files\MongoDB\Server\4.0\bin>mongorestore -d test -c oplog C:\data\oplog.bson 2019-03-20T10:06:35.981+0300 checking for collection data in C:\data\oplog.bson 2019-03-20T10:06:35.985+0300 restoring test.oplog from C:\data\oplog.bson 2019-03-20T10:06:37.058+0300 no indexes to restore 2019-03-20T10:06:37.058+0300 finished restoring test.oplog (10138 documents) 2019-03-20T10:06:37.059+0300 done
在上面的 mongorestore 命令中,test是數據庫名稱,oplog是集合名稱。是文件路徑位置
C:\data\oplog.bson
。.bson
如需進一步參考,請點擊此處
根據您上傳的oplog 文件
1552888560
,最小時間戳為,最大時間戳為1553063754
. 但是--oplogLimit=1552828432
您在問題中提到的不在限制範圍內,小於最小時間戳值。這就是沒有恢復任何文件的原因。從文件所在的文件夾執行以下命令
0000000000_0_oplog.bson
。mongorestore -u admin -p password --authenticationDatabase=admin --oplogFile 0000000000_0_oplog.bson --oplogReplay --oplogLimit=1553063755 --dir='/oplog/temp'
此外,請確保
.bson
文件/oplog/temp
夾中沒有文件。