MongoDB is a document database, storing data in JSON-like form, which is revolutionary approach in the contrast of traditional relational databases. This does not mean that SQL databases will die out anytime soon; they will be here for a long time when you need to store structured data.
That being said, MongoDB gets more and more use cases; the ability to store data in a form that can change on the fly are things that must be counted with.
In this tutorial we will install the latest community release of this NoSQL database to a RHEL 8 / CentOS 8, using the tarball package. For this to work smoothly we'll set up the minimal environment, and test our configuration and running service.
In this tutorial you will learn:- How to download & extract MongoDB tarball
- How to set up environment for the service
- How to manage mongod service
- How to login to mongo shell, insert and query sample data
Software Requirements and Conventions Used
Category | Requirements, Conventions or Software Version Used |
---|---|
System | RHEL 8 / CentOS 8 |
Software | MongoDB 4 |
Other | Privileged access to your Linux system as root or via the sudo command. |
Conventions | # - requires given linux commands to be executed with root privileges either directly as a root user or by use of sudo command $ - requires given linux commands to be executed as a regular non-privileged user |
How to install mongodb on RHEL 8 / CentOS 8 step by step instructions
We need to gather an URL before the installation. For this, we need to visit the MongoDB Download Centre Community site, select the Operating system and version (Linux 64bit legacy in this case, we need the tarball). While we are provided with a download button, we also get a direct URL below, which we can use from the target machine directly.
This saves us from downloading the package trough the browser and then transfer it to the target machine, provided we do have Internet access from the target. So take note of the URL, we'll use it shortly.
Subscribe to RSS and NEWSLETTER and receive latest Linux news, jobs, career advice and tutorials.
- We'll store the binaries under
/opt
. On the target machine, we enter the directory:
And download the tarball by providing the URL acquired earlier to# cd /opt
wget
:# wget https://fastdl.mongodb.org/linux/mongodb-linux-x86_64-4.0.5.tgz --2019-01-03 16:49:59-- https://fastdl.mongodb.org/linux/mongodb-linux-x86_64-4.0.5.tgz Resolving fastdl.mongodb.org (fastdl.mongodb.org)... 52.222.150.27, 52.222.150.229, 52.222.150.45, ... Connecting to fastdl.mongodb.org (fastdl.mongodb.org)|52.222.150.27|:443... connected. HTTP request sent, awaiting response... 200 OK Length: 73214518 (70M) [application/x-gzip] Saving to: 'mongodb-linux-x86_64-4.0.5.tgz' mongodb-linux-x86_64-4.0.5.tgz 100%[================================================================================================================>] 69.82M 3.12MB/s in 23s 2019-01-03 16:50:22 (3.06 MB/s) - 'mongodb-linux-x86_64-4.0.5.tgz' saved [73214518/73214518]
- We extract the tarball:
And create an easier-to-remember symlink called# tar -zxvf mongodb-linux-x86_64-4.0.5.tgz
mongodb
that points to the extracted directory (the version number may differ):# ln -s mongodb-linux-x86_64-4.0.5 mongodb
- We create the user that will run the service called
mongod
:# useradd mongod
- We create the directory where mongodb will store it's data:
# mkdir -p /var/lib/mongo
- We set the
mongod
user as the owner of both the binaries and the data directory:# chown -R mongod:mongod /opt/mongodb* # chown -R mongod: /var/lib/mongo
- We create a basic configuration file for mongodb. We specify the data directory created, and set the database to listen only on localhost, on the default port
27017
. We create the text file/etc/mongod.conf
with the following content:
Notice thestorage: dbPath: "/var/lib/mongo" journal: enabled: true net: port: 27017 bindIp: "127.0.0.1"
dbPath
parameter, that we set to the directory we created for data storage in an earlier step. - For
systemd
to be able to manage the service, we create the text file/etc/systemd/system/mongod.service
with minimal configuration:
Note that we used the[Unit] Description=MongoDB After=syslog.target network.target [Service] Type=simple User=mongod Group=mongod ExecStart=/opt/mongodb/bin/mongod --config /etc/mongod.conf [Install] WantedBy=multi-user.target
mongod
user and group, used our custom path for themongod
binary, and included the configuration file we created by hand. - We set
selinux
to permissive for now, as it would block the service accessing resources. Setting theselinux
policies is out of the scope of this tutorial.# setenforce 0
- We'll ask
systemd
to reload:systemctl daemon-reload
- And check if the service is recognized:
# systemctl status mongod mongod.service - MongoDB Loaded: loaded (/etc/systemd/system/mongod.service; disabled; vendor preset: disabled) Active: inactive (dead)
- We are ready to start the service:
# systemctl start mongod
- And check it's status. If all goes well, we should see something like the following:
# systemctl status mongod mongod.service - MongoDB Loaded: loaded (/etc/systemd/system/mongod.service; disabled; vendor preset: disabled) Active: active (running) since Thu 2019-01-03 17:01:48 CET; 4s ago Main PID: 2993 (mongod) Tasks: 23 (limit: 12544) Memory: 45.3M CGroup: /system.slice/mongod.service 2993 /opt/mongodb/bin/mongod --config /etc/mongod.conf
- We can test our service with
mongo shell
, a command line interface shipped with MongoDB. To be able to access it, we need to include the binaries we extracted in the$PATH
. As lazy admins, we only do this once, the permanent way. We add the following line to/root/.bash_profile
, before the last "export PATH" line:
And run the script:## mongodb PATH=$PATH:/opt/mongodb/bin
# . ~/.bash_profile
- We start the
mongo shell
:
There may be some startup warnings, like huge pages settings, but we'll ignore these in this tutorial.# mongo MongoDB shell version v4.0.5 connecting to: mongodb://127.0.0.1:27017/?gssapiServiceName=mongodb Implicit session: session { "id" : UUID("8999342b-e313-48e6-92c4-bf6b07cee0e4") } MongoDB server version: 4.0.5 Welcome to the MongoDB shell. For interactive help, type "help". [...] >
- On the
mongo shell
, we'll ask for any databases present:> db test
- And switch to the shipped
test
database:> use test switched to db test
- We insert some test data (key "x" with the value of "1") into a collection created on the fly:
> db.exampleCollection.insertOne( { x: 1 } ); { "acknowledged" : true, "insertedId" : ObjectId("5c2e33040854f2d89326ae9c") } >
- And finally we query for any data in the new collection, verifying that our key-value pair is stored successfully:
> db.getCollection("exampleCollection").find().pretty(); { "_id" : ObjectId("5c2e4c2fd129ceef6a6c6112"), "x" : 1 } >