MongoDB won’t connect remotely

amazon ec2mongodbremote-access

I have installed mongoDB on a redhat virtual machine on EC2.

The server is running perfectly locally, I can connect and execute commands, etc.
Now, I tried connecting remotely from another virtual machine on EC2 but I can't get to connect regardless how much I'm trying. At first I thought it was a problem of security groups, so I tried enabling all ports from within the group (both virtual machines belong to the same secirity groups) I also enabled connections in the iptables file (and yes, restarted the service) but it still doesn't work… Any ideas on what the problem could be?

the error I get is:

MongoDB shell version: 2.0.2
connecting to: ec2-XXXX.eu-west-1.compute.amazonaws.com/test
Thu Jan  5 13:55:32 Error: couldn't connect to server ec2-XXXX.eu-west-1.compute.amazonaws.com shell/mongo.js:86
exception: connect failed

Thank in advance.

Regarding the "bind_ip" setting, well.. there is no default value for that, I mean there even is no bind_ip line on the mongodb.conf, by default. I was probably expecting something like bind_ip = 127.0.0.1 so that I could comment it out, but it wasn't there, so I tried with bind_ip = 0.0.0.0 but still, it's not working either.

Best Answer

Hopefully this helps. You need to explicitly tell mongodb to listen on non-localhost devices. By default it runs only on localhost.

From http://www.mongodb.org/display/DOCS/Security+and+Authentication:

IP Address Binding By default, a mongod server will listen on all available IP addresses on a machine. You can restrict this to a single IP address with the 'bind_ip' configuration option for mongod.

Typically, this would be set to 127.0.0.1, the loopback interface, to require that mongod only listen to requests from the same machine (localhost).

To enable listening on all interfaces, remove the bind_ip option from your server configuration file.

Update: I guess I was confused myself. I thought the default behavior was different than what was described above. (Maybe it used to be?) In any case, you should check your mongo config file to make sure that you have it configured as you expect.

Related Topic