MongoDB comes from the English word “humongous,” which means enormous. MongoDB is a NoSQL database, so data is not stored in tables as it is in MySQL. Basically, MongoDB stores data structures in JSON-like documents with a dynamic schema, making integration with other applications easier and faster. Another advantage of MongoDB is that it supports horizontal scaling.
In this article, we will show you how to install MongoDB Community Edition on a server running the Ubuntu distribution.
Install MongoDB
To install MongoDB, we need to obtain its key and add it:
# wget -qO - https://www.mongodb.org/static/pgp/server-5.0.asc | sudo apt-key add -
If an error appears when adding the key, run the following commands:
# apt-get -y install wget gnupg && wget -qO - https://www.mongodb.org/static/pgp/server-5.0.asc | apt-key add -
Next, we need to add the repository corresponding to the version of the distribution being used:
# echo "deb [ arch=amd64,arm64 ] https://repo.mongodb.org/apt/ubuntu focal/mongodb-org/5.0 multiverse" | sudo tee /etc/apt/sources.list.d/mongodb-org-5.0.list
# echo "deb [ arch=amd64,arm64 ] https://repo.mongodb.org/apt/ubuntu bionic/mongodb-org/5.0 multiverse" | sudo tee /etc/apt/sources.list.d/mongodb-org-5.0.list
# echo "deb [ arch=amd64,arm64 ] https://repo.mongodb.org/apt/ubuntu xenial/mongodb-org/5.0 multiverse" | sudo tee /etc/apt/sources.list.d/mongodb-org-5.0.list
Then we update the repositories:
# apt-get update
And install MongoDB:
# apt-get install -y mongodb-org
We enable the service, start it, and check its status:
# systemctl enable mongod && systemctl start mongod && systemctl status mongod
● mongod.service - High-performance, schema-free document-oriented database
Loaded: loaded (/lib/systemd/system/mongod.service; disabled; vendor preset:
enabled)
Active: active (running) since Wed 2018-02-14 14:05:37 CET; 5s ago
Docs: https://docs.mongodb.org/manual
Main PID: 2298 (mongod)
Tasks: 23
Memory: 41.4M
CPU: 59ms
CGroup: /system.slice/mongod.service
└─2298 /usr/bin/mongod --config /etc/mongod.conf
Feb 14 14:05:37 mongodb systemd[1]: Started High-performance, schema-free document-oriented
database.
Secure MongoDB
Now that MongoDB is installed, it is time to secure it so that no one can access it without a username and password. To do this, we will create a root user. We will connect to MongoDB by running:
# mongo
use admin
db.createUser(
{
user: "root",
pwd: "CloudingEjemploKB",
roles: [ { role: "userAdminAnyDatabase", db: "admin" } ]
}
)
Use strong passwords; the previous step is only an example. Now that we have created the root user, we will modify the MongoDB configuration to allow only secure connections. We will edit the file /etc/mongod.conf and uncomment the lines:
security: authorization: enabled
Configure IPs
By default, MongoDB only responds through IP 127.0.0.1. You may need to access your server externally; to do this, you will need to modify the configuration in /etc/mongod.conf and change bindIp:
#Allow access via private IP (10.20.10.XX)
bindIp: 127.0.0.1,10.20.10.XX
#Allow access via public IP (46.183.11X.XX)
bindIp: 127.0.0.1,46.183.11X.XX