Replication

mongodb 複製集時無法執行 rs.initiate()

  • August 7, 2018

使用 CentOS 7。

MongoDB 版本:3.2.15

主機名:node1

製作目錄:

mkdir /mongo-metadata

/etc/mongod.conf

# mongod.conf

# for documentation of all options, see:
#   http://docs.mongodb.org/manual/reference/configuration-options/

# where to write logging data.
systemLog:
 destination: file
 logAppend: true
 path: /var/log/mongodb/mongod.log

# Where and how to store data.
storage:
 dbPath: /mongo-metadata
 journal:
   enabled: true
#  engine:
#  mmapv1:
#  wiredTiger:

# how the process runs
processManagement:
 fork: true  # fork and run in background
 pidFilePath: /var/run/mongodb/mongod.pid  # location of pidfile

# network interfaces
net:
 port: 27017
 bindIp: 127.0.0.1  # Listen to local interface only, comment to listen on all interfaces.


#security:

#operationProfiling:

replication:
 replSetName: rs0

#sharding:

## Enterprise-Only Options

#auditLog:

#snmp:

啟動複製成員:

mongod --config /etc/mongod.conf

執行mongo

mongo
# try
rs.initiate()    
{
       "info2" : "no configuration specified. Using a default configuration for the set",
       "me" : "node1:27017",
       "ok" : 0,
       "errmsg" : "No host described in new configuration 1 for replica set rs0 maps to this node",
       "code" : 93
}

# try
config = {
   _id : "rs0",
    members : [
        {_id : 0, host : "node1:27017"},
        {_id : 1, host : "node2:27017"},
    ]
}
rs.initiate(config)
{
   "ok" : 0,
   "errmsg" : "No host described in new configuration 1 for replica set rs0 maps to this node",
   "code" : 93
}

參考:

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

那麼是什麼原因呢?它無法辨識主機名?

答案很簡單!您的 /etc/hosts 文件中沒有定義“node1”。您的機器不知道“node1”(或“node2”)的 IP 地址。

echo "127.0.0.1  node1" >> /etc/hosts

然後是另一件事,您已經定義(在配置文件中)您的 mongod 只監聽 localhost。所以這意味著你的整個副本集必須在那台機器上,如果是這樣,所有這三個成員都不能使用相同的埠 27017,每個成員必須有不同的埠。

對於我的情況:替換127.0.0.1localhost. 然後它工作。

這是rsconf

rsconf = {
 _id: 'rs0',
 members: [
   {
     _id: 0,
     host: 'localhost:27017'
   },
   {
     _id: 1,
     host: 'localhost:27018'
   },
   {
     _id: 2,
     host: 'localhost:27019'
   }
 ]
};

引用自:https://dba.stackexchange.com/questions/180189