To install MongoDB and MongoDB Compass on Ubuntu, follow these steps:
1. Install MongoDB
Step 1: Import the MongoDB public key
First, you need to import the MongoDB public GPG key:
wget -qO - https://www.mongodb.org/static/pgp/server-6.0.asc | sudo apt-key add -
Step 2: Create a list file for MongoDB
Create a list file for MongoDB:
echo "deb [ arch=amd64,arm64 ] https://repo.mongodb.org/apt/ubuntu $(lsb_release -cs)/mongodb-org/6.0 multiverse" | sudo tee /etc/apt/sources.list.d/mongodb-org-6.0.list
Step 3: Update the package list
Update the package list:
sudo apt-get update
Step 4: Install MongoDB
Install the latest stable version of MongoDB:
sudo apt-get install -y mongodb-org
Step 5: Start MongoDB
Start the MongoDB service:
sudo systemctl start mongod
Step 6: Enable MongoDB to start on boot
Enable MongoDB to start on boot:
sudo systemctl enable mongod
Step 7: Verify MongoDB installation
Check the status of the MongoDB service to ensure it is running:
sudo systemctl status mongod
2. Install MongoDB Compass
Step 1: Download MongoDB Compass
Download the MongoDB Compass .deb
package from the official MongoDB website:
wget https://downloads.mongodb.com/compass/mongodb-compass_1.36.2_amd64.deb
Step 2: Install MongoDB Compass
Install the downloaded .deb
package:
sudo dpkg -i mongodb-compass_1.36.2_amd64.deb
If you encounter any dependency issues, run:
sudo apt-get install -f
Step 3: Launch MongoDB Compass
You can launch MongoDB Compass from the terminal or by searching for it in your application menu:
mongodb-compass
3. Verify Installation
MongoDB: You can verify the MongoDB installation by connecting to the MongoDB shell:
mongo --version
You can also connect to the MongoDB server using the MongoDB shell:
mongo
MongoDB Compass: You can verify the installation by launching MongoDB Compass and connecting to your MongoDB instance.
4. (Optional) Secure MongoDB
By default, MongoDB does not have authentication enabled. It is recommended to secure your MongoDB installation by enabling authentication and creating a user with appropriate privileges.
Step 1: Connect to MongoDB Shell
Connect to the MongoDB shell:
mongo
Step 2: Switch to the admin database
Switch to the admin
database:
use admin
Step 3: Create an admin user
Create an admin user with the necessary privileges:
db.createUser({
user: "admin",
pwd: "your_password",
roles: [{ role: "userAdminAnyDatabase", db: "admin" }, "readWriteAnyDatabase"]
})
Step 4: Enable authentication
Edit the MongoDB configuration file to enable authentication:
sudo nano /etc/mongod.conf
Find the security
section and add the following line:
security:
authorization: enabled
Save and close the file, then restart the MongoDB service:
sudo systemctl restart mongod
Now, you will need to authenticate when connecting to the MongoDB shell:
mongo -u admin -p your_password --authenticationDatabase admin
Conclusion
You have successfully installed MongoDB and MongoDB Compass on Ubuntu. You can now start using MongoDB to manage your databases and MongoDB Compass to interact with your MongoDB instances through a graphical interface.