Getting MongoDB on Linux to listen to remote connections
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/7159737/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Getting MongoDB on Linux to listen to remote connections
提问by user315648
I've successfully installed MongoDB on Windows (on a local machine) as a service, but now I want to move MongoDb to a separate server. So I extracted the tarball to a virtual server on network (running linux).
I've successfully installed MongoDB on Windows (on a local machine) as a service, but now I want to move MongoDb to a separate server. So I extracted the tarball to a virtual server on network (running linux).
When I connected to the server ("testmongoserver") using PuTTY from my local machine, I started the mongod server and it told me that it was listening to the default 28017 port. The mongo console is also working and allowed me to create a new database (testdb) and add users to it.
When I connected to the server ("testmongoserver") using PuTTY from my local machine, I started the mongod server and it told me that it was listening to the default 28017 port. The mongo console is also working and allowed me to create a new database (testdb) and add users to it.
However, I could not access the server from remote. When I type testmongoserver:28017
it doesn't open the HTTP console as localhost:28017
on my local machine does. I also can't connect using official drivers and providing a connectionstring.
However, I could not access the server from remote. When I type testmongoserver:28017
it doesn't open the HTTP console as localhost:28017
on my local machine does. I also can't connect using official drivers and providing a connectionstring.
What are the neccesarry steps to install MongoDB on Linux, so that I could access it from a remote machine with a connectionstring and use its HTTP console via testmongoserver:28017
What are the neccesarry steps to install MongoDB on Linux, so that I could access it from a remote machine with a connectionstring and use its HTTP console via testmongoserver:28017
Thanks!
Thanks!
采纳答案by Andrej Ludinovskov
- Run netstat -a on mongo server and check a port.
- Check DNS settings and check that linux server allows external connections.
- Check that mongodb can accept external/remote connection.
- Run netstat -a on mongo server and check a port.
- Check DNS settings and check that linux server allows external connections.
- Check that mongodb can accept external/remote connection.
Default port for mongo is 27017. 28017 - port for webstats.
Default port for mongo is 27017. 28017 - port for webstats.
See http://www.mongodb.org/display/DOCS/Security+and+Authentication#SecurityandAuthentication-Ports
See http://www.mongodb.org/display/DOCS/Security+and+Authentication#SecurityandAuthentication-Ports
回答by Yves M.
1. Bind IP option
1. Bind IP option
Bind IP is a MongoDB option that restricts connections to specifics IPs.
Bind IP is a MongoDB option that restricts connections to specifics IPs.
Have a look at your mongod configuration file, most of the time bind_ipis set to 127.0.0.1
for obvious security reasons. You can:
Have a look at your mongod configuration file, most of the time bind_ipis set to 127.0.0.1
for obvious security reasons. You can:
- Add your desired IP by concatenating a list of comma separated values to bind MongoDB to multiple IP addresses.
- Remove or comment (with
#
character) thebind_ip
line. But be aware that all remote connection will be able to connect your MongoDB server!
- Add your desired IP by concatenating a list of comma separated values to bind MongoDB to multiple IP addresses.
- Remove or comment (with
#
character) thebind_ip
line. But be aware that all remote connection will be able to connect your MongoDB server!
More about bind_ip
configuration option: https://docs.mongodb.com/manual/reference/configuration-options/#net.bindIp
More about bind_ip
configuration option: https://docs.mongodb.com/manual/reference/configuration-options/#net.bindIp
Bind IP can also be set as a command argument: http://docs.mongodb.org/manual/reference/program/mongod/#cmdoption--bind_ip
Bind IP can also be set as a command argument: http://docs.mongodb.org/manual/reference/program/mongod/#cmdoption--bind_ip
2. Firewall
2. Firewall
Check that you are not running behind a firewall
Check that you are not running behind a firewall
回答by victorkurauchi
Make sure in your /etc/mongodb.conf
file you have the following line,
Make sure in your /etc/mongodb.conf
file you have the following line,
bind_ip = 0.0.0.0
http://jitu-blog.blogspot.com.br/2013/06/allow-mongo-to-connect-from-remote-ip.html
http://jitu-blog.blogspot.com.br/2013/06/allow-mongo-to-connect-from-remote-ip.html
回答by J.C. Gras
Another problem may be that the mongodb port is not enabled. Check, from another host, the ports enabled on your server. For that you can use the command:
Another problem may be that the mongodb port is not enabled. Check, from another host, the ports enabled on your server. For that you can use the command:
sudo nmap -P0 your_server_ip
You can get an answer like this:
You can get an answer like this:
Host is up (0.052s latency).
Not shown: 997 filtered ports
PORT STATE SERVICE
22/tcp open ssh
80/tcp open http
443/tcp closed https
If you use a virtual server in the cloud, as AWS, you need to add a new rule to add mongodb port (27017 by default).
If you use a virtual server in the cloud, as AWS, you need to add a new rule to add mongodb port (27017 by default).
Important: Note that with this configuration anyone can have access to your database
Important: Note that with this configuration anyone can have access to your database
回答by John Culviner
Just had this issue and this fixed it:
Just had this issue and this fixed it:
Edit /etc/mongod.conf
with sudo nano /etc/mongod.conf
ensure that the net section looks like below (localhost binding by default doesn't allow for remote access):
Edit /etc/mongod.conf
with sudo nano /etc/mongod.conf
ensure that the net section looks like below (localhost binding by default doesn't allow for remote access):
# network interfaces
net:
port: 27017
bindIp: 0.0.0.0
Make sure to restart mongod when you are done with above with below (assuming systemd ubuntu 16.04+ etc.):
Make sure to restart mongod when you are done with above with below (assuming systemd ubuntu 16.04+ etc.):
sudo service mongod restart
sudo service mongod restart
Obviously from a security perspective if you are going to be opening up mongo to your network/the world be aware of the implications of this (if any)
Obviously from a security perspective if you are going to be opening up mongo to your network/the world be aware of the implications of this (if any)