MongoDB is an open source and document-based NoSQL database designed to efficiently process large amounts of data. Stores data as Binary JSON documents (BSON). MongoDB provides ACID support in 4.0 and later versions.
In order to achieve high performance in MongoDB, some features commonly found in RDBMS systems are not available in MongoDB. MongoDB does not have table, row and column concepts. It has a dynamic schema structure. So documents can have different schemes, which means that the scheme can change as the application evolves.
A single MongoDB instance can host multiple databases. Each database is a collection set. Collections are similar to the concept of tables in relational databases; however, these are schematic. There can be more than one document in a collection. We can think of the document as a row in relational databases.
We will install MongoDB on CentOS 8 server.
Install MongoDB on Centos 8
For the installation, we create the “/etc/yum.repos.d/mongodb-org-4.2.repo” repo file with the vim command and add the following lines into the file, save and exit:
1 2 3 4 5 6 7 8 |
vim /etc/yum.repos.d/mongodb-org-4.2.repo [mongodb-org-4.2] name=MongoDB Repository baseurl=https://repo.mongodb.org/yum/redhat/$releasever/mongodb-org/4.2/x86_64/ gpgcheck=1 enabled=1 gpgkey=https://www.mongodb.org/static/pgp/server-4.2.asc |
We install the mongodb-org package:
1 |
sudo dnf install mongodb-org |
The mongodb-org package we have installed consists of “mongos, server, shell and tools” packages.
mongodb-org-server: It includes the Mongod program, and the init command.
mongodb-org-mongos: It includes the Mongos program.
mongodb-org-shell: Includes Mongo shell program.
mongodb-org-tools: Contains mongo commands; mongodump, mongorestore, mongoexport, mongoimport, mongostat, mongotop etc.
We start the mongod(MongoDB service), and enable it to start automatically when the server is started:
1 2 |
systemctl start mongod systemctl enable mongod |
Is mongodb running?
We can check Mongod service status as follows:
1 |
systemctl status mongod |
The MongoDB configuration file is “/etc/mongod.conf” by default. It runs on port 27017 at 127.0.0.1 with default settings.
With the mongo command, we connect to the database with default settings:
1 |
mongo |
With the show dbs
command, we can view the existing databases:
MongoDB has admin, local and config databases by default.
admin database in MongoDB
The root database for administrative operations. If a user is added to the admin database, the user automatically inherits the permissions of all databases. There are also some service-wide commands that can only be run from the admin database, such as listing all databases or shutting down the service.
local database in MongoDB
Contains information of MongoDB service in replication processes. It is also not copied during replication processes.
config database in MongoDB
Stores Sharded cluster information.
By default, the authentication setting is set to disable when MongoDB service is installed. This is the reason for the warning you see in the command output below.
“WARNING: Access control is not enabled for the database.”
We will not receive this warning when we create an authorized user in the database, enable the authentication setting in the default configuration file “/etc/mongod.conf” and restart the service.
MongoDB recommends disabling Transparent HugePages for better performance. This setting is enabled by default on most Linux distributions. This is the reason for the warning you see in the command output below. “WARNING: / sys / kernel / mm / transparent_hugepage / enabled is ‘always’.”
After creating users in the database, we will change this setting as recommended.
Create User in MongoDB and MongoDB Database Operations
We connect to the admin database with the following command and create an authorized user in the entire cluster:
Create admin user in MongoDB:
1 2 3 4 5 6 7 8 |
use admin db.createUser( { user: "zaydemir", pwd: "mypassword", roles: [ { role: "userAdminAnyDatabase", db: "admin" }, "readWriteAnyDatabase" ] } ) |
We exit the database with the exit command and add the following line to the file “/etc/mongod.conf”:
1 2 |
security: authorization: enabled |
The final version of the /etc/mongod.conf file is as follows:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 |
cat /etc/mongod.conf systemLog: destination: file logAppend: true path: /var/log/mongodb/mongod.log storage: dbPath: /var/lib/mongo journal: enabled: true # how the process runs processManagement: fork: true # fork and run in background pidFilePath: /var/run/mongodb/mongod.pid # location of pidfile timeZoneInfo: /usr/share/zoneinfo # network interfaces net: port: 27017 bindIp: 127.0.0.1 security: authorization: enabled |
We are restarting the Mongod service:
Restart Mongod Service:
1 |
systemctl restart mongod |
1 |
mongo --port 27017 --host 127.0.0.1 --authenticationDatabase "admin" -u zaydemir -p 'mypassword' |
use customer
It connects to a database named customer, it creates if there is no database with that name. If we leave the database without creating an object, the database will be deleted.
db
It shows the database to which we are connected.
db.people.insert ()
In the customer database, it adds a record to the collection named people, and creates it if there is no collection.
db.people.find ()
Returns all records in the people collection.
show dbs
Returns the current database list.
show collections
Returns the list of collections in the database we connect to.
Disable Transparent HugePages
Transparent HugePages (THP) is a memory management system that is enabled by default in most Linux operating systems. In order for MongoDB to run better on Linux, THP must be disabled.
Let’s create a service file that will disable THP before the mongodb service starts every time the server is started.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
vim /etc/systemd/system/disable-transparent-huge-pages.service [Unit] Description=Disable Transparent Huge Pages (THP) DefaultDependencies=no After=sysinit.target local-fs.target Before=mongod.service [Service] Type=oneshot ExecStart=/bin/sh -c 'echo never | tee /sys/kernel/mm/transparent_hugepage/enabled > /dev/null' [Install] WantedBy=basic.target |
Reload systemd:
1 |
systemctl daemon-reload |
We start the service and enable it to be enable every time the server is started.
1 2 |
systemctl start disable-transparent-huge-pages systemctl enable disable-transparent-huge-pages |
Check Transparent HugePages
We can see that the THP setting is [never] with the following command:
1 |
cat /sys/kernel/mm/transparent_hugepage/enabled |
Sample output is as follows:
There are other ways to disable Transparent HugePages. You can find it on the MongoDB page.
In this article, I talked about basic MongoDB installation. You can find more detailed information in the article below.
“How To Install Mongodb Sharded Cluster with Keyfile Access Control on Red Hat or CentOS“