How to create a user in MongoDB

MongoDB has mongod (Mongo Deamon) to run an instance of MongoDB on your computer or server. The default configuration file that mongod uses by default is mongod.config .
In Ubuntu, to check if there is any instance of mongod running on your system, just run this command :

sudo service mongod status

Note that, in addition to showing the system status, this command also reveals the path of the configuration file mongod.conf – /etc/mongod.conf. If your system does not have an active mongod instance , just run this command:

sudo service mongod start                              [mongodb start server]

In MongoDB you can enable specific users for specific databases with specific rules . However, in this tutorial we will create a user who has the rule: root . That is, a user who can do everything.

The mongod is just the server that will run an instance of MongoDB on your operating system. The person responsible for sending commands to your mongod is the mongo shell .

Read: Install MongoDB on Ubuntu 22.04

If your MongoDB port is configured with the default port (27017), simply access:

sudo mongo –port 27017

If you have configured your mongod on another port (3000, for example), it would be enough to pass the port configured in the “port” parameter. If not, the command “mongo” would be enough.

The next step is to create the root user. First select the “admin” database.

use admin

Netx create your user using the command below:

db.createUser (                                    [mongodb create user in database]
user: “myUserAdmin”,
pwd: “abc123 @”,
roles: [“userAdminAnyDatabase”,

In this command, You are creating a user with the login “myUserAdmin”, who has the password “acb123 @” and is a user of the type “root” – has the power to do everything. The complete list of roles can be found in the official MongoDB documentation .

Read: How to install MongoDB on Ubuntu 20.04 or 18.04

Now that your user has been created, simply enable authentication on MongoDB, as it is not enabled by default. For that, you will need to modify the mongodb configuration file mongod.conf. With your favorite file editor, access the configuration file:

sudo vim /etc/mongod.conf

Click the “i” button to enable the “insertion” of data in the file. And right after, add (Do not remove any other settings) these settings in your file:

authorization: enabled

After adding these two lines, just exit the file edit, click on “ESC”, type “: wq!” (write and quit) and press enter.

Now, every time a mongod instance is launched, it will consider the authentication setting “enabled”. However, the current instance of your mongod did not notice the change in the file. Because of this, you need to restart the service:

service mongod restart

Your mongod now has user authentication enabled. If you access the mongo shell without any users and try, for example, to list your MongoDB databases, an error will occur. To authenticate you have two options:

  • Access Mongo Shell without authentication.

sudo mongo –port 27017

  • Select the database.

use admin

  • Authenticate

db.auth (“myUserAdmin”, “abc123 @”)


Authenticate by passing the information by parameter in Mongo Shell.

mongo admin -u myUserAdmin -p abc123 @

Okay, now your MongoDB has a root user configured. Everything is safer now. However, what about having a root user later, if your MongoDB can be accessed only locally? That is why we will enable external access.

Enabling External Access

To enable external access, changing the mongod.conf file is required . Thus:

Open the file:

sudo vim /etc/mongod.conf

Add the configuration below:

# network interfaces
port: 27017

Save the file and restart the service:

service mongod restart

This setting will allow any computer to connect to your server, because “bindIp” is configured to release all existing IP’s: “”.

If you like the content, we would appreciate your support by buying us a coffee. Thank you so much for your visit and support.


Nikolaus Oosterhof

Nikolaus holds a degree in software development and has a strong passion for all things tech-related, especially gadgets with screens. Though he is nostalgic for older phone models, he's a retired gamer and continues to enjoy programming in open-source environments. Additionally, Nikolaus enjoys writing about Linux, macOS and Windows and has experience designing web pages.

Leave a Reply