Install MongoDB Community Edition on CentOS 8

0
1758

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

Install MongoDB Community Edition on CentOS 8

Run system update.

dnf update

Create MongoDB RPM Repository

As of this writing, the official MongoDB rpm repos for centOS 8 are not yet released and thus the repos providing test MongoDB packages are available.

To create the MongoDB rpm repository file, run the command below and enter content below;

The repository created below is for installing MongoDB 4.2 on CentOS 8.

vim /etc/yum.repos.d/mongodb.repo
[mongodb-org-4.2]
name=MongoDB Repository
baseurl=https://repo.mongodb.org/yum/redhat/8/mongodb-org/testing/x86_64/
gpgcheck=1
enabled=1
gpgkey=https://www.mongodb.org/static/pgp/server-4.2.asc

You can as well use the official repos for previous CentOS releases. For example, to use the RPM repos for CentOS 7;

[mongodb-org-4.2]
name=MongoDB Repository
baseurl=https://repo.mongodb.org/yum/redhat/7/mongodb-org/4.2/x86_64/
gpgcheck=1
enabled=1
gpgkey=https://www.mongodb.org/static/pgp/server-4.2.asc

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

dnf install mongodb-org
...
=======================================================================================================================================================
 Package                            Arch                   Version                                               Repository                       Size
=======================================================================================================================================================
Installing:
 mongodb-org                        x86_64                 4.2.1-0.1.rc0.el8                                     mongodb-org-4.2                  10 k
Installing dependencies:
 python2                            x86_64                 2.7.15-24.module_el8.0.0+193+7850e68f                 AppStream                       107 k
 python2-libs                       x86_64                 2.7.15-24.module_el8.0.0+193+7850e68f                 AppStream                       6.0 M
 python2-pip                        noarch                 9.0.3-13.module_el8.0.0+32+017b2cba                   AppStream                       2.0 M
 python2-setuptools                 noarch                 39.0.1-11.module_el8.0.0+32+017b2cba                  AppStream                       643 k
 mongodb-org-mongos                 x86_64                 4.2.1-0.1.rc0.el8                                     mongodb-org-4.2                  14 M
 mongodb-org-server                 x86_64                 4.2.1-0.1.rc0.el8                                     mongodb-org-4.2                  25 M
 mongodb-org-shell                  x86_64                 4.2.1-0.1.rc0.el8                                     mongodb-org-4.2                  17 M
 mongodb-org-tools                  x86_64                 4.2.1-0.1.rc0.el8                                     mongodb-org-4.2                  47 M
Enabling module streams:
 python27                                                  2.7                                                                                        

Transaction Summary
=======================================================================================================================================================
Install  9 Packages

Total download size: 111 M
Installed size: 295 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.2.1-rc0
git version: edf6d45851c0b9ee15548f0f847df141764a317e
OpenSSL version: OpenSSL 1.1.1 FIPS  11 Sep 2018
allocator: tcmalloc
modules: none
build environment:
    distmod: rhel80
    distarch: x86_64
    target_arch: x86_64

Running MongoDB on CentOS 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
...
2019-10-15T12:20:32.490-0400 I  NETWORK  [initandlisten] Listening on /tmp/mongodb-27017.sock
2019-10-15T12:20:32.490-0400 I  NETWORK  [initandlisten] Listening on 127.0.0.1
2019-10-15T12:20:32.491-0400 I  NETWORK  [initandlisten] waiting for connections on port 27017
...

Enable MongoDB to run on system boot.

systemctl enable mongod

Using MongoDB on CentOS 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.2.1-rc0
connecting to: mongodb://127.0.0.1:27017/?compressors=disabled&gssapiServiceName=mongodb
Implicit session: session { "id" : UUID("1f77f765-eb9f-414c-9a33-d40d4ad3c2dd") }
MongoDB server version: 4.2.1-rc0
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 databases) 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()
{
	"_id" : ObjectId("5da6036c387fad741503e4a1"),
	"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: '[email protected]',
     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" : "testdb.testuser",
		"userId" : UUID("e0e3911b-c2b3-4580-b043-1c044b0f51f5"),
		"user" : "testuser",
		"db" : "testdb",
		"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: '[email protected]',
    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: '[email protected]',
    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.

There you go and that marks the end of our guide on how to install MongoDB community edition on CentOS 8.

Learn how to manage MongoDB from graphical user interface using Robo 3T by following the link below;

Install Robo 3T MongoDB GUI Tool on CentOS 8

Related Tutorials

Install MongoDB 4 on Fedora 30/29/CentOS 7

Install MariaDB 10.4 on CentOS 8

Install MySQL 8 on CentOS 8

Install LAMP Stack on CentOS 8

Install LEMP Stack on CentOS 8

LEAVE A REPLY

Please enter your comment!
Please enter your name here