首页 > 解决方案 > MongoDB replica set initiate

问题描述

I am quite new to MongoDB and have following question for replica set initiation.

When I tried to initiate a replica set from standalone mongod, I tried:

> use admin
> config = {id: "rs_name", members: [{id: 0, host: "127.0.0.1:27017"}]}
> db.runCommand('replSetInitiate', config)

"info2" : "no configuration specified. Using a default configuration for the set",
"ok": 1
...

Here is the first curiosity: Why did it show "no configuration" as I already assigned "config" as replica set configuration?

Though replica set was founded, when I tired:

> use local
> db.system.replset.find().pretty()

...
"members": [
   {
      "host": "here is hostname insted of host IP"
   }
...

Here is the second ponit I didn't get: Why did the replica information show host name instead of host IP as value of property "host"?

But When I tried a different way:

> use admin
> config = {id: "rs_name", members: [{id: 0, host: "127.0.0.1:27017"}]}
> rs.initiate(config)

It went well and did not give any message like "no configuration specified". Replica information also showed host IP instead of host name.

Here is the last one: What is the difference between mechanism of replSetInitiate and rs.initiate when we try to initiate a replica set?

标签: mongodbreplicaset

解决方案


配置 = {id:“rs_name”,成员:[{id:0,主机:“127.0.0.1:27017”}]}

  1. 将 id 替换为 _id

db.runCommand('replSetInitiate', 配置)

  1. db.runCommand({replSetInitiate : config})改为使用

这是我没有得到的第二个要点:为什么副本信息显示主机名而不是主机 IP 作为属性“主机”的值?

  1. 正如 mongodb 文档所描述的那样When possible, use a logical DNS hostname instead of an ip address, particularly when configuring replica set members or sharded cluster members. 。在第一步中,您得到了no configuration specified. Using a default configuration for the set,默认配置可能使用主机名而不是 IP 地址。

来自:https ://docs.mongodb.com/manual/reference/command/replSetInitiate/


推荐阅读