[SERVER-35838] couldn't connect to server localhost:27017, connection attempt failed Created: 27/Jun/18  Updated: 23/Jul/18  Resolved: 27/Jun/18

Status: Closed
Project: Core Server
Component/s: Networking
Affects Version/s: 3.4.15
Fix Version/s: None

Type: Bug Priority: Major - P3
Reporter: Prashant Pokhriyal Assignee: Nick Brewer
Resolution: Done Votes: 0
Labels: None
Remaining Estimate: Not Specified
Time Spent: Not Specified
Original Estimate: Not Specified

Issue Links:
Related
Operating System: ALL
Participants:

 Description   

I'm not able to copy a database. It returns me following error:

{
	"ok" : 0,
	"errmsg" : "couldn't connect to server localhost:27017, connection attempt failed",
	"code" : 6,
	"codeName" : "HostUnreachable"
}

I don't know why it says localhost, I'm connecting to mongodb server having ip address 172.31.21.200.

Output of mongo --version

MongoDB shell version v3.4.15
git version: 52e5b5fbaa3a2a5b1a217f5e647b5061817475f9
OpenSSL version: OpenSSL 1.0.2g  1 Mar 2016
allocator: tcmalloc
modules: none
build environment:
    distmod: ubuntu1604
    distarch: x86_64
    target_arch: x86_64



 Comments   
Comment by Nick Brewer [ 29/Jun/18 ]

Hi prashant.pokhriyal

In general, it is not an advisable practice to change the value of localhost in /etc/hosts - nor is it a good idea to have multiple lines for the same IP address.

Assuming that you have properly configured the 172.31.21.200 IP address on your machine, you should be able to resolve this issue by re-mapping localhost to 127.0.0.1, and then using the --host option when connecting to it with the mongo shell.

I mention the proper configuration of the IP address - you can confirm that it's configured properly via:

ip a | grep 172.31.21.200

These are Linux configuration issues, and do not demonstrate an underlying bug in MongoDB. As such, I'd ask that you please post on the mongodb-user group or Stack Overflow with the mongodb tag. A question like this involving more discussion would be best posted on the mongodb-user group.

Regards,
Nick

Comment by Prashant Pokhriyal [ 29/Jun/18 ]

@Nick Brewer,

Thanks for the response, I'm able to reproduce this issue. In my /etc/hosts if I mapped same ip address twice then it's behaving same as mentioned above.

/etc/hosts file

172.31.21.200 localhost
172.31.21.200 testing.dev

/etc/mongod.conf file

net:
port: 27017
bindIp: testing.dev

lsof shows that it is litening on localhost instead of testing.dev

> lsof -iTCP -sTCP:LISTEN | grep mongo
mongod  29472  mongodb    7u  IPv4 340303      0t0  TCP localhost:27017 (LISTEN)

Comment by Nick Brewer [ 27/Jun/18 ]

prashant.pokhriyal,

I'd ensure that you're specifying the host you wish to connect to - by default the mongo shell uses localhost. For example:

mongo --host 172.31.21.200

If you're using a non-standard port (something other than 27017), you will also need to specify the port number:

mongo --host 172.31.21.200 --port 27018

See the Mongo shell reference documentation for more information.

Please note that SERVER project is for reporting bugs or feature suggestions for the MongoDB server. For MongoDB-related support discussion please post on the mongodb-user group or Stack Overflow with the mongodb tag.

Cheers,
Nick

Generated at Thu Feb 08 04:41:10 UTC 2024 using Jira 9.7.1#970001-sha1:2222b88b221c4928ef0de3161136cc90c8356a66.