Many people wonder on how to install and securing MongoDB especially in Ubuntu 18.04 and 20.04. Before that, let’s define what is MongoDB.
MongoDB is a general purpose document database which stores its data in JSON-like documents. It also claimed this is the most natural way to think about data.
#JSON file example
{
"os": "Ubuntu",
"company": "Canonical Ltd.",
"founder": {
"name": "Mark Shuttleworth"
},
"age": "16",
"flavours": [
"👽︎ Kubuntu",
"✔ Lubuntu",
"MATE",
"Xubuntu"
]
}
MongoDB has features that traditional database in Relational Database Management (RDBMS), like MariaDB and PostgreSQL don’t have or need additional steps to set up properly, such as:
## MySQL way
# initialization
CREATE TABLE `JSON_in_RDBMS` (
`os` VARCHAR(150) NOT NULL,
`company` VARCHAR(150) NOT NULL,
`founder` VARCHAR(150) NOT NULL,
`age` INT,
`flavours` VARCHAR(150) NOT NULL)
ENGINE = InnoDB;
# inserting the data
INSERT INTO `JSON_in_RDBMS` (`os`, `company`, `founder`, `age`, `flavours`) VALUES ('Ubuntu1', 'Canonical Ltd.', 'Mark Shuttleworth', '16', 'Kubuntu, Lubuntu, MATE, Xubuntu')
This tutorial will cover installation MongoDB Community Edition on 64-bit Ubuntu Linux LTS (long-term support) releases using the apt
package manager, especially on the 18.04 (Bionic) and 20.04 (Focal) LTS releases on x86_64 architecture.
# Make sure you have installed the gnupg package
sudo apt-get install gnupg
# Import the MongoDB public GPG Key
wget -qO - https://www.mongodb.org/static/pgp/server-4.4.asc | sudo apt-key add -
# Create the /etc/apt/sources.list.d/mongodb-org-4.4.list file for Ubuntu
echo "deb [ arch=amd64,arm64 ] https://repo.mongodb.org/apt/ubuntu $(lsb_release -sc)/mongodb-org/4.4 multiverse" | sudo tee /etc/apt/sources.list.d/mongodb-org-4.4.list
# Then make sure the local package database reloaded
sudo apt-get update
# Install the latest stable package
sudo apt-get install -y mongodb-org
# Start the process on systemd init
sudo systemctl start mongod
5. MongoDB is up and running !
# Enable Autostart the process
sudo systemctl enable mongod
# Restart the process
sudo systemctl restart mongod
# Stop the process
sudo systemctl stop mongod
# Verify the process running or stopped
sudo systemctl status mongod
1. Connect to the MongoDB
# First Connect to the mongo instance
$ mongo
MongoDB shell version v4.4.3
connecting to: mongodb://127.0.0.1:27017/?compressors=disabled&gssapiServiceName=mongodb
Implicit session: session { “id” : UUID(“0cfcaad9-7c56-40ea-81f8-af273c014d5e”) }
MongoDB server version: 4.4.3
Welcome to the MongoDB shell.
>
2. Create the user
# Copy paste this to mongodb shell
use ubi_db
db.createUser(
{
user: “ubi_user”,
pwd: “ub1_p4ss”,
roles: [ { role: “readWrite”, db: “ubi_db” },
{ role: “read”, db: “ubi_db_other” } ]
}
)
If successfully created, it will show like this output
2. Then connect & authenticate as the user
# Inline output on mongo shell command
. > use ubi_db
switched to db ubi_db
. > db.auth(“ubi_user”, “ub1_p4ss”)
db.foo.insert({x:1})
1
. > db.foo.insert({x:1})
WriteResult({ “nInserted” : 1 })
. > use ubi_db_other
switched to db ubi_db_other
. > db.foo.find({})