修改参数文件
[mongo@cjc-db-01 conf]$ vi mongodb.conf
新增
replSet = cjcMongodb
重启mongodb
停
> use admin;
switched to db admin
> db.shutdownServer();
启
[mongo@cjc-db-01 ~]$ cd /mongodb/27017/conf/
[mongo@cjc-db-01 conf]$ mongod -f mongodb.conf
查询
[mongo@cjc-db-01 conf]$ mongo --port 27017
> show dbs;
2023-02-08T17:15:54.317+0800 E QUERY [js] Error: listDatabases failed:{
"ok" : 0,
"errmsg" : "not master and slaveOk=false",
"code" : 13435,
"codeName" : "NotMasterNoSlaveOk"
} :
_getErrorWithCode@src/mongo/shell/utils.js:25:13
Mongo.prototype.getDBs@src/mongo/shell/mongo.js:151:1
shellHelper.show@src/mongo/shell/utils.js:882:13
shellHelper@src/mongo/shell/utils.js:766:15
@(shellhelp2):1:1
初始化
> rs.initiate();
{
"info2" : "no configuration specified. Using a default configuration for the set",
"me" : "cjc-db-01:27017",
"ok" : 1
}
再次查询
cjcMongodb:SECONDARY> show dbs;
admin 0.000GB
cjcdb 0.000GB
config 0.000GB
local 0.000GB
cjcMongodb:PRIMARY> use local
switched to db local
cjcMongodb:PRIMARY> show tables;
oplog.rs
replset.election
replset.minvalid
replset.oplogTruncateAfterPoint
startup_log
system.replset
system.rollback.id
Oplog 存储在 local 库的 oplog.rs 集合里面
cjcMongodb:PRIMARY> db.oplog.rs.find();
{ "ts" : Timestamp(1675847798, 1), "h" : NumberLong("-4142341003137703290"), "v" : 2, "op" : "n", "ns" : "", "wall" : ISODate("2023-02-08T09:16:38.168Z"), "o" : { "msg" : "initiating set" } }
{ "ts" : Timestamp(1675847798, 3), "t" : NumberLong(1), "h" : NumberLong("-8128858175787158320"), "v" : 2, "op" : "n", "ns" : "", "wall" : ISODate("2023-02-08T09:16:38.174Z"), "o" : { "msg" : "new primary" } }
{ "ts" : Timestamp(1675847798, 4), "t" : NumberLong(1), "h" : NumberLong("2262279575084486449"), "v" : 2, "op" : "c", "ns" : "config.$cmd", "ui" : UUID("e1b444f6-20e3-4695-8ddb-45011cde7906"), "wall" : ISODate("2023-02-08T09:16:38.191Z"), "o" : { "create" : "image_collection", "idIndex" : { "v" : 2, "key" : { "_id" : 1 }, "name" : "_id_", "ns" : "config.image_collection" } } }
{ "ts" : Timestamp(1675847798, 5), "t" : NumberLong(1), "h" : NumberLong("-7259636297367536820"), "v" : 2, "op" : "c", "ns" : "config.$cmd", "ui" : UUID("4286a448-9182-44a6-b1fc-7ab1b0e79f1d"), "wall" : ISODate("2023-02-08T09:16:38.206Z"), "o" : { "create" : "transactions", "idIndex" : { "v" : 2, "key" : { "_id" : 1 }, "name" : "_id_", "ns" : "config.transactions" } } }
......
{ "ts" : Timestamp(1675847938, 1), "t" : NumberLong(1), "h" : NumberLong("-2455973444836006621"), "v" : 2, "op" : "n", "ns" : "", "wall" : ISODate("2023-02-08T09:18:58.251Z"), "o" : { "msg" : "periodic noop" } }
......
cjcMongodb:PRIMARY>
cjcMongodb:PRIMARY>
查看Oplog状态
cjcMongodb:PRIMARY> rs.printReplicationInfo()
configured oplog size: 2654.523241996765MB
log length start to end: 550secs (0.15hrs)
oplog first event time: Wed Feb 08 2023 17:16:38 GMT+0800 (CST)
oplog last event time: Wed Feb 08 2023 17:25:48 GMT+0800 (CST)
now: Wed Feb 08 2023 17:25:51 GMT+0800 (CST)
###chenjuchao 20230208###
欢迎关注我的公众号《IT小Chen》