MongoDB on CetnOS 8 or Stream Linux or on any operating system is meant to provide a highly reliable and scalable enterprise database. MongoDB is called a document type database and can store JSON etc.
The commands given below are to install and perform initial settings of the latest version of MongoDB; on the CentOS 8 Linux / stream or RHEL 8 server environment, however, it will also work for CentOS 7.5. Just follow the steps given below.
1. Login as the root user
You must have root or a user with sudo access to install MongoDB or any other packages on CentOS or RHEL.
su
Press the Enter button after the above command and type the root password of your account.
2. Register MongoDB 4.2 repository in CentOS 8
The first thing which we do to install the MongoDB on our CentOS 8 Linux system is to add MongoDB official repository for its latest version that is 4.2 while writing this article. Even if later there would be a new version we can simply use the MongoDB update command to get the latest version. But first, we have to install the MongoDB community edition on our Linux operating system. Simply follow the below command to all
yum install nano
Create a MongoDB repo file:
nano /etc/yum.repos.d/mongodb-org-4.2.repo
After creating a repo file, add the following line in it
[mongodb-org-4.2] name=MongoDB Repository baseurl=https://repo.mongodb.org/yum/redhat/$releasever/mongodb-org/development/x86_64/ gpgcheck=1 enabled=1 gpgkey=https://www.mongodb.org/static/pgp/server-4.2.asc
Now, press Ctrl+X and then Y to save the file.
3. Run system update
To update the cache of the CentOS or RHEL repository run the below command so that it can recognize our latest added repo of MongoDB.
yum update
or
dnf update
4. Check the available version of MongoDB
To know which is the latest packages we are about to download and install on our CentOS 8 Linux or Stream, run the below command:
yum info mongodb-org
Output for the above command:
Available Packages Name : mongodb-org Version : 4.2.0 Release : 0.1.latest.el8 Arch : x86_64 Source : mongodb-org-4.2.0-0.1.latest.el8.src.rpm Repo : @System From repo : mongodb-org-4.2 Summary : MongoDB open source document-oriented database system (metapackage) URL : http://www.mongodb.org License : AGPL 3.0
5. Install MongoDB 4.2 using yum or dnf
The installation command will install all the needed packages for the MongoDB server along with tools and shells. Here is that:
dnf install mongodb-org
or
yum install mongodb-org
Output for the above command:
[root@localhost ~]# dnf install mongodb-org CentOS-8 - AppStream 1.2 kB/s | 4.3 kB 00:03 CentOS-8 - Base 873 B/s | 3.8 kB 00:04 CentOS-8 - Extras 469 B/s | 1.5 kB 00:03 MongoDB Repository 894 B/s | 5.7 kB 00:06 Dependencies resolved. ========================================================================================================= Package Arch Version Repository Size ========================================================================================================= Installing: mongodb-org x86_64 4.2.0-0.1.latest.el8 mongodb-org-4.2 10 k Installing dependencies: python2 x86_64 2.7.15-22.module_el8.0.0+32+017b2cba AppStream 107 k python2-libs x86_64 2.7.15-22.module_el8.0.0+32+017b2cba 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.0-0.1.latest.el8 mongodb-org-4.2 14 M mongodb-org-server x86_64 4.2.0-0.1.latest.el8 mongodb-org-4.2 25 M mongodb-org-shell x86_64 4.2.0-0.1.latest.el8 mongodb-org-4.2 17 M mongodb-org-tools x86_64 4.2.0-0.1.latest.el8 mongodb-org-4.2 46 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
Depending on your environment, you may be prompted to import a GPG key. When it asks you to import that, press Y and then the Enter button to continue.
6. Check the installed MongoDB version
Once the installation gets completed, check the version of MongoDB available on your server or Desktop.
mongo --version
Output for the above command:
[root@localhost ~]# mongo --version MongoDB shell version v4.2.0-163-gb48b8bf git version: b48b8bf95d9a6e201238496eb7a39dd67b8d8dcf 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
7. Set MongoDB as system Service
To make sure the MongoDB service gets started automatically whenever it crashed system the gets rebooted or booted use the below commands:
 systemctl enable mongod
8. Start the MongoDB service on CentOS 8
systemctl start mongod
Access to the MongoDB shell:
Type:
mongo
9. How to use mongo command and check its operation
As a confirmation of MongoDB working is verified above by using the command: mongo
Now, we can specify any database name after the.
Let’s create a database – db01
mongo db01
Create a collection to insert some demo data into it:
db.createCollection('users');
Add documents to the collection. For example, we are adding name and age data in JSON format.
db.users.insert( {name:"micheal", age:27} );
Gets the documents in the collection. There is only one case this time, but it is possible to get the result in order to test it by specifying conditions.
db.users.find();
output:
{ "_id" : ObjectId("5d948001181f1b56bb74e854"), "name" : "micheal", "age" : 27 }
Now, let’s find out the collection with a particular age that is 27.
db.users.find( {age:27} );
Output: In the above command we are finding out documents with age 27. Here we have only a single entry so it will show that one:
{ "_id" : ObjectId("5d948001181f1b56bb74e854"), "name" : "micheal", "age" : 27 }
Other Articles:
Related Posts
How to open HEIC images on Windows for free
How to install Server Manager Windows 11 or 10
Install Remote Server Administration (RSAT) Tools for Windows 11 or 10
Get back the Missing Show Desktop icon in Windows 11
Get Missing Hibernate Button Back on Windows 11 using CMD
What is the DPC watchdog violation on Windows 11? Why this is caused and the possible fixes