“...I've been working since 2008 with Ruby / Ruby on Rails, love a bit of Elixir / Phoenix and learning Rust. I also poke through other people's code and make PRs for OpenSource Ruby projects that sometimes make it. Currently working for InPay...”

Rob Lacey
Senior Software Engineer, UK

I don't even dev-ops: Part 7- MongoDB Not Starting

Since December I’ve some dev-ops which is not my usual gig. Today, just running round upgrading and rebooting staging servers. Alas MongoDB didn’t come back up on reboot. My immediate thought was that it wasn’t configured to start on start up so let’s get that sorted and bring it up.

sudo systemctl enable mongod.service
sudo service mongod start

Gah…

$ mongo
MongoDB shell version v5.0.24
connecting to: mongodb://127.0.0.1:27017/?compressors=disabled&gssapiServiceName=mongodb
Error: couldn't connect to server 127.0.0.1:27017, connection attempt failed: SocketException: Error connecting to 127.0.0.1:27017 :: caused by :: Connection refused :
connect@src/mongo/shell/mongo.js:372:17
@(connect):2:6
exception: connect failed
exiting with code 1

Ok, is the database corrupt or does Ubuntu think it’s already running and it isn’t or something?

$ sudo systemctl status mongod
● mongod.service - MongoDB Database Server
     Loaded: loaded (/lib/systemd/system/mongod.service; enabled; vendor preset: enabled)
     Active: failed (Result: exit-code) since Tue 2024-02-06 08:36:19 UTC; 43s ago
       Docs: https://docs.mongodb.org/manual
    Process: 2483 ExecStart=/usr/bin/mongod --config /etc/mongod.conf (code=exited, status=2)
   Main PID: 2483 (code=exited, status=2)

Feb 06 08:36:19 178-79-140-122 systemd[1]: Started MongoDB Database Server.
Feb 06 08:36:19 178-79-140-122 mongod[2483]: Error opening config file: Permission denied
Feb 06 08:36:19 178-79-140-122 mongod[2483]: try '/usr/bin/mongod --help' for more information
Feb 06 08:36:19 178-79-140-122 systemd[1]: mongod.service: Main process exited, code=exited, status=2/INVALIDARGUMENT
Feb 06 08:36:19 178-79-140-122 systemd[1]: mongod.service: Failed with result 'exit-code'.

Ok, the /etc/mongod.conf is not readable, it’s seemingly owned by root but something in a recent upgrade means that presumably the config is read by the mongodb user rather than root. I assume.

sudo chown mongodb:mongodb /etc/mongod.conf
ls -la /etc/mongod.conf 
-rw-r-x--- 1 mongodb mongodb 615 Dec 19  2013 /etc/mongod.conf

Cool. Let’s restart the service.

sudo service mongod start

And check the status

sudo systemctl status mongod
● mongod.service - MongoDB Database Server
     Loaded: loaded (/lib/systemd/system/mongod.service; enabled; vendor preset: enabled)
     Active: active (running) since Tue 2024-02-06 08:38:03 UTC; 4s ago
       Docs: https://docs.mongodb.org/manual
   Main PID: 2792 (mongod)
     Memory: 182.8M
     CGroup: /system.slice/mongod.service
             └─2792 /usr/bin/mongod --config /etc/mongod.conf

Feb 06 08:38:03 178-79-140-122 systemd[1]: Started MongoDB Database Server.
Feb 06 08:38:03 178-79-140-122 mongod[2792]: {"t":{"$date":"2024-02-06T08:38:03.246Z"},"s":"I",  "c":"CONTROL",  "id":7484500, "ctx":"-","msg":"Environment variable MONG>

Cool. Beans and Sausages.

GPK of the Day Wrappin' RUTH