0

I want to use MongoDB in replica-set mode over 2 servers by IPs.But when I deployed the secondary one on other server, I faced the issues about connecting between 2 instance. For example, the primary located on server 192.168.1.1:27018 with hostname COM1 and the second located on server 192.168.1.2:27018 with hostname COM2. Despite of configuring like this

net:
    bindIp: 0.0.0.0, COM1|192.168.1.1, COM2|192.168.1.2
    port: 27018

But in MongoDB's log, it responses that cant hear from COM1:27018 or can find COM2:27018, I don't know how to map these addresses with these IPs. Please help me, thanks!

  • The bindIp directive specifies the network interface(s) mongod should listen for connections on: those IP addresses have to be local to the mongod environment. It looks like you are trying to specify remote IPs and hostname mapping for other members of your replica set -- this syntax is not valid. Hostname to IP mapping is handled by standard DNS resolution, which is configured in your O/S and not part of the MongoDB server configuration. There are plenty of tutorials for setting up DNS, but if you are having trouble finding details Server Fault would be more appropriate for questions. – Stennie Apr 2 at 10:50
  • Since the MongoDB configuration syntax you've provided should result in a startup error, I'm guessing the errors you mention in the MongoDB log relate to the hostnames you used when creating your replica set config. If the names COM1 and COM2 are not resolvable, you can use rs.reconfig() to update your replica set configuration using IPs instead of hostnames. If you are still having difficulty, please edit your question to include the output of rs.conf(). – Stennie Apr 2 at 10:56
0

From your primary machine run the following command

mongo --host COM2 --port 27018 

You should be able to connect to the MongoDB running in the secondary node using the above command. If it fails to connect, check the following configuration settings

  1. Check the bindIp settings of the secondary node configuration file, it should allow connection from 192.168.1.1:27018.
  2. Check the port number 27018 is accepting inbound connections in the secondary node. If not add an entry in IP Table.
  3. Check if secondary nodes hosts file is updated with primary machines DNS name and IP Address.

Follow the same for Primary node as well and try to connect to each machine from other machine using the first command. If everything working fine you should be able to set up your replica set.

0

Replica Set in MongoDB by using IPs instead of domains?

As per MongoDB documentation here To override and bind to other ip addresses, you can use the net.bindIp configuration file setting or the --bind_ip command-line option to specify a list of hostnames or ip addresses.

For example, the following mongod instance binds to both the localhost and the hostname My-Example-Associated-Hostname, which is associated with the ip address 198.51.100.1:

mongod --bind_ip localhost,My-Example-Associated-Hostname

In order to connect to this instance, remote clients must specify the hostname or its associated ip address 198.51.100.1:

mongo --host My-Example-Associated-Hostname

mongo --host 198.51.100.1

Ensure that network traffic can pass securely between all members of the set and all clients in the network .

Consider the following:

  • Establish a virtual private network. Ensure that your network topology routes all traffic between members within a single site over the local area network.
  • Configure access control to prevent connections from unknown clients to the replica set.
  • Configure networking and firewall rules so that incoming and outgoing packets are permitted only on the default MongoDB port and only from within your deployment. See the IP Binding considerations.

WARNING NOTE: Before binding to a non-localhost (e.g. publicly accessible) IP address, ensure you have secured your cluster from unauthorized access. For a complete list of security recommendations, see Security Checklist. At minimum, consider enabling authentication and hardening network infrastructure.

If you will specifies the replica set name and the ip binding through the --replSet and --bind_ip command-line options:

mongod --replSet "rs0" --bind_ip localhost,<hostname(s)|ip address(es)>

For , specify the hostname(s) and/or ip address(es) for your mongod instance that remote clients (including the other members of the replica set) can use to connect to the instance.

Alternatively, you can also specify the replica set name and the ip addresses in a configuration file:

replication:
   replSetName: "rs0"
net:
   bindIp: localhost,<hostname(s)|ip address(es)>

Note: Changed in version 3.6: Starting in MongoDB 3.6, MongoDB binaries, mongod and mongos, bind to localhost by default. If the net.ipv6 configuration file setting or the --ipv6 command line option is set for the binary, the binary additionally binds to the localhost IPv6 address.

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.