Prerequisites.
- Ubuntu 20.04 server.
- A sudo user.
Introduction to MongoDB
MongoDB is a cross-platform database that uses documents that are JSON-like with schemas that can be customized.
This NoSQL database application focuses on documents and supports search, transactional, mobile, and analytical use cases. MongoDB can deliver these features while employing a robust data model and a developer-friendly query interface.
You can simply preserve data integrity and provide high availability with MongoDB to help your organization flourish. Before moving further, MongoDB verifies that your mission-critical workloads fulfill compliance and security criteria.
With indexing, sharding, pre-built replication, performance tools, and more, MongoDB gives you the confidence to run in production.
Introduction to Ubuntu
Ubuntu is an open-sourced operating system that is based on Linux. It supports computers, cellphones, and network servers. The system was created by Canonical Ltd which is based in the UK.
Install MongoDB on Ubuntu Steps
Here are the key steps that you may implement to install MongoDB on Ubuntu, configure and run it:
Install MongoDB on Ubuntu Steps: Install MongoDB Server
- Step 1: To import the MongoDB public GPG Key, type the following command in a terminal window.
wget -qO – https://www.mongodb.org/static/pgp/server-5.0.asc | sudo apt-key add –
This should respond with an “OK”.
- Step 2: Create a list file for your MongoDB package under the /etc/apt/sources.list.d directory.
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
- Step 3: Refresh the local package database.
sudo apt-get update
In the following location, the installation script creates a data directory.
/var/lib/mongodb
MongoDB reports activity logs to a mongod.log file which can be located from this location:
/var/log/mongodb
- Step 4: Install the MongoDB Packages
sudo apt-get install -y mongodb-org
Despite the fact that you can select any version of MongoDB, apt-get will automatically upgrade the packages whenever a newer version becomes available. You can pin the package to the currently installed version to prevent unwanted upgrades:
echo “mongodb-org hold” | sudodpkg –set-selections
echo “mongodb-org-database hold” | sudodpkg –set-selections
echo “mongodb-org-server hold” | sudodpkg –set-selections
echo “mongodb-org-shell hold” | sudodpkg –set-selections
echo “mongodb-org-mongos hold” | sudodpkg –set-selections
echo “mongodb-org-tools hold” | sudodpkg –set-selections
Install MongoDB on Ubuntu Steps: Run MongoDB
The mongod service manages the MongoDB package. To manage it, use the systemctl commands.
- Step 1: Check the status.
$ sudosystemctl status mongod
- Step 2: Start MongoDB.
sudosystemctl start mongod
- Step 3: Allow MongoDB to be started on boot.
$ sudosystemctl enable mongod
- Step 4: Verify whether MongoDB has started.
sudosystemctl status mongod
- Step 5: Stop MongoDB
sudosystemctl stop mongod
- Step 6: Restart MongoDB
sudosystemctl restart mongod
- Step 7: Start using MongoDB by starting a mongosh session.
mongosh
Install MongoDB on Ubuntu Steps: Configuration
Because you haven’t configured MongoDB authentication, you’ll get some starting warnings when the service loads.
Current Mongosh Log ID: 612e0b0da174636a8c40e2de
…
Using MongoDB: 5.0.2
Using Mongosh: 1.0.5
…
test >
Step 1: Switch to admin
test > use admin
Step 2: Run the following command to create an administrator user.
admin>db.createUser(
{
user: “mongo_db_admin”,
pwd: passwordPrompt(),
roles: [ { role: “userAdminAnyDatabase”, db: “admin” }, “root”]
}
)
Enter your password and continue.
Step 3: Exit the command line interface
admin> quit
Step 4: To enable authorization, open the MongoDB file using a nano text editor
$ sudo nano /etc/mongod.conf
Step 5: Locate the security directive that is commented out and change the value.
security:
authorization: enabled.
Save and close the file.
Step 6: Restart the server.
$ sudosystemctl restart mongod
Install MongoDB on Ubuntu Steps: Uninstalling MongoDB
You must delete the MongoDB applications, the configuration files, and any directories containing data and logs to entirely remove MongoDB from a system.
This process is not reversible and it completely removes MongoDB, its configuration, and all of the databases in it, so make sure you backup your data beforehand.
Step 1: Stop MongoDB
sudo service mongod stop
Step 2: Remove all the packages you had installed.
sudo apt-get purge mongodb-org*
Step 3: Delete all the databases and the log files which were created.
sudo rm -r /var/log/mongodb
sudo rm -r /var/lib/mongodb
Conclusion
This blog discusses the steps to install MongoDB on Ubuntu and configure it. This includes a brief about MongoDB and how to run it. It also discusses the steps to uninstall MongoDB from your system.