MongoDB bind_ip won't work unless set to 0.0.0.0

后端 未结 12 780
遇见更好的自我
遇见更好的自我 2020-12-04 23:43

I really tried, even reinstall the MongoDB.

And it\'s the same to MongoDB bind_ip error: bind() failed errno:99 Cannot assign requested address for socket

It

相关标签:
12条回答
  • 2020-12-05 00:29

    For those who still wondering - problem is not in the syntax, but the addresses you put in.

    In order to receive the remote client connections you need to add the server public IP as well. So you must add:

     - localhost
     - 127.0.0.1 // Add both localhost and 127.0.0.1 to ensure local accessibility
     - server_public_ip  // This is important one. Add the public server IP address.
     - remote_client_ip1
     - remote_client_ip2
     - remote_client_ip3 // As many client IPs as you want to grant access to
    

    So the configuration should look like this:

    bind_ip=[localhost,127.0.0.1,server_public_ip,remote_client_ip]

    where we put both 127.0.0.1 and localhost to ensure local accessibility, because different configurations can work with only one of the settings.

    Note: bind_ip accepts multiple syntaxes. If you have syntax error mongodb service will not run. You can check it with service mongodb status.

    DON'T ever place 0.0.0.0 as you can be a victim of one of the regular RANSOME attacks happening every few months. Read about it

    0 讨论(0)
  • 2020-12-05 00:32

    Mongo 3.6.2 Community

    The solution for me was to edit the section of /etc/mongod.conf

    # network interfaces
    net:
      port: 27017
      bindIp: 127.0.0.1,192.168.1.240   # No brackets, No spaces, only comma separated
    
    #security
    

    Then save and do this to restart and verify the service:

    > service mongod restart
    > service mongod status
    

    No failure here, now verify that someone is listening:

    > netstat -a |grep :27017
    
    tcp   0   0  yourhostname:27017   0.0.0.0:*     LISTEN
    tcp   0   0  localhost:27017      0.0.0.0:*     LISTEN
    

    Now connect using your favorite Mongo tools or command line.

    Some results of different formatting in /etc/mongod.conf

    • comma and space results in only the first IP being bound.
    • space only separator results in only the first IP being bound
    • [ ] surround results in failure to start mongod
    0 讨论(0)
  • 2020-12-05 00:36

    I spent hours beating my head against a wall with this issue. Eventually, looking at logs and googling what I found THERE got me somewhere (all I got when googling 'mongo bindIp multipl' (etc) was a load of pages like this one with answers that didn't help). First, the block in /etc/mongod.conf that worked for me was:

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

    No spaces, no quotes, no brackets... but even with it correct restarting mongodb gave an error and then it refused to start. I spent hours trying various other configurations that were incorrect (which is frustrating since the correctness of this line did not actually solve the problem and I was unaware that there was another).

    I was able to solve it by deleting the mongodb socket file:

    rm /etc/mongodb-27017.sock 
    

    After this, running

    systemctl restart mongod
    

    worked without errors. The interesting thing (part of what made it really frustrating) was that during the trial and error process if I set the bindIP back to just 127.0.0.1 and restarted mongod it worked, which made me think that that line was ok and the problems were with the alternative entries/syntax I was trying. (My best guess is that something in the socket file references the ips? I'm unfamiliar with that element of coding.)

    After deleting the socket I was then able to shell into mongo like so (options required with authentication enabled):

    mongo -u admin -p password --authenticationDatabase "admin")
    

    which establishes that the 127.0.0.1 works and also to connect from my remote app (in my current scenario the nodebb testing instance I am setting up).

    0 讨论(0)
  • 2020-12-05 00:38

    work for me for ubuntu 18 and the mongo --version 4.x.xx:

    1 - in etc/mongod.conf -net add

    bindIp: "127.0.0.1,0.0.0.0"

    2 - then use pm2:

    sudo apt-get update sudo apt-get pm2

    3 - start the pm2 service to the mongod

    pm2 start mongod

    PD: you need to erase 0.0.0.0 in production scenario

    0 讨论(0)
  • 2020-12-05 00:38

    work for me for ubuntu 18 and the mongo --version 4.x.xx:

    1 - in etc/mongod.conf -net add

    bindIp: "127.0.0.1,0.0.0.0"

    2 - then use pm2:

    sudo apt-get update 
    
    sudo apt-get pm2
    

    3 - start the pm2 service to the mongod

    pm2 start mongod
    

    PD: you need to erase 0.0.0.0 in production scenario

    0 讨论(0)
  • 2020-12-05 00:43

    The documentation said

    "You may concatenate a list of comma separated values to bind mongod to multiple IP addresses."

    So, it's not true...

    0 讨论(0)
提交回复
热议问题