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: How to install MongoDB on Ubuntu 20.04 or 18.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 (                                    [create user in mongodb]
{
user: “myUserAdmin”,
pwd: “abc123 @”,
roles: [“userAdminAnyDatabase”,
“dbAdminAnyDatabase”,
“readWriteAnyDatabase”]})

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 .

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:

security:
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 @”)

or

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
net:
port: 27017
bindIp: 0.0.0.0

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: “0.0.0.0”.


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 has a degree in software development. He is passionate about gadgets with a screen, nostalgic for phones, a retired gamer and open source programmer. He likes also to write about macOS and Windows. design web pages and debug long programs!

Leave a Reply

Close Menu