Introduction
MongoDB is a popular, free, and open-source NoSQL document database often used in modern web applications. This guide will walk you through setting up MongoDB on your server in a production environment.
Prerequisites
Before starting, ensure you have a sudo non-root user. If you don't have one, you can create it with the following steps:
Create a new user:
adduser name_of_new_user
Add the new user to the "sudo" group to grant administrative privileges:
gpasswd -a name_of_new_user sudo
Switch to the new user:
su name_of_new_user
Importing the Public Key
To install MongoDB, we'll use the official MongoDB repository, which provides the most up-to-date version. Ubuntu ensures package authenticity by verifying them with GPG keys, so we need to import MongoDB's GPG public key first:
sudo apt-key adv --keyserver hkp://keyserver.ubuntu.com:80 --recv 7F0CEB10
Creating a List File
Next, we'll add the MongoDB repository details to your APT sources list so that APT knows where to download the packages:
echo "deb http://repo.mongodb.org/apt/ubuntu $(lsb_release -sc)/mongodb-org/3.0 multiverse" | sudo tee /etc/apt/sources.list.d/mongodb-org-3.0.list
After adding the repository, update the package list:
sudo apt-get update
Installing and Verifying MongoDB
Now, install MongoDB with the following command:
sudo apt-get install -y mongodb-org
This command installs the latest stable version of MongoDB along with useful management tools. Once the installation is complete, MongoDB will start automatically. You can verify that it's running with:
sudo service mongod status
You should see an output indicating that MongoDB is running, with a process ID. You can manage MongoDB using the service command (e.g., service mongod stop
, service mongod start
).