14

I've installed mongodb then I've created a mongo service:

 [Unit]
    Description=High-performance, schema-free document-oriented database
    After=network.target

    [Service]
    User=mongodb
    ExecStart=/usr/bin/mongod --quiet --config /etc/mongod.conf

    [Install]
    WantedBy=multi-user.target

But When I launch the service and then I check the status, I get always this error:

● mongodb.service - High-performance, schema-free document-oriented database
   Loaded: loaded (/etc/systemd/system/mongodb.service; enabled; vendor preset: enabled)
   Active: failed (Result: exit-code) since Mon 2017-04-24 13:08:55 UTC; 6min ago
  Process: 1094 ExecStart=/usr/bin/mongod --quiet --config /etc/mongod.conf (code=exited, status=48)
 Main PID: 1094 (code=exited, status=48)

Apr 24 13:08:54 ip-172-31-37-163 systemd[1]: Started High-performance, schema-free document-oriented database.
Apr 24 13:08:55 ip-172-31-37-163 systemd[1]: mongodb.service: Main process exited, code=exited, status=48/n/a
Apr 24 13:08:55 ip-172-31-37-163 systemd[1]: mongodb.service: Unit entered failed state.
Apr 24 13:08:55 ip-172-31-37-163 systemd[1]: mongodb.service: Failed with result 'exit-code'.
Chlebta
  • 3,090
  • 15
  • 50
  • 99

5 Answers5

18

The problem was in config file and changing

bindIp: 127.0.0.1, X.X.X.X

to

bindIp: [127.0.0.1, X.X.X.X]

Solved my issue

Chlebta
  • 3,090
  • 15
  • 50
  • 99
  • 1
    Similar issue appears here https://stackoverflow.com/questions/30884021/mongodb-bind-ip-wont-work-unless-set-to-0-0-0-0 . – Rafael Valero Apr 04 '18 at 16:56
4

Try changing the owner permissions

chown -R mongodb:mongodb /var/lib/mongodb

chown mongodb:mongodb /tmp/mongodb-27017.sock

Savio
  • 101
  • 5
2

In MongoDB 3.6 the brackets proposed by @Chlebta did not work for me, returning the error:

mongod.service: Main process exited, code=exited, status=2/INVALIDARGUMENT

My mistake was separating the IP addresses with commas and spaces. There has to be only commas between the addresses:

bindIp: 127.0.0.1,X.X.X.X,Y.Y.Y.Y
Rodrigo Pinto
  • 2,384
  • 22
  • 23
  • There is no capital 'P' to bindIp. Even with that correction I'm still stuck. – François Guthmann Feb 03 '18 at 12:25
  • True. Misspelling fixed, thanks. Also, look for tabs in the file. The conf file only works with spaces. – Rodrigo Pinto Feb 03 '18 at 17:19
  • I'll check that. I think the issue is that those IP need to be on the same network and I'm trying to allow an external IP access to the database. – François Guthmann Feb 03 '18 at 18:37
  • Ok. It should be possible. Use the IP address of the machine where you are editing the conf file, not the addresses of the external machines. I have 3 servers in Digital Ocean that communicate between them through VPN, so I used the private IP. – Rodrigo Pinto Feb 03 '18 at 19:26
  • Hmm that's what I'm doing already. But writing the IP of the machine I'm editing the conf file on is pretty much the same as writing 127.0.0.1, right? Here is my full problem. I have a machine with an Express/Nodejs server and a MongoDB/Mongod daemon installed on it. If I write `BindIp : www.myAddress.com` in the conf file the server talks to the data base just fine. What I want to be able to do is browse my database with Mongo Compass from another computer without the `bindAllIp : true` parameter. – François Guthmann Feb 04 '18 at 11:03
  • It is not the same as writing 127.0.0.1. BindIp is the address through which other servers will see your MongoDB server. In your case, if your Express server is on the same machine as your MongoDB, leave BindIp with 127.0.0.1 and make Express connect to that. If Express and MongoDB are in separate machines, put in BindIp the address of the MongoDB server. I browse my db remotely with Robo 3T. It first connects to the server via ssh and then connects to MongoDB asking for localhost. It does not need anything from BindIp. I don't know if Mongo Compass can do that. – Rodrigo Pinto Feb 05 '18 at 18:12
  • Hmm I see. I don't need Compass specifically, Robo3T seems good enough. How does Robo3T access the database through the server? I mean, what did you configure on the server for it to work? – François Guthmann Feb 06 '18 at 10:40
  • Fill the tabs you need in the connection settings window. In "Connection" set `Address: localhost:27017`. Fill "Authentication" with the credentials to access your db. Fill "SSH" with the same parameters you use to access your server from the terminal. I use a private key from my .ssh folder. Let me know if you are stuck. Bonne chance! – Rodrigo Pinto Feb 06 '18 at 21:36
  • Haaaaan that's how it's done? I had no idea the software could handle the SSH connection on its own. That's great, I have to try this. PS: Robo3T is way better than Compass, changed my life already. Cheers dude! – François Guthmann Feb 06 '18 at 23:50
  • Yeah. Open software FTW! I'm glad I could help. Best of luck. – Rodrigo Pinto Feb 07 '18 at 04:08
1

Update for Ubuntu 20.04 and MongoDB server version 4.4.2...

The issue for me ended up being that I was trying to use my machine's public IP address.

Once I specified my private IP address, everything started working. The mongod service starts without any errors, and I am now able to connect to it from remote machines.

On Ubuntu, you can use the following command to get your private IP address:

hostname -I | awk '{print $1}'

Here's what I have in /etc/mongod.conf (only showing relevant sections, and partial private IP address):

net:
  port: 27017
  bindIp: 127.0.0.1, 172.##.##.##

security:
  authorization: enabled

Note that putting brackets around the two IP addresses (as in the accepted answer) did NOT work for me, and resulted in a mongod status code of 2 (effectively a syntax error in the mongod.conf file). Also the space after the comma seems to work now, though not required.

MTS
  • 1,845
  • 2
  • 17
  • 18
-4

I just reboot system (sudo reboot) it is working for me. i don't know why.

Killman
  • 1
  • 1