进行 mongodb 复制集时无法运行 rs.initiate()

clo*_*oud 6 replication mongodb centos-7

使用 CentOS 7。

MongoDB 版本:3.2.15

主机名:node1

制作目录:

mkdir /mongo-metadata
Run Code Online (Sandbox Code Playgroud)

/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:
Run Code Online (Sandbox Code Playgroud)

启动复制成员:

mongod --config /etc/mongod.conf
Run Code Online (Sandbox Code Playgroud)

运行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
}
Run Code Online (Sandbox Code Playgroud)

参考:

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

那是什么原因呢?它无法实现主机名?

小智 7

答案很简单!您的 /etc/hosts 文件中没有定义“node1”。您的机器不知道“node1”(或“node2”)的 IP 地址。

echo "127.0.0.1  node1" >> /etc/hosts
Run Code Online (Sandbox Code Playgroud)

然后其他的事情,你已经定义(在配置文件中)你的 mongod 只监听本地主机。所以这意味着你的整个副本集必须在那台机器上,如果是这样,这三个成员不能使用相同的端口 27017,每个成员必须有不同的端口。