In this blog we have talked about some database systems, among which the most popular are MariaDB and MySQL, as they are the systems that are usually included in Xampp or Lampp. This time we are going to talk about another system which is MongoDB
MongoDB is an open source document oriented NoSQL database system, This is a modern document database management system designed for high-performance data persistence, high availability, as well as automatic scaling, based on the technology of the NoSQL technique.
Instead of saving data in tables as it is done in relational databases, MongoDB saves data structures in a document, which is a data structure comprising of field and value pairs (MongoDB documents are comparable to JSON objects).
Because it provides high performance and excellent scalability characteristics, it is being used to build modern applications that require powerful, mission-critical and highly available databases.
MongoDB installation on Ubuntu
To install this database system on our system, We only have to open a terminal and execute the following command in it:
sudo apt install mongodb
Installation done, MongoDB service will start automatically via systemd and the process will run on port 27017. You can check its status using the command:
sudo systemctl status mongodb
If not, we can force the start with this command:
sudo systemctl start mongodb
Enabling remote connections to MongoDB
The MongoDB database system by default uses port 27017 which we must open In order to accept remote connections, we only enable it with the following command.
sudo ufw allow 27017
By default, port 27017 can only be accessed from the local address 127.0.0.1. To allow remote connections to MongoDB, it is necessary to add the server's IP address in the file /etc/mongodb.conf
Just edit it with:
sudo nano
And we can see something similar to this:
bind_ip = 127.0.0.1, your_server_ip #port = 27,017
Which we edit with the data from our server.
Allowing access to MongoDB from everywhere, it gives unrestricted access to the data in the database. Therefore, it is better to give access to the location-specific default IP address of the MongoDB port.
sudo ufw allow from your_server_IP/32 to any port 27017 sudo ufw status
Once this is done, it is enough to restart the service so that the new changes are loaded at the beginning, this we do with:
sudo systemctl restart mongodb
Create a user in MongoDB with password
By default the database has quite simple access, so to reinforce this we can create a user with a password.
MongoDB uses the SCRAM authentication mechanism by default. Using SCRAM on the system verifies the supplied user credentials against the user name, password, and authentication database.
Just start the database console with the following command:
Mongo
We can make a list of the existing databases with:
show dbs
We can create a user that will allow us to manage users and functions such as creating users, granting or revoking user roles, and creating or modifying customs roles.
Just type
use admin
And we create the user with the following command, where we replace the name of the user "root" and the password "password", by the access crecenciales that we believe appropriate.
db.createUser({user:"root", pwd:"password", roles:[{role:"root", db:"admin"}]})
Once this is done, we must enable the authentication method for this we are going to edit the following file:
sudo nano /lib/systemd/system/mongodb.service
And we will look for the next line:
ExecStart=/usr/bin/mongod --unixSocketPrefix=${SOCKETPATH} --config ${CONF} $DAEMON_OPTS
And the edit so that it is as follows:
ExecStart=/usr/bin/mongod --auth --unixSocketPrefix=${SOCKETPATH} --config ${CONF} $DAEMON_OPTS
Now we simply reboot in the database
systemctl daemon-reload sudo systemctl restart mongodb sudo systemctl status mongodb
Y We test the authentication method with:
mongo -u "usuario" -p --authenticationDatabase "contraseña"
Help me with this ERROR.
2021-03-24T21: 33: 16.233 + 0000 E QUERY [thread1] Error: listDatabases failed: {
«Ok»: 0,
"Errmsg": "not authorized on admin to execute command {listDatabases: 1.0, $ db: \" admin \ "}",
"Code": 13,
"CodeName": "Unauthorized"
}:
_getErrorWithCode@src/mongo/shell/utils.js: 25:13
Mongo.prototype.getDBs@src/mongo/shell/mongo.js: 65: 1
shellHelper.show@src/mongo/shell/utils.js: 816: 19
shellHelper@src/mongo/shell/utils.js: 706: 15
@ (shellhelp2): 1: 1