0

On CentOS 7.

MongoDB version: 3.2.15

There are two nodes:

  • node1
  • node2

node1

/etc/hosts

127.0.0.1   localhost localhost.localdomain localhost4 localhost4.localdomain4 node1
::1         localhost localhost.localdomain localhost6 localhost6.localdomain6

192.168.0.100              node1
192.168.0.101              node2

# 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 192.168.0.100  # Listen to local interface only, comment to listen on all interfaces.


security:
  keyFile: /root/keyfile

#operationProfiling:

replication:
  replSetName: rs0

#sharding:

## Enterprise-Only Options

#auditLog:

#snmp:

node2

/etc/hosts

127.0.0.1   localhost localhost.localdomain localhost4 localhost4.localdomain4 node2
::1         localhost localhost.localdomain localhost6 localhost6.localdomain6

192.168.0.100              node1
192.168.0.101              node2

/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 192.168.0.101  # Listen to local interface only, comment to listen on all interfaces.


security:
  keyFile: /root/keyfile

#operationProfiling:

replication:
  replSetName: rs0

#sharding:

## Enterprise-Only Options

#auditLog:

#snmp:

Run mongo command on node1

mongo
rs.initiate()
use admin
db.createUser(...)
db.auth('admin', '...')
rs.add('node2')

Caused error:

{
    "ok" : 0,
    "errmsg" : "Quorum check failed because not enough voting nodes responded; required 2 but only the following 1 voting nodes responded: node1:27017; the following nodes did not respond affirmatively: node2:27017 failed with No route to host",
    "code" : 74
}

Is it can't connect to node2 from node1? Is there a official guide about how to do mongodb replication set?

  • The net.bindIp value should be a comma-separated list if you want to provide multiple values. Try: bindIp: 127.0.0.1,192.168.0.101. There may also be an issue with firewall configuration. The MongoDB documentation has a good guide to security measures, including examples of firewall configuration: Security Checklist. – Stennie Jul 19 '17 at 6:43
  • @Stennie Thank you. Have fixed the issue. Added iptables rule. – cloud_cloud Jul 19 '17 at 7:20
1

Answer is simple. You have not open your firewall for port 27017, is must be done on both nodes.

And asked documentation can be found here!

| improve this answer | |
  • Thank you. That's the reason. I add this: iptables -A INPUT -p tcp --dport 27017 -j ACCEPT and save the iptables: service iptables save on each node. But still the same error. I tried to ping ping node2:27017 from node1, got error: Don't know the name or the service. – cloud_cloud Jul 19 '17 at 6:57
  • telnet node2 27017 should give you answer, if port is open AND there is mongod waiting.. Remove bindIp -line from config, so mongod listen all interfaces – JJussi Jul 19 '17 at 6:59
  • 1
    Check your iptables. Run command iptables-save and check that your ACCEPT rule of port 27017 is NOT after DROP rule what would catch that 27017 – JJussi Jul 19 '17 at 7:01
  • 1
    After I flush all chains of iptables: iptables -F, and add iptables -A INPUT -p tcp --dport 27017 -j ACCEPT, saved it: service iptables save. It works! Thank you very much for your kindly help! – cloud_cloud Jul 19 '17 at 7:13

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.