MongoDB replication (1)
> rs.initiate()
{
"ok" : 0,
"errmsg" : "This node was not started with the replSet option",
"code" : 76,
"codeName" : "NoReplicationEnabled"
}
啓動時沒有配置複製集,使用mongo –replSet <複製集名稱> 啓動。
> rs.initiate()
{
"ok" : 0,
"errmsg" : "assertion src/mongo/db/repl/replset_commands.cpp:274",
"code" : 8,
"codeName" : "UnknownError"
}
該行斷言失敗的原因:host不能配置爲localhost。
Assertion failure h != "localhost" src/mongo/db/repl/replset_commands.cpp 274
此時,配置複製集信息:
> config = {_id:"rs0", members:[ {_id:0, host:"127.0.0.1:27017"}]}
{
"_id" : "rs0",
"members" : [
{
"_id" : 0,
"host" : "127.0.0.1:27017"
}
]
}
> rs.initiate(config)
{ "ok" : 1 }
或者:
rs0:PRIMARY> rs.add("localhost:27017")
現在執行rs0:PRIMARY> db.oplog.rs.find()
,結果如下:
{ "ts" : Timestamp(1487582277, 1), "t" : NumberLong(1), "h" : NumberLong("1421023485009824583"), "v" : 2, "op" : "n", "ns" : "", "o" : { "msg" : "periodic noop" } }
{ "ts" : Timestamp(1487582280, 1), "t" : NumberLong(1), "h" : NumberLong("353054299917983331"), "v" : 2, "op" : "c", "ns" : "test.$cmd", "o" : { "create" : "test", "idIndex" : { "v" : 2, "key" : { "_id" : 1 }, "name" : "_id_", "ns" : "test.test" } } }
{ "ts" : Timestamp(1487582280, 2), "t" : NumberLong(1), "h" : NumberLong("3858332860662095118"), "v" : 2, "op" : "i", "ns" : "test.test", "o" : { "_id" : ObjectId("58aab4482f0f96c51f43205f"), "empno" : 100, "name" : 100, "address" : "it is a test!" } }
查看複製集狀態:
rs0:PRIMARY> rs.printReplicationInfo()
configured oplog size: 192MB
log length start to end: 811secs (0.23hrs)
oplog first event time: Mon Feb 20 2017 17:13:36 GMT+0800 (CST)
oplog last event time: Mon Feb 20 2017 17:27:07 GMT+0800 (CST)
now: Mon Feb 20 2017 17:27:11 GMT+0800 (CST)
I