Step 1: Update System Packages
Before installing MongoDB, ensure your system packages are up-to-date.
sudo apt-get update
sudo apt-get upgrade
Step 2: Import the MongoDB Public Key
MongoDB provides a public key to verify the packages.
wget -qO - https://www.mongodb.org/static/pgp/server-4.4.asc | sudo apt-key add -
Step 3: Create a MongoDB List File
Create a list file for MongoDB in the sources.list.d directory.
echo "deb [ arch=amd64,arm64 ] http://repo.mongodb.org/apt/ubuntu focal/mongodb-org/4.4 multiverse" | sudo tee /etc/apt/sources.list.d/mongodb-org-4.4.list
Step 4: Reload Local Package Database
Reload the local package database to include the MongoDB packages.
sudo apt-get update
Step 5: Install MongoDB Packages
Install the MongoDB packages using the following command.
sudo apt-get install -y mongodb-org
Step 6: Start MongoDB Service
Start the MongoDB service and ensure it runs on system startup.
sudo systemctl start mongod
sudo systemctl enable mongod
Step 7: Verify MongoDB Installation
Check if MongoDB has been installed correctly by checking its status.
sudo systemctl status mongod
Step 8: Access MongoDB Shell
To start using MongoDB, access the MongoDB shell.
mongo
Additional Configuration (Optional)
You may want to configure MongoDB further for security or performance reasons.
1. Configuration File
MongoDB's configuration file is located at /etc/mongod.conf
. You can edit this file to change various settings.
sudo nano /etc/mongod.conf
2. Enable Authentication
To enable authentication, add the following lines under security
:
security:
authorization: enabled
Restart MongoDB to apply the changes.
sudo systemctl restart mongod
3. Create Administrative User
Access the MongoDB shell and create an administrative user.
use admin
db.createUser(
{
user: "admin",
pwd: "password",
roles: [ { role: "userAdminAnyDatabase", db: "admin" } ]
}
)
Conclusion
Congratulations! You have successfully installed and configured MongoDB on your Linux system. You can now start building your applications with MongoDB as your database.