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.listStep 3: Update the package list
Update the package list:
sudo apt-get updateStep 4: Install MongoDB
Install the latest stable version of MongoDB:
sudo apt-get install -y mongodb-orgStep 5: Start MongoDB
Start the MongoDB service:
sudo systemctl start mongodStep 6: Enable MongoDB to start on boot
Enable MongoDB to start on boot:
sudo systemctl enable mongodStep 7: Verify MongoDB installation
Check the status of the MongoDB service to ensure it is running:
sudo systemctl status mongod2. 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.debStep 2: Install MongoDB Compass
Install the downloaded .deb package:
sudo dpkg -i mongodb-compass_1.36.2_amd64.debIf you encounter any dependency issues, run:
sudo apt-get install -fStep 3: Launch MongoDB Compass
You can launch MongoDB Compass from the terminal or by searching for it in your application menu:
mongodb-compass3. Verify Installation
MongoDB: You can verify the MongoDB installation by connecting to the MongoDB shell:
mongo --versionYou can also connect to the MongoDB server using the MongoDB shell:
mongoMongoDB 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:
mongoStep 2: Switch to the admin database
Switch to the admin database:
use adminStep 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.confFind the security section and add the following line:
security:
authorization: enabledSave and close the file, then restart the MongoDB service:
sudo systemctl restart mongodNow, you will need to authenticate when connecting to the MongoDB shell:
mongo -u admin -p your_password --authenticationDatabase adminConclusion
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.