简体   繁体   中英

Connecting to remote mongo server

I am having problems in connecting to an instance of mongodb running on a remote server. lets say i have a server A (123.45.67:27017) that runs a mongo db on port 27017. Then when i try to connect from my own machine B by typing:

B$: mongo A:27017

I will get an error

Failed to connect to A:27017, in(checking socket for error after poll), reason: Connection refused

What i did for trouble shooting:

  1. Made sure the port 27017 is open on the server A. Tested using nc -l 27017 and it works - i can type over to B.
  2. Connected successfully to the database by using a ssh tunnel to bind the 27017 port to localhost: ssh -f -L 27017:localhost:27017 myUserName@A sleep 10; mongo localhost:27017 ssh -f -L 27017:localhost:27017 myUserName@A sleep 10; mongo localhost:27017

So in short, the database runs, the port is open and i can use the database when bound to local port. Anyone can help?

Your server probably isn't listening other than on localhost.

In mongod.conf (probably /etc/mongod.conf )

# network interfaces
net:
  port: 27017
  bindIp: 0.0.0.0  # listen on all interfaces

Then restart the mongod service.

You need to authorize a remote connexion with mongo. By default you can access only from the same IP (localhost)

If I remember right, you should look inside the conf file sudo vim /etc/mongod.conf And look for a line call security . It should be uncommented and with the value enable

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM