Installing and securing MongoDB in Debian 11
The following tutorial describes how to install MongoDB in Debian 11.
We will do the whole thing by making a dozen or so entries in the console.
First, we download the necessary packages, we add the pgp key and get the mongoDB repositories
1 | apt-get install curl apt-transport-https software-properties-common gnupg2 -y |
1 | wget -qO - https://www.mongodb.org/static/pgp/server-4.4.asc | apt-key add - |
1 | echo "deb http://repo.mongodb.org/apt/debian buster/mongodb-org/4.4 main" | tee /etc/apt/sources.list.d/mongodb-org.list |
We update the package list and install mongoDB
1 | apt-get update -y |
1 | apt-get install mongodb-org -y |
Run it on your system:
1 2 | systemctl start mongod systemctl enable mongod |
We can check the version of Mongodb by issuing the command:
1 | mongod --version |
the result of the:
1 2 3 4 5 6 7 8 9 10 11 12 13 | db version v4.4.9 Build Info: { "version": "4.4.9", "gitVersion": "b4048e19814bfebac717cf5a880076aa69aba481", "openSSLVersion": "OpenSSL 1.1.1k 25 Mar 2021", "modules": [], "allocator": "tcmalloc", "environment": { "distmod": "debian10", "distarch": "x86_64", "target_arch": "x86_64" } } |
Now we will enable authorization in mongoDB
We execute the command:
1 | mongo |
And we connect:
1 | > use admin |
We create the base:
1 2 3 4 5 6 7 | > db.createUser( { user: "madmin", pwd: "password", roles: [ { role: "userAdminAnyDatabase", db: "admin" } ] } ) |
the result of the:
1 2 3 4 5 6 7 8 9 | Successfully added user: { "user" : "madmin", "roles" : [ { "role" : "userAdminAnyDatabase", "db" : "admin" } ] } |
We exit using the CRTL shortcut + D
We edit the configuration file:
1 | nano /etc/mongod.conf |
And change:
1 2 | security: authorization: enabled |
The last step is to reload the service:
1 | systemctl restart mongod |