MySQL is a widely used relational database management system, and the MySQL root account is the account with the highest authority in MySQL. When using MySQL, we often need to modify or set the MySQL root password. This article will introduce how to set the MySQL root password through the command line under the Linux system.
Before setting or modifying the MySQL root password, we need to check the running status and version of MySQL. Enter the following command on the command line:
sudo systemctl status mysql
This command will output the current running status of MySQL. If MySQL is not installed yet, you need to install it first. After installing MySQL, check the MySQL version information through the following command:
mysql -V
Before setting the MySQL root password, we need to stop the MySQL service . Enter the following command on the command line:
sudo systemctl stop mysql
This command will stop the MySQL service.
The MySQL configuration file is stored in /etc/mysql/my.cnf. We need to modify this file to enable the MySQL root password. Enter the following command on the command line:
sudo vi /etc/mysql/my.cnf
Before modifying the MySQL configuration file, we need to back up an original file. Press the "i" key to enter the editing mode, and then add the following lines of code at the end of my.cnf:
[mysqld] skip-grant-tables
After the input is completed, press the "Esc" key to exit the editing mode and enter ":wq" Save the file and exit the vi editor.
After modifying the MySQL configuration file, we need to restart the MySQL service to make it take effect. Enter the following command on the command line:
sudo systemctl restart mysql
The default username for the MySQL root account is "root" and the default password is blank. Now we need to enter MySQL through the following command:
sudo mysql -u root
This command will allow us to enter MySQL as root.
After entering MySQL, we need to reset the password of the root account. Enter the following command:
use mysql; update user set authentication_string=password('你想要的密码') where user='root'; flush privileges; exit;
This command will set the new password you want to the root account, refresh permissions and exit MySQL.
After completing the password reset, we need to restore the MySQL configuration file to its original state. Enter the following command in the command line:
sudo vi /etc/mysql/my.cnf
Press the "i" key to enter the edit mode, and then delete the following lines of code added in step 3:
[mysqld] skip-grant-tables
After the input is completed, Press the "Esc" key to exit editing mode, and enter ":wq" to save the file and exit the vi editor.
We have completed setting the MySQL root password, and now we need to restart the MySQL service so that it can read the new configuration file. Enter the following command on the command line:
sudo systemctl restart mysql
At this point, the MySQL root password setting has been completed. These steps can keep your MySQL database secure. Be sure to follow security best practices and change your MySQL root password regularly.
The above is the detailed content of How to set mysql root password under Linux. For more information, please follow other related articles on the PHP Chinese website!