Error when i launch the command "mongod" in my ubuntu terminal

When i try to follow the lecture instructions in the first lecture of Chapter01 - Basic Cluster Administration i get an error when i try to start up a mongod process by running the command: mongod

If you want to run the mongod command from Ubuntu you will have to install MongoDB.

For the Mongo University Labs there is a built in IDE that has Mongo already installed and you can run the commands from there. I believe this is the recommended way to complete the MongoDB University courses.

But i already have mongodb installed on my ubuntu and i can access to cluster m001 and i can run mongodb-compass and mongo-shell too but when i run mongod command i get that error.

Here you can see mongod status:

Look at the first screen shot it looks like you have an error “Non existent data directory” which makes it sounds like the data directory specified in the mongod.conf doesn’t exist or the mongod user doesn’t have access.

Can you please post the contents of your /etc/mongod.conf

2 Likes

This is my mongod.conf file.

Your dbPath is: /var/lib/mongodb so you will want to verify that that directory is created on your system as well as the user that is running mongod has access to that directory.

Directories
If you installed via the package manager, the data directory /var/lib/mongodb and the log directory /var/log/mongodb are created during the installation.

By default, MongoDB runs using the mongodb user account. If you change the user that runs the MongoDB process, you must also modify the permission to the data and log directories to give this user access to these directories.

When you run mongod without any parameters it will start mongod on default port 27017 and default dirpath /data/db
Since this dir is missing it failed to start

I think you installed mongod as service so yur mongod must be up and running as you confirmed from your service status
Why do you want to start another mongod?
Just issue mongo command.If you can connect it means mongod is up and running

You can start another mongod instance using mongod command by giving different port and dbpath/logpath dirs

The server status showed a state of ‘failed’ and the default data path when installing on Ubuntu from the package manager is /var/lib/mongodb as you can see in the screenshot of his mongod.conf.

Now it work when i use the command with and extra params for dbpath and with sudo : sudo mongod --dbpath /var/lib/mongodb

1 Like

Since db path is in your config file, it should also work if you run:

sudo mongod --config /etc/mongod.conf

this will try to start mongod with the config file and all the parameters specified. This way if you change any settings in your config file they will be reflected when you restart mongodb with the above command.

1 Like

It is not recommended to run mongod as sudo
If you check dbpath you will see all files will be owned by root
They should be owned by mongod user
You will have issues when you start mongod as normal user or thru service

There is a difference in running mongod with and without config file
My response was specific to the command that was issued
When you run the command mongod without any params/arguments it tries to bring up mongod on default dirpath /data/db and port 27017.These are hardcoded in mongodb binaries
On all Unix systems /data/db is the default dirpath(when not using config file or command line param dbpath) and C:\data\db on Windows
Had the dir /data/db existed mongod would have started without any issue (i,e without sudo)

We should understand why it failed to start as service
May be the dirs under /var/lib did not get created or some permissions issue or lock file issue
When sudo systemctl start mongod is issued it will call init scripts and starts mongod instance as user mongod.If this user does not have permissions on the dir/logpaths it will fail to start as mentioned in your post
So the ideal method is to fix the permissions issue and start mongod with service instead of using sudo

Hi Rabah,

I had the same issue as you initially. The default path of the data directory is /data/db. However, in your mongo.conf file, it states that the file path is actually /var/lib/mongo

So all you need to do is specify the path when using the mongod command.
mongod --dbpath /var/lib/mongo
I did not need to use the sudo command.