MongoDB is a popular NoSQL database known for its high performance, scalability, and flexibility. This guide will walk you through the steps to install MongoDB Community Edition on Ubuntu 24.04 Noble Numbat.
Step 1: Import the MongoDB GPG Key
The first step in installing MongoDB is to import the public GPG key used by the package management system to verify the packages. Open your terminal and run the following command:
$ wget -qO - https://www.mongodb.org/static/pgp/server-6.0.asc | sudo apt-key add -Step 2: Create the MongoDB Source List
Next, create a list file for MongoDB. Use the following command to create the file and add the MongoDB repository:
$ echo "deb [ arch=amd64,arm64 ] https://repo.mongodb.org/apt/ubuntu focal/mongodb-org/6.0 multiverse" | sudo tee /etc/apt/sources.list.d/mongodb-org-6.0.listStep 3: Update the Package Database
After adding the MongoDB repository, update the local package database:
$ sudo apt updateStep 4: Install MongoDB Packages
Now, install the MongoDB packages. This command will install mongodb-org, which includes the latest stable version of MongoDB along with related tools:
$ sudo apt install -y mongodb-orgStep 5: Start and Enable MongoDB Service
Once the installation is complete, start the MongoDB service and enable it to start on boot:
$ sudo systemctl start mongod
$ sudo systemctl enable mongodTo check the status of the MongoDB service, use the following command:
$ sudo systemctl status mongodYou should see output indicating that MongoDB is active and running.
Step 6: Verify the Installation
To verify that MongoDB was installed and is running correctly, you can connect to the MongoDB shell using the mongo command:
$ mongo --eval 'db.runCommand({ connectionStatus: 1 })'If MongoDB is running properly, you will see output showing the connection status.
Step 7: Configure MongoDB (Optional)
MongoDB Configuration File
The MongoDB configuration file is located at /etc/mongod.conf. You can edit this file to change the default configuration settings. For example, to change the bind IP address to allow remote connections, you can modify the bindIp setting:
$ sudo nano /etc/mongod.confFind the net section and update the bindIp value:
net:
port: 27017
bindIp: 0.0.0.0Save the file and exit. Then, restart the MongoDB service to apply the changes:
$ sudo systemctl restart mongodCreate an Administrative User
To secure your MongoDB installation, it is recommended to create an administrative user. First, connect to the MongoDB shell:
$ mongoSwitch to the admin database:
use adminCreate a new administrative user with a username and password of your choice:
db.createUser({
user: "admin",
pwd: "yourpassword",
roles: [{ role: "userAdminAnyDatabase", db: "admin" }]
})Exit the MongoDB shell:
exitEnable Authentication
To enable authentication, edit the MongoDB configuration file again:
$ sudo nano /etc/mongod.confFind the security section and enable authorization:
security:
authorization: "enabled"Save the file and exit. Then, restart the MongoDB service:
$ sudo systemctl restart mongodNow, you need to authenticate as the administrative user to perform any actions in the MongoDB shell:
$ mongo -u admin -p --authenticationDatabase adminConclusion
You have successfully install MongoDB Community Edition on your Ubuntu 24.04 Noble Numbat system. You can now start using MongoDB to manage your databases and applications.
