简体   繁体   中英

enable remote access for mongoDB server

I have to linux servers running on the same local network. One of them is running mongoDB server. I am trying to connect to the first server mongodb from the second server. I have added port 27017 to the first server firewall rules. I have modified the /etc/mongo.conf file as follows: bind_ip=127.0.0.1,10.0.0.202

That did not work. I have also tried the next version: bind_ip=[127.0.0.1,10.0.0.202]

That did not work as well. After modifying the file I am trying to restart the mongod service but the service won't restart. It will only with the original line: bind_ip=127.0.0.1 .

Here is the error once i restart the service and check the status:

    mongod.service - SYSV: Mongo is a scalable, document-oriented database.
   Loaded: loaded (/etc/rc.d/init.d/mongod)
   Active: failed (Result: exit-code) since Sun 2016-11-13 11:32:15 IST; 4min 58s ago
     Docs: man:systemd-sysv-generator(8)
  Process: 37572 ExecStop=/etc/rc.d/init.d/mongod stop (code=exited, status=0/SUCCESS)
  Process: 37546 ExecStart=/etc/rc.d/init.d/mongod start (code=exited, status=0/SUCCESS)
 Main PID: 37559 (code=exited, status=48)

Nov 13 11:32:15 localhost.localdomain systemd[1]: Starting SYSV: Mongo is a scalable, document-oriented database....
Nov 13 11:32:15 localhost.localdomain runuser[37555]: pam_unix(runuser:session): session opened for user mongod...d=0)
Nov 13 11:32:15 localhost.localdomain mongod[37546]: Starting mongod: [  OK  ]
Nov 13 11:32:15 localhost.localdomain systemd[1]: Started SYSV: Mongo is a scalable, document-oriented database..
Nov 13 11:32:15 localhost.localdomain systemd[1]: mongod.service: main process exited, code=exited, status=48/n/a
Nov 13 11:32:15 localhost.localdomain mongod[37572]: Stopping mongod: [FAILED]
Nov 13 11:32:15 localhost.localdomain systemd[1]: Unit mongod.service entered failed state.
Nov 13 11:32:15 localhost.localdomain systemd[1]: mongod.service failed.

What am i doing wrong? How do i fix it?
Any help would be appreciated. Thank you.

Problem solved. Apparently when adding an IP to the bind_ip=127.0.0.1 line, the next IP should be the one of that same machine. If the IP of the machine running the mongo server is 10.0.0.201, so we should change the line into bind_ip=127.0.0.1,10.0.0.201 . This way other machines that are on the same network will be able to connect to its mongo server.

Set bindIp as 0.0.0.0 to let the mongo db served bind to all interfaces:

# network interfaces
net:
  port: 27017
#  bindIp: 127.0.0.1
  bindIp: 0.0.0.0

For someone who is using Amazon AWS for instance, bind local IP port and not public IP port. Than You allow port 27017 in your Security Group to allow it for incoming connections.

/etc/mongod.conf

# network interfaces
net:
  port: 27017
  bindIp: 127.0.0.1,172.33.1.10

Do not forget to secure it by creating user:

> show dbs
admin       135 kB
config      111 kB
local      73.7 kB

> db.createUser({
    user: "LetMeIn",
    pwd:  "MyStrongPsswd",
    roles: [{role: "userAdminAnyDatabase" , db: "admin"}]
});
{ ok: 1 }

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