How to install MongoDB on Ubuntu 22.04
In today’s tech world, MongoDB is like a superhero for managing data in applications. It’s super flexible, handling different types of data smoothly, which is perfect for today’s ever-changing apps. MongoDB can also manage a lot of data, making it great for apps that deal with tons of information. Its special abilities, like spreading data across many computers, help it keep up with the fast growth of data in modern apps.
In this blog, we’ll explore how to install MongoDB on Ubuntu 22.04. You can find detailed instructions and more information on the official MongoDB documentation at https://www.mongodb.com/docs/manual/tutorial/install-mongodb-on-ubuntu/.
- Login to your AWS account, then launch a basic instance, connect to it, and perform the initial update.
sudo su
apt-get update
2. So, we are essentially installing the community edition, not an enterprise one. First, check whether MongoDB is already installed on your server.
mongod --version
3. Then add the following key.
wget -qO - https://www.mongodb.org/static/pgp/server-6.0.asc | sudo apt-key add -
4. then Create the /etc/apt/sources.list.d/mongodb-org-7.0.list
file for Ubuntu 22.04 (Jammy):
echo "deb [ arch=amd64,arm64 ] https://repo.mongodb.org/apt/ubuntu jammy/mongodb-org/6.0 multiverse" | sudo tee /etc/apt/sources.list.d/mongodb-org-6.0.list
5. Now, let’s update and upgrade the machine before installing MongoDB.
apt-get update
apt-get upgrade -y
6. Now, let’s install MongoDB.
sudo apt install mongodb-org
7. Now, verify again with the command from step number 2. Here, you should see that MongoDB has been installed successfully.
8. Then enable MongoDB to start at system startup and initiate its start as well.
sudo systemctl enable mongod
sudo service mongod start
9. Now check the MongoDB status.
sudo service mongod status
10. Now, to enter MongoDB, use the “mongosh” command to access the database.
mongosh
11. You can execute some basic database commands within MongoDB as follows.
show dbs;
use admin;
show users;
12. So, this is how you install MongoDB on Ubuntu 22. Additionally, if you notice, we can enter the database without any authentication or password. This is a security breach. Let’s see how to configure this further.
13. Open and edit the MongoDB config file. In that file, you will see the default port MongoDB uses. Then, uncomment the “Security” section and add the following line. Save the changes.
sudo vi /etc/mongod.conf
14. Then, allow the port.
sudo ufw allow 27017
15. Now, restart the MongoDB service because we made changes to the MongoDB config file. After restarting, check if MongoDB is listening on the configured port.
sudo service mongod restart
sudo lsof -i | grep mongo
After restarting, if you notice that MongoDB has not started, please run the following command.
sudo rm /tmp/mongodb-27017.sock
sudo systemctl restart mongod
16. Now, log in again to MongoDB and check if the database is visible. If it’s not showing, it might be because we enabled authorization, and currently, we are not authorized to view the database.
17. To achieve that, go back to the “conf” file, disable the security line, create an admin user in MongoDB, and then re-enable the security line. After doing so, you should be able to see the database.
Conclusion:
In a nutshell, MongoDB is a real game-changer for developers and businesses. Its easy-to-use format makes it a hit with developers, and its powerful features ensure quick and efficient data handling. Whether it’s for online stores, quick data analysis, or running mobile apps, MongoDB’s adaptability and strength make it the go-to choice in the exciting world of technology.