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
Restart the database service.
sudo systemctl restart mysql
This command adds security to your installation.
sudo mysql_secure_installation
This command is to connect to mysql server.
sudo mysql
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;
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';
Here you add all permissions to the database.
GRANT ALL PRIVILEGES ON *.* TO 'luffy'@'%' WITH GRANT OPTION;
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
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"}]
})
You need to edit this file, if you need to connect from remote IP.
sudo nano /etc/mongod.conf
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!