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: !()-_+=$
конец внимание
- Open Mongo CLI (Command Line Interface) and create a database:
mongo
- Use the elma365 database. If it doesn't exist, you need to create it.
use elma365
- Create the elma365 user to work with the database and set SecretPassword as the password:
db.createUser({user:'elma365', pwd:'SecretPassword', roles:["readWrite"]})
- 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:
- Connect to mongo:
mongo
- Create a superuser:
use admin
db.createUser(
{
user: "superuser",
pwd: "SecretPassword",
roles: [ "root" ]
}
)
- Make sure the user has been created:
show users
- To enable authentication, edit the
/etc/mongod.conf
configuration file:
sudo nano /etc/mongod.conf
- Find and uncomment the
security
section, then add theauthorization
parameter and set its value toenabled
. Note that there has to be no space before thesecurity
line, and there are two spaces before theauthorization
line:
security:
authorization: "enabled"
- 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.
конец внимание
- To back up the Mongodb data, use the following command:
elma365ctl dump --parts=mongo –consistent
- The dump will be saved to the
/backup
folder with a Time Stamp Protocol.
Restore the MongoDB data using elma365ctl restore
- 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.
- Connect to the elma365 database and delete it using the following commands:
use elma365
db.dropDatabase()
- Create a database and assign the selected user as its owner, as described above.
- 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