Watch keynotes and sessions from, our virtual developer conference.

Mongoc driver ping command giving false positives

Hello All,

I am writing a module to check if mongod/mongos is running on a give host-port combination. I am making use of the ping command with mongoc driver to achieve this.

My mongos server is bound to the IP of the machine and not the hostname. Lets say the IP is and hostname is myHost. In /etc/hosts, I have myHost. mongos is running on port 27020.

For me, the following command from shell gives a connection error. This is expected as my mongos is not bound to myHost. It is bound to

>mongo --port 27020 --host myHost --eval “db.adminCommand({ping: 1})”
MongoDB shell version: 3.2.22
connecting to: myHost:27020/test
2020-03-31T16:37:19.598+0530 W NETWORK [thread1] Failed to connect to, in(checking socket for error after poll), reason: errno:111 Connection refused
2020-03-31T16:37:19.598+0530 E QUERY [thread1] Error: couldn’t connect to server centostemp:27020, connection attempt failed :

exception: connect failed


But, when running the ping command using mongoc driver with host as myHost and port as 27020. It successfully connects which I don’t expect to happen. As a result, my connectivity check module is giving false positives.

I suspect there is a dns lookup happening when running the command using mongoc driver. Is there anyway to stop this from happening?


@santhanu_mukundan your description is inconsistent. There is no such thing as being able to bind to the hostname. Any daemon will bind to 1 or more specified IP addresses (or the special “all” address of Your shell output includes “Failed to connect to”, which indicates that when the shell resolves the hostname “myHost” that the resolver is returning the address That could be the result of a wide variety of configuration-related things. For instance, despite what is in your hosts file, the system may be configured to consult a DNS server before looking in the hosts file.

Also, it doesn’t make sense that you say the server is bound to, that /etc/hosts has an entry “ myHost” and that you expect the ping to fail to connect when you give it the hostname “myHost”. The shell resolving “myHost” to seems suspect given the information that you have provided.

Please confirm the address resolution configuration of your system (nslookup and dig might be helpful in this regard). You may also want to consider providing your actual hosts file, the contents of /etc/nsswitch.conf, /etc/resolv.conf (or their equivalents if you use different services) and the complete terminal output showing the server launch, the shell interaction with the server, and the ping of the server.