Installing MongoDB on Linux Cloud Servers

2019-11-28 By Aaron 39 Views MongoDB database DB

MongoDB is a document-based open source database management system, which can be used to store a large volume of data in various forms. It is commonly used in modern web applications. It supports various features, some of which includes indexing, replication, load-balancing, file storage etc.


This guide outlines the steps to install MongoDB in various Linux distributions such as CentOS, Ubuntu and Debian.


CentOS


  1. To install MongoDB in CentOS, the MongoDB repository needs to be first enabled. For this, please create a file /etc/yum.repos.d/mongodb-org.repo with any editor of your choice.

    # vi /etc/yum.repos.d/mongodb-org.repo  
    

    Add the below entries in the created repository file, save and exit the editor.

    [mongodb-org-4.0]
    name=MongoDB Repository
    baseurl=https://repo.mongodb.org/yum/redhat/$releasever/mongodb-org/4.0/x86_64/
    gpgcheck=1
    enabled=1
    gpgkey=https://www.mongodb.org/static/pgp/server-4.0.asc
    
  2. Execute the below command to install MongoDB package.

    # yum install -y mongodb-org
    
  3. Start the MongoDB service.

    # systemctl start mongod
    
  4. Stop or Restart the MongoDB service.

    # systemctl stop mongod
    # systemctl restart mongod
    
  5. Check the status of the MongoDB service.

    # systemctl status mongod
    
  6. To check and confirm that the MongoDB service has been installed, make use of the mongo command to access the MongoDB shell.

    # mongo 
    

    The below screenshot shows how the MongoDB shell looks like. MongoDB1


Ubuntu


  1. To install MongoDB in Ubuntu, the MongoDB repository needs to be enabled prior to the installation. For this, the MongoDB public key needs to be imported first and then create the required source list file for MongoDB.
    Import Key:

    # sudo apt-key adv --keyserver hkp://keyserver.ubuntu.com:80 --recv 4B7C549A058F8B6B         
    


    Create source list file:

    echo "deb [ arch=amd64,arm64 ] https://repo.mongodb.org/apt/ubuntu xenial/mongodb-org/4.2 multiverse" | sudo tee /etc/apt/sources.list.d/mongodb.list
    
  2. Once this has been, the package repository needs to be updated by executing the below command.

    # sudo apt update
    
  3. Install the MongoDB package.

    # sudo apt install -y mongodb-org
    
  4. Enable and start the MongoDB service.

    # sudo systemctl enable mongod
    # sudo systemctl start mongod
    
  5. Stop or Restart the MongoDB service.

    # sudo systemctl stop mongod
    # sudo systemctl restart mongod
    
  6. To check and confirm that the MongoDB service has been installed, make use of the mongo command to access the MongoDB shell.
    MongoDB2


Debian


  1. To install MongoDB in Debian, it would be to install the required packages for adding a new repository.

    # apt install software-properties-common dirmngr
    
  2. Add the MongoDB public GPG key to the system.

    apt-key adv --keyserver hkp://keyserver.ubuntu.com:80 –recv
    9DA31620334BD75D9DCB49F368818C72E52529D4
    
  3. The MongoDB repository needs to be added then, for which run the below command.

    # add-apt-repository 'deb http://repo.mongodb.org/apt/debian stretch/mongodb-org/4.0 main'
    
  4. Update the packages list and install MongoDB package.

    # apt update
    # apt install mongodb-org
    
  5. Enable and start the MongoDB service.

    # systemctl enable mongod
    # systemctl start mongod
    
  6. Stop or Restart the MongoDB service.

    # sudo systemctl stop mongod
    # Sudo systemctl restart mongod
    
  7. To check and confirm that the MongoDB service has been installed, make use of the mongo command to access the MongoDB shell.
    MongoDB3


Fedora


  1. To install MongoDB in Fedora, the MongoDB repository needs to be first enabled. For this, please create a file /etc/yum.repos.d/mongodb-org.repo with any editor of your choice.

    # sudo vi /etc/yum.repos.d/mongodb-org.repo  
    

    Add the below entries in the created repository file, save and exit the editor.

    [Mongodb]
    name=MongoDB Repository
    baseurl=https://repo.mongodb.org/yum/amazon/2/mongodb-org/4.2/x86_64/
    gpgcheck=1
    enabled=1
    gpgkey=https://www.mongodb.org/static/pgp/server-4.2.asc
    
  2. Execute the below command to install MongoDB package.

    # sudo dnf install -y mongodb-org
    

    MongoDB4

  3. Start the MongoDB service.

    # systemctl start mongod
    
  4. Stop or Restart the MongoDB service.

    # systemctl stop mongod
    # systemctl restart mongod
    
  5. Check the status of the MongoDB service.

    # systemctl status mongod
    

    MongoDB5

  6. To check and confirm that the MongoDB service has been installed, make use of the mongo command to access the MongoDB shell.

    # mongo 
    

    The below screenshot shows how the MongoDB shell looks like.

    MongoDB6