How to Install MongoDB on Ubuntu

Introduction

This guide will walk you through the steps to install and configure MongoDB on an Ubuntu system, ensuring it is up-to-date and secured with basic authentication.

Prerequisites

  • You should have administrative (root) access to the Ubuntu system.
  • A stable internet connection is required to download packages and updates.

Step 1 – Update system

This ensures your system has the latest security patches and software updates, reducing potential vulnerabilities.

sudo apt update && sudo apt upgrade -y

Step 2 – Check if libssl is already installed and Download MongoDB

This checks if libssl (a crucial library for secure communication) is installed. If it’s missing or outdated, it downloads and installs a compatible version.

openssl version

if [ $? -ne 0 ]; then
wget http://archive.ubuntu.com/ubuntu/pool/main/o/openssl/libssl1.1_1.1.0g-2ubuntu4_amd64.deb
sudo dpkg -i libssl1.1_1.1.0g-2ubuntu4_amd64.deb
fi

Step 3 -Import MongoDB GPG Key

This imports the MongoDB GPG key, allowing your system to verify the authenticity of packages from the MongoDB repository.

wget -qO - https://www.mongodb.org/static/pgp/server-4.4.asc | sudo apt-key add -

Step 4 – Add MongoDB Repository (adjust “jammy” if needed)

This adds the official MongoDB repository to your system’s software sources. Make sure to replace “jammy” with your Ubuntu codename (e.g., “focal,” “bionic”) if needed. You can find your Ubuntu codename with lsb_release -cs.

echo "deb [ arch=amd64,arm64 signed-by=/usr/share/keyrings/mongodb-server-4.4.gpg ] https://repo.mongodb.org/apt/ubuntu jammy/mongodb-org/4.4 multiverse" | sudo tee /etc/apt/sources.list.d/mongodb-org-4.4.list

Step 5 – Update package lists and install MongoDB

This updates your package lists and installs the MongoDB server and associated tools.

sudo apt update
sudo apt install mongodb-org

Step 6 – Start and enable MongoDB service

This starts the MongoDB service and configures it to start automatically at boot time.

sudo systemctl start mongod
sudo systemctl enable mongod

Step 7 – Verify service status

This checks if the MongoDB service is running correctly. You should see output indicating it’s “active (running).”

sudo systemctl status mongod

Step 8 – Secure MongoDB

Next, we must enable authorization, requiring users to authenticate before accessing the database.

# Edit the configuration file
sudo nano /etc/mongod.conf

# Add the following line under the `security` section
security:
  authorization: "enabled"

# Save and exit the editor (Ctrl+X, Y, Enter)

# Restart MongoDB to apply the changes
sudo systemctl restart mongod

Step 9 – Create admin user (in mongo shell)

Now create an administrative user with full privileges over all databases.

# Access the MongoDB shell
mongo

# Switch to the 'admin' database
use admin

# Create the admin user (replace 'your_strong_password' with a secure password)
db.createUser({
  user: "admin",
  pwd: passwordPrompt(), 
  roles: [{ role: "userAdminAnyDatabase", db: "admin" }]
})

# Exit the shell
exit

Step 10 Verify connectivity

Replace ‘your_strong_password’ with the actual password you set. This command attempts to connect to the MongoDB server using the admin credentials. If successful, you’ll enter the MongoDB shell.

mongosh --host localhost --port 27017 -u admin -p --authenticationDatabase admin

Optional: Configure firewall rules if necessary

If you have a firewall enabled (e.g., ufw), you might need to allow incoming connections on port 27017 (the default MongoDB port). Refer to your firewall’s documentation for specific instructions.

Conclusion

You have successfully installed and secured MongoDB on your Ubuntu system. Remember to replace placeholders with actual values and adjust commands if needed based on your specific setup.

Additional Tips

  • Backup Regularly: Implement a regular backup strategy to protect your MongoDB data.
  • Monitor Performance: Use MongoDB’s monitoring tools or third-party solutions to track database performance and identify potential issues.
  • Stay Updated: Periodically check for MongoDB updates and apply them to benefit from security enhancements and bug fixes.
Elsewhere On TurboGeek:  How to install Microsoft SQL 2012 R2

Richard.Bailey

Richard Bailey, a seasoned tech enthusiast, combines a passion for innovation with a knack for simplifying complex concepts. With over a decade in the industry, he's pioneered transformative solutions, blending creativity with technical prowess. An avid writer, Richard's articles resonate with readers, offering insightful perspectives that bridge the gap between technology and everyday life. His commitment to excellence and tireless pursuit of knowledge continues to inspire and shape the tech landscape.

You may also like...

Leave a Reply

Your email address will not be published. Required fields are marked *

Translate »