MongoDB 单节点开启 oplog
目录
MongoDB 单节点是没有 oplog 的, 但是在测试的时候或者在生产环境, 是有存在单节点的情况的. 这个文档介绍一下是怎么单节点开启 oplog 的.
修改配置文件
在 mongodb.conf
中添加一行
|
|
重启 MongoDB
停止 MongoDB
|
|
如果报错这个错误的话, 需要添加权限:
|
|
启动 MongoDB
|
|
初始化副本集的脚本
需要在 local
或者 admin
数据库中执行:
|
|
如果报这个错误则, 需要添加权限:
1 2 3 4 5 6 7 8
{ "ok" : 0, "errmsg" : "not authorized on admin to execute command { replSetInitiate: { _id: \"single\", members: [ { _id: 0.0, host: \"10.140.6.26:27017\" } ] }, lsid: { id: UUID(\"7fdda9e0-3a2b-469b-93b6-e5e92f2f4997\") }, $db: \"admin\" }", "code" : 13, "codeName" : "Unauthorized" } > db.grantRolesToUser( "mustard" , [ { role: "clusterManager", db: "admin" } ])
初始完, 副本集中唯一的节点, 可能短时间显示为 SECONDARY
或 OTHER
. 一般而言, 稍等一会, 就会自然恢复为 PRIMARY
, 无需人工干预.