Installing MongoDB system Centos 8
The following tutorial describes how to install MongoDB system Centos 8.
The whole is simple and comes down to a few simple commands wykonanaia.
In previous entries we described the installation process for Ubuntu 18.04 which is under this entry.
For starters, we create a repository:
1 | nano /etc/yum.repos.d/mongodb.repo |
its content:
1 2 3 4 5 6 | [mongodb-org-4.2] name=MongoDB Repository baseurl=https://repo.mongodb.org/yum/redhat/$releasever/mongodb-org/development/x86_64/ gpgcheck=1 enabled=1 gpgkey=https://www.mongodb.org/static/pgp/server-4.2.asc |
Save the file and install the same service kodonujemy:
1 | dnf install mongodb-org |
Run it and add to autostart:
1 2 | systemctl start mongod systemctl enable mongod |
Check the status of services:
1 | systemctl status mongod |
Score:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 | ? mongod.service - MongoDB Database Server Loaded: loaded (/usr/lib/systemd/system/mongod.service; enabled; vendor preset: disabled) Active: active (running) since Mon 2020-01-03 03:59:12 EDT; 5min ago Docs: https://docs.mongodb.org/manual Process: 737 ExecStart=/usr/bin/mongod $OPTIONS (code=exited, status=0/SUCCESS) Process: 735 ExecStartPre=/usr/bin/chmod 0755 /var/run/mongodb (code=exited, status=0/SUCCESS) Process: 732 ExecStartPre=/usr/bin/chown mongod:mongod /var/run/mongodb (code=exited, status=0/SUCCESS) Process: 726 ExecStartPre=/usr/bin/mkdir -p /var/run/mongodb (code=exited, status=0/SUCCESS) Main PID: 914 (mongod) Memory: 216.1M CGroup: /system.slice/mongod.service ??914 /usr/bin/mongod --auth -f /etc/mongod.conf Oct 28 03:58:14 centos8 systemd[1]: Starting MongoDB Database Server... Oct 28 03:58:28 centos8 mongod[737]: about to fork child process, waiting until server is ready for connections. Oct 28 03:58:28 centos8 mongod[737]: forked process: 914 Oct 28 03:59:12 centos8 mongod[737]: child process started successfully, parent exiting Oct 28 03:59:12 centos8 systemd[1]: Started MongoDB Database Server. |
MongoDB to get to using the command:
1 | mongo |
Creating a user administrative MongoDB
we get to MongoDB command :
1 | mongo |
Then we move the database named admin
1 | use admin |
We issue a command to create user:
1 2 3 4 5 6 7 | db.createUser( { user: "mongodadmin", pwd: "password123", roles: [ { role: "userAdminAnyDatabase", db: "admin" } ] } ) |
password123 we change, of course, to our secret password 😉
We should get the result:
1 2 3 4 5 6 7 8 9 | Successfully added user: { "user" : "mongodadmin", "roles" : [ { "role" : "userAdminAnyDatabase", "db" : "admin" } ] } |
Check your command:
1 | show users |
Score:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 | { "_id" : "admin.mongodadmin", "userId" : UUID("f6e908db-e393-44a9-8c77-0fdb1c2baa0e"), "user" : "mongodadmin", "db" : "admin", "roles" : [ { "role" : "userAdminAnyDatabase", "db" : "admin" } ], "mechanisms" : [ "SCRAM-SHA-1", "SCRAM-SHA-256" ] } |
Configure authentication MongoDB
By default MongoDB allows all users access to the MongoDB shell and run arbitrary commands. It is therefore recommended that you configure authentication MongoDB, to prevent other users to run commands without the required permissions.
First you need to enable authentication for MongoDB, by editing the file
1 | nano /lib/systemd/system/mongod.service |
And so we set the following line:
1 | Environment="OPTIONS= --auth -f /etc/mongod.conf" |
reload services:
1 2 | systemctl --system daemon-reload systemctl restart mongod |
Now we try to enter without permission:
1 2 3 | mongo use admin show users |
We'll see an error
1 2 3 4 5 6 | 2019-10-28T04:13:15.346-0400 E QUERY [js] uncaught exception: Error: command usersInfo requires authentication : _getErrorWithCode@src/mongo/shell/utils.js:25:13 DB.prototype.getUsers@src/mongo/shell/db.js:1638:15 shellHelper.show@src/mongo/shell/utils.js:883:9 shellHelper@src/mongo/shell/utils.js:790:15 @(shellhelp2):1:1 |
Now we log on correctly privileges:
1 2 | db.auth('mongodadmin', 'password123') show users |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 | You should see the admin user with their roles in the following output: { "_id" : "admin.mongodadmin", "userId" : UUID("f6e908db-e393-44a9-8c77-0fdb1c2baa0e"), "user" : "mongodadmin", "db" : "admin", "roles" : [ { "role" : "userAdminAnyDatabase", "db" : "admin" } ], "mechanisms" : [ "SCRAM-SHA-1", "SCRAM-SHA-256" ] } |