Fix: MongoDB Service Crash WSL2

Fix for the MongoDB service crashes or not starting after installation on WSL2

Issue Background

While working on full-stack application development on Windows machines, I prefer using WSL2 as my development environment host. While following the installation of the MongoDB service on WSL2 for local development I faced an issue with the mongod service. The service was crashing while I was executing the start command with the following error.

MongoDB Crash Error log

mongod.service - MongoDB Database Server
   Loaded: loaded (/lib/systemd/system/mongod.service; enabled; vendor preset: enabled)
   Active: failed (Result: exit-code) since Wed 2020-02-19 22:39:00 CET; 2s ago
     Docs: https://docs.mongodb.org/manual
  Process: 29368 ExecStart=/usr/bin/mongod --config /etc/mongod.conf (code=exited, status=14)
 Main PID: 29368 (code=exited, status=14)

Feb 19 22:39:00 pop-os systemd[1]: Started MongoDB Database Server.
Feb 19 22:39:00 pop-os systemd[1]: mongod.service: Main process exited, code=exited, status=14/n/a
Feb 19 22:39:00 pop-os systemd[1]: mongod.service: Failed with result 'exit-code'.
  • One of the main reasons for the above error is related to the incorrect permission set for the pid file that the mongod service uses to maintain the pid for the running process.

Solution for the Permission Issue

Find out the config that the MongoDB service is using

Use the systemctl command. this command will give the current status of the service.

$ systemctl status mongod.service # if required use sudo.
 mongod.service - MongoDB Database Server
   Loaded: loaded (/lib/systemd/system/mongod.service; enabled; vendor preset: enabled)
   Active: failed (Result: exit-code) since Wed 2020-02-19 22:39:00 CET; 2s ago
     Docs: https://docs.mongodb.org/manual
  Process: 29368 ExecStart=/usr/bin/mongod --config /etc/mongod.conf (code=exited, status=14)
 Main PID: 29368 (code=exited, status=14)

Feb 19 22:39:00 pop-os systemd[1]: Started MongoDB Database Server.
Feb 19 22:39:00 pop-os systemd[1]: mongod.service: Main process exited, code=exited, status=14/n/a
Feb 19 22:39:00 pop-os systemd[1]: mongod.service: Failed with result 'exit-code'.

In the above output ExecStart is the command that has the configuration file that we need to check

Find the path of the pid file

  • Get the content of the file that we got in the above step. (/etc/mongod.conf).

  • From the file, we need to find a block like below

processManagement:
  pidFilePath: /var/run/mongod.pid # This is the pid file for mongod process
  timeZoneInfo: /usr/share/zoneinfo
  • Apart from the processManagement section, the file also contains the configuration for storage, systemLog, net(network). We can update those according to our needs. However, that is not in the scope of this article.

Check and Create a pid file with proper permission

  • now check the file at pidFilePath the location. If the file is already there, mongodb user should have access to and ownership of the file. Follow the below steps as per the instructions.
$ sudo touch /var/run/mongod.pid # Do not create a file if it already exists. if directory is not there, create a directory as well
$ sudo chown mongodb:mongodb /var/run/mongod.pid # Give file ownership to mongodb user.
$ sudo systemctl start  mongod.service # This will start the service. Now the service should be running as expected

Conclusion

This is just a solution for the issue with pid file permission. However, if from the logs you found out that the permission issue is there on logFile or the dataFile you can follow the above process to fix those issues as well.

Did you find this article valuable?

Support Rajendrasinh Parmar by becoming a sponsor. Any amount is appreciated!