Database war
Mysql & Mongodb
Users and Remote Connection

Database war Mysql & Mongodb Users and Remote Connection

Carlos Rivas's photo
Carlos Rivas

Published on Mar 1, 2021

3 min read

Hey my friends, a new post about this war. I don't know which is better, but I'll try to figure it out. I hope y'all enjoy this series. Here we go!

This time I'm going to talk about how add users and connection to the databases MYSQL and Mongodb

Mysql

First, you need to edit this file, if you need to connect from remote IP.

sudo nano /etc/mysql/mysql.conf.d/mysqld.cnf

Selection_456.png

Selection_457.png

Restart the database service.

sudo systemctl restart mysql

This command adds security to your installation.

sudo mysql_secure_installation

Selection_458.png

Selection_459.png

Selection_460.png

Selection_461.png

Selection_462.png

Selection_463.png

Selection_464.png

This command is to connect to mysql server.

sudo mysql

Selection_468.png

Into mysql environment you can list all users.

SELECT user,authentication_string,host FROM mysql.user;

You should change the way to connect to mysql server.

ALTER USER 'root'@'localhost' IDENTIFIED WITH mysql_native_password BY 'Sunny%01';

You must run this command when the user existed.

FLUSH PRIVILEGES;

Selection_470.png

The best option is to create a particular user to remote access. This character '%' you can change for your specify IP.

CREATE USER 'luffy'@'%' IDENTIFIED BY 'Sunny%01';

Selection_471.png

Here you add all permissions to the database.

GRANT ALL PRIVILEGES ON *.* TO 'luffy'@'%' WITH GRANT OPTION;

Selection_472.png

With this command you can access from remote IP.

 mysql -h 192.168.0.7 -u luffy -p

MongoDB

This command is to connect to Mongodb server.

mongo

Selection_465.png

This command selects the collection "admin"

use admin

To create a particular user. This role is to be a superuser admin to the database.

db.createUser({
  user:"luffy",
  pwd:"Sunny%01",
  roles:[{role:"root"}]
 })

Selection_473.png

You need to edit this file, if you need to connect from remote IP.

sudo nano /etc/mongod.conf

Selection_466.png

Selection_467.png

Restart the database service.

sudo systemctl restart mongod

With this command you can access from remote IP.

mongo --host "192.168.0.7:27017" -u "luffy" -p "Sunny%01"

Conclusion

  • Commands: Mysql has a language called SQL and Mongodb use JSON structure to get information.

Would you like to learn about queries in databases?

I hope you enjoy my post and remember that I am just a Dev like you!

 
Share this