As the most commonly used database management system, the installation and configuration of MySQL is a skill that we must master when doing back-end development, especially the setting of the MySQL root password.
The MySQL root account is the highest authority account of the MySQL database and has functions such as creating and deleting databases, user accounts, and modifying permissions. For MySQL database security, the password setting of the root account is crucial. Below we will explain in detail how to set the MySQL root password.
Enter the following command in the terminal to log in to MySQL:
mysql -u root
If you set a password during MySQL installation, You will need to enter a password. If you are logging in for the first time or if you have not set a password, leave it blank and press Enter to enter.
After entering the MySQL client, we need to change the password of the root account. Enter the following command in the terminal:
ALTER USER 'root'@'localhost' IDENTIFIED BY 'newpassword';
Among them, replace "newpassword" with the password you want to set.
After changing the password, we need to confirm whether the password change is successful. Enter the following command in the terminal:
SELECT User, Host, Password FROM mysql.user;
Check the output results to see whether the password of the root account is the new password we just set.
In addition to changing the password in the MySQL client, we also need to modify the MySQL configuration file. Enter the following command in the terminal:
sudo nano /etc/mysql/mysql.conf.d/mysqld.cnf
Find the following content in the file:
#bind-address = 127.0.0.1
Modify it to:
bind-address = 0.0.0.0
Save the file and exit. The purpose of this step is to make the MySQL server accessible on other computers than the local one.
Enter the following command in the terminal to restart the MySQL server:
sudo service mysql restart
After restarting, we can use the root account on other computers To access the MySQL database, use the new password you just set.
Summary
In using MySQL for back-end development, password setting for the root account is an essential process and a very important security measure. Through the above introduction, I believe you have mastered how to set the MySQL root password. I hope this article can be helpful to everyone.
The above is the detailed content of How to set root password in mysql. For more information, please follow other related articles on the PHP Chinese website!