Install MongoDB on Rocky Linux 8

|
Last Updated:
|
|

This guide provides a step-by-step tutorial on how to install MongoDB on Rocky Linux 8. MongoDB is a cross-platform document-oriented and a NoSQL database program.

Installing MongoDB on Rocky Linux 8

Create MongoDB RPM Repository

As of this writing, the official default repositories for Rocky Linux 8 do not provide MongoDB packages.

Thus, in order to install MongoDB on Rocky Linux, you need to create the MongoDB repository.

Note that MongoDB is available as a Community Edition and as an Enterprise Edition. This guide installs CE version.

The repository created below is for installing MongoDB 4.4, which is the current stable release version.

Run the command below to install MongoDB 4.4 repository on Rocky Linux 8.


cat > /etc/yum.repos.d/mongodb.repo << 'EOL'
[mongodb-org-4.4]
name=MongoDB Repository
baseurl=https://repo.mongodb.org/yum/redhat/$releasever/mongodb-org/4.4/x86_64/
gpgcheck=1
enabled=1
gpgkey=https://www.mongodb.org/static/pgp/server-4.4.asc
EOL

Install MongoDB

Once the MongoDB repository, you can install MongoDB by running the command;

dnf install mongodb-org

MongoDB Repository                                                                                                                           21 kB/s |  23 kB     00:01    
Dependencies resolved.
============================================================================================================================================================================
 Package                                            Architecture             Version                                                Repository                         Size
============================================================================================================================================================================
Installing:
 mongodb-org                                        x86_64                   4.4.6-1.el8                                            mongodb-org-4.4                    11 k
Installing dependencies:
 cyrus-sasl                                         x86_64                   2.1.27-5.el8                                           baseos                             95 k
 cyrus-sasl-gssapi                                  x86_64                   2.1.27-5.el8                                           baseos                             49 k
 cyrus-sasl-plain                                   x86_64                   2.1.27-5.el8                                           baseos                             46 k
 mongodb-database-tools                             x86_64                   100.3.1-1                                              mongodb-org-4.4                    54 M
 mongodb-org-database-tools-extra                   x86_64                   4.4.6-1.el8                                            mongodb-org-4.4                    23 k
 mongodb-org-mongos                                 x86_64                   4.4.6-1.el8                                            mongodb-org-4.4                    17 M
 mongodb-org-server                                 x86_64                   4.4.6-1.el8                                            mongodb-org-4.4                    22 M
 mongodb-org-shell                                  x86_64                   4.4.6-1.el8                                            mongodb-org-4.4                    14 M
 mongodb-org-tools                                  x86_64                   4.4.6-1.el8                                            mongodb-org-4.4                    11 k
 python3-pip                                        noarch                   9.0.3-19.el8.rocky                                     appstream                          19 k
 python3-setuptools                                 noarch                   39.2.0-6.el8                                           baseos                            162 k
 python36                                           x86_64                   3.6.8-2.module+el8.3.0+120+426d8baf                    appstream                          18 k
Enabling module streams:
 python36                                                                    3.6                                                                                           

Transaction Summary
============================================================================================================================================================================
Install  13 Packages

Total download size: 106 M
Installed size: 342 M
Is this ok [y/N]: y

Once the installation is done, you can verify the installed version by running the command below;

mongod --version

db version v4.4.6
Build Info: {
    "version": "4.4.6",
    "gitVersion": "72e66213c2c3eab37d9358d5e78ad7f5c1d0d0d7",
    "openSSLVersion": "OpenSSL 1.1.1g FIPS  21 Apr 2020",
    "modules": [],
    "allocator": "tcmalloc",
    "environment": {
        "distmod": "rhel80",
        "distarch": "x86_64",
        "target_arch": "x86_64"
    }
}

Running MongoDB on Rocky Linux 8

MongoDB runs as a non-privileged mongod user and it uses the /var/lib/mongo (the data directory) and /var/log/mongodb (the log directory) default directories.

MongoDB daemon is managed by systemd. Hence, you can start it by executing the command below;

systemctl start mongod

To check if MongoDB has run successfully, check its log file, /var/log/mongodb/mongod.log. You should be able to see a line indicating the MongoDB is ready and waiting for the connections.

tail /var/log/mongodb/mongod.log
...
{"t":{"$date":"2021-07-01T17:55:47.409+03:00"},"s":"I",  "c":"NETWORK",  "id":23015,   "ctx":"listener","msg":"Listening on","attr":{"address":"/tmp/mongodb-27017.sock"}}
 {"t":{"$date":"2021-07-01T17:55:47.409+03:00"},"s":"I",  "c":"NETWORK",  "id":23015,   "ctx":"listener","msg":"Listening on","attr":{"address":"127.0.0.1"}}
 {"t":{"$date":"2021-07-01T17:55:47.409+03:00"},"s":"I",  "c":"NETWORK",  "id":23016,   "ctx":"listener","msg":"Waiting for connections","attr":{"port":27017,"ssl":"off"}}
...

You can also check the status of the service by running the command below;

systemctl status mongod

● mongod.service - MongoDB Database Server
   Loaded: loaded (/usr/lib/systemd/system/mongod.service; enabled; vendor preset: disabled)
   Active: active (running) since Thu 2021-07-01 17:55:47 EAT; 1min 34s ago
     Docs: https://docs.mongodb.org/manual
  Process: 2191 ExecStart=/usr/bin/mongod $OPTIONS (code=exited, status=0/SUCCESS)
  Process: 2189 ExecStartPre=/usr/bin/chmod 0755 /var/run/mongodb (code=exited, status=0/SUCCESS)
  Process: 2187 ExecStartPre=/usr/bin/chown mongod:mongod /var/run/mongodb (code=exited, status=0/SUCCESS)
  Process: 2186 ExecStartPre=/usr/bin/mkdir -p /var/run/mongodb (code=exited, status=0/SUCCESS)
 Main PID: 2194 (mongod)
   Memory: 76.1M
   CGroup: /system.slice/mongod.service
           └─2194 /usr/bin/mongod -f /etc/mongod.conf

Jul 01 17:55:39 localhost.localdomain systemd[1]: Starting MongoDB Database Server...
Jul 01 17:55:40 localhost.localdomain mongod[2191]: about to fork child process, waiting until server is ready for connections.
Jul 01 17:55:40 localhost.localdomain mongod[2191]: forked process: 2194
Jul 01 17:55:47 localhost.localdomain mongod[2191]: child process started successfully, parent exiting
Jul 01 17:55:47 localhost.localdomain systemd[1]: Started MongoDB Database Server.

Enable MongoDB to run on system boot.

systemctl enable mongod

Using MongoDB on Rocky Linux 8

Once the installation completes, you can now start using MongoDB.

MongoDB listens on local address on port 27017 by default. Hence, from the localhost, you can simply login to MongoDB shell by running;

mongo

MongoDB shell version v4.4.6
connecting to: mongodb://127.0.0.1:27017/?compressors=disabled&gssapiServiceName=mongodb
Implicit session: session { "id" : UUID("678ce04a-f894-44a3-b70d-48c9bee1ab8f") }
MongoDB server version: 4.4.6
Welcome to the MongoDB shell.
For interactive help, type "help".
...
>

From the MongoDB shell, you can run your database management commands. For example to list available databases;

> db
test

To use a specific MongoDB database, simply run;

use database-name

To create MongoDB database, simply switch to a new database (non-existing database) by specifying the name of the database to create and insert data into it.

> use testdatabase
switched to db testdatabase

You can now insert data into your new database.

db.userdetails.insertOne(
   { "F_Name" : "fname",
     "L_NAME" : "lname",
     "ID_NO" : "12345",
     "AGE" : "19",
     "TEL" : "654321"
   }
)

Press Enter to insert the data.

{
	"acknowledged" : true,
	"insertedId" : ObjectId("5da6036c387fad741503e4a1")
}

To list database collections;

show collections

To show the data contained in a MongoDB database collection;

db.NAME-OF-COLLECTION.find().pretty()

Take for example, to list the content of the collection created above;

> use testdatabase
switched to db testdatabase
> show collections
userdetails
> db.userdetails.find().pretty()

Sample output;

{
	"_id" : ObjectId("60ddd89d60fdb735de85a0c6"),
	"F_Name" : "fname",
	"L_NAME" : "lname",
	"ID_NO" : "12345",
	"AGE" : "19",
	"TEL" : "654321"
}
>

To create MongoDB database user with read/write privileges.

use testdatabase
db.createUser(
   {
     user: 'testuser',
     pwd: 'P@ssWord',
     roles: [ { role: 'readWrite', db: 'testdatabase' } ]
   }
 );

Press Enter to add the user.

Successfully added user: {
     "user" : "testuser",
     "roles" : [
         {
             "role" : "readWrite",
             "db" : "testdatabase"
         }
     ]
 }

To list database users;

db.getUsers()

[
	{
		"_id" : "testdatabase.testuser",
		"userId" : UUID("ffd6f3ae-9b07-4b5a-84e0-9679923c625e"),
		"user" : "testuser",
		"db" : "testdatabase",
		"roles" : [
			{
				"role" : "readWrite",
				"db" : "testdatabase"
			}
		],
		"mechanisms" : [
			"SCRAM-SHA-1",
			"SCRAM-SHA-256"
		]
	}
]

If you want to create an administrator for a single database;

use testdatabase
db.createUser(
  {
    user: 'testadmin',
    pwd: 'P@ssW0rd',
    roles: [ { role: 'userAdmin', db: 'testdatabase' } ]
  }
);

To create an overall database admin with all administrative rights on all databases, use the administrative database and create admin user as follows;

use admin
db.createUser(
  {
    user: 'admin',
    pwd: 'P@ssW0rd',
    roles: [ { role: 'userAdminAnyDatabase', db: 'admin' } ]
  }
);

Well, there is a lot more to learn on how to use MongoDB. Refer to MongoDB Getting Started Guides for more information.

Enable password Authentication on MongoDB

If you note, Access control is not enabled for the database by default. To learn how to enable password authentication for MongoDB, check the guide below;

Enable MongoDB Authentication

Other Tutorials

Install Robo 3T MongoDB GUI Tool on CentOS 8

Install MongoDB 4 on Fedora 30/29/CentOS 7

SUPPORT US VIA A VIRTUAL CUP OF COFFEE

We're passionate about sharing our knowledge and experiences with you through our blog. If you appreciate our efforts, consider buying us a virtual coffee. Your support keeps us motivated and enables us to continually improve, ensuring that we can provide you with the best content possible. Thank you for being a coffee-fueled champion of our work!

Photo of author
koromicha
I am the Co-founder of Kifarunix.com, Linux and the whole FOSS enthusiast, Linux System Admin and a Blue Teamer who loves to share technological tips and hacks with others as a way of sharing knowledge as: "In vain have you acquired knowledge if you have not imparted it to others".

Leave a Comment