How to Install MongoDB on a Linux Cloud Server?

08-02-2024 02:59:09

MongoDB is a widely used NoSQL database in popular web applications. This article explains the steps to install MongoDB on a Linux cloud server, applicable to CentOS 8/7/6 and Ubuntu 20.04/18.04/16.04 operating systems.

CentOS Operating System

First, create a repository file for MongoDB software.

# nano /etc/yum.repos.d/mongodb-org-4.4.repo

The content of the repo file is as follows.

[mongodb-org-4.4]
name=MongoDB Repository
baseurl=https://repo.mongodb.org/yum/redhat/$releasever/mongodb-org/4.4/x86_64/
gpgcheck=1
enabled=1
gpgkey=https://www.mongodb.org/static/pgp/server-4.4.asc

Then, install the latest version of MongoDB.

# sudo yum install -y mongodb-org

Start the MongoDB service.

# systemctl start mongod.service
# systemctl enable mongod.service

Ubuntu Operating System

First, download the necessary files from the official MongoDB software repository.

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

Create a software repository list file for MongoDB and perform an update.

$ echo "deb [ arch=amd64,arm64 ] https://repo.mongodb.org/apt/ubuntu focal/mongodb-org/5.0 multiverse" | sudo tee /etc/apt/sources.list.d/mongodb-org-5.0.list
$ sudo apt update

After the update is complete, install the latest version of MongoDB database.

$ sudo apt-get install -y mongodb-org

Check the status of MongoDB.

$ sudo systemctl status mongod

Start the MongoDB service.

$ sudo systemctl start mongod
$ sudo systemctl enable mongod

The configuration file path for MongoDB is: /etc/mongod.conf. Modify this file to adjust MongoDB's configuration, such as changing the directories for data and log files.

After modifying the configuration file, restart MongoDB for the changes to take effect.

$ sudo systemctl restart mongod