MongoDB

This article describes how to install MongoDB for Ubuntu Linux 20.04. You can also find a short user guide in the official documentation.

начало внимание 

In this example we use elma365 as the database name, elma365 as the user, and SecretPassword as the password.
When setting up MongoDB, follow your company’s security policies.

конец внимание

Install MongoDB

For correct operation, you will need MongoDB versions 3.6–5.0.

To install MongoDB, add the official repository:

sudo echo "deb [ arch=amd64 ] https://repo.mongodb.org/apt/ubuntu bionic/mongodb-org/3.6 multiverse" | sudo tee /etc/apt/sources.list.d/mongodb-org-3.6.list
sudo wget -qO - https://www.mongodb.org/static/pgp/server-3.6.asc | sudo apt-key add -
sudo apt update

Next, install MongoDB:

sudo apt install mongodb-org

Allow connection from another network

In the /etc/mongod.conf configuration file, in network interfaces, find the bindIp value. After a comma, add the external IP address of the server where MongoDB is running (for example, 192.168.10.10):

sudo nano /etc/mongod.conf
# network interfaces
net:
  port: 27017
  bindIp: 127.0.0.1,192.168.10.10

Restart MongoDB to apply the changes:

sudo systemctl restart mongod

Create a database

начало внимание

You can use the following characters in a password:

  • Capital Latin letters (A to Z)
  • Lowercase Latin letters (a to z)
  • Digits (0 to 9)
  • Symbols: !()-_+=$

конец внимание

  1. Open Mongo CLI (Command Line Interface) and create a database:

mongo

  1. Use the elma365 database. If it doesn't exist, you need to create it.  

use elma365

  1. Create the elma365 user to work with the database and set SecretPassword as the password:

db.createUser({user:'elma365', pwd:'SecretPassword', roles:["readWrite"]})

  1. Finish configuration:

exit

MongoDB security

When MongoDB is not installed in a DMZ, and other users can connect to it, you have to provide data security. To do that:

  1. Connect to mongo:

mongo

  1. Create a superuser:

use admin
db.createUser(
  {
    user: "superuser",
    pwd: "SecretPassword",
    roles: [ "root" ]
  }
)

  1. Make sure the user has been created:

show users

  1. To enable authentication, edit the /etc/mongod.conf configuration file:

sudo nano /etc/mongod.conf

  1. Find and uncomment the security section, then add the authorization parameter and set its value to enabled. Note that there has to be no space before the security line, and there are two spaces before the authorization line:

security:
  authorization: "enabled"

  1. Restart mongodb:

sudo systemctl restart mongod

Connect to MongoDB

Use this command to connect MondoDB:

mongodb://elma365:SecretPassword@<mongodb-server-address>:27017/elma365

Back up the MongoDB data using elma365ctl dump

начало внимание

While a backup is being made, elma365 services are stopped, and the application becomes unavailable.

конец внимание

  1. To back up the Mongodb data, use the following command:

elma365ctl dump --parts=mongo –consistent

  1. The dump will be saved to the /backup folder with a Time Stamp Protocol.

Restore the MongoDB data using elma365ctl restore

  1. Stop the application’s services and prepare the system for the data recovery using the following command:

elma365ctl restore –prepare

After that, you need to recreate the MongoDB database.

  1. Connect to the elma365 database and delete it using the following commands:

use elma365
db.dropDatabase()

  1. Create a database and assign the selected user as its owner, as described above.
  2. Restore the data using the following command:

elma365ctl restore --parts=mongo --path=/backup/<dump_folder>

The system will be available again within a few minutes after the command is completed.

Found a typo? Highlight the text, press ctrl + enter and notify us