Home > Database > Mysql Tutorial > body text

How to change the password of MySQL root user in Linux

PHPz
Release: 2023-04-19 14:59:42
Original
3458 people have browsed it

Security is very important when using open source software such as Linux and MySQL. When installing and configuring MySQL, setting a strong password is necessary, and changing this password regularly is highly recommended. This article will introduce in detail how to change the password of the MySQL root user in a Linux system environment.

Step 1: Log in to the MySQL server

In the Linux system, we use the client tool officially provided by MySQL to connect to the MySQL server. When installing MySQL, these tools are already installed on the system by default. We can use the following command to log in to the MySQL server:

mysql -u root -p
Copy after login

This command will ask you to enter the password of the MySQL root user. If entered correctly, you will enter the MySQL command line prompt.

Step 2: Stop the MySQL server

Before changing the MySQL root password, we need to stop the MySQL server to ensure data consistency and security. Use the following command to stop the MySQL server:

sudo systemctl stop mysql
Copy after login

Step 3: Modify the my.cnf configuration file

my.cnf is the MySQL configuration file, which stores MySQL configuration information. We need to modify it to change the root password. Enter the following command in the terminal to open the my.cnf file:

sudo vi /etc/mysql/my.cnf
Copy after login

In my.cnf, we need to find the [mysqld] section and add a skip-grant-tables configuration, which will allow us to modify MySQL root password. Add the following configuration at the end of the [mysqld] section:

skip-grant-tables
Copy after login

Save the file and exit the editor.

Step 4: Start the MySQL server

After modifying the my.cnf configuration file, we need to restart the MySQL server. Use the following command to start the MySQL server:

sudo systemctl start mysql
Copy after login

When the MySQL server restarts, we need to log in to the MySQL command line prompt again.

Step 5: Change the root password

Use the following command to enter the MySQL command line prompt:

mysql -u root
Copy after login

After you enter the MySQL command line prompt, use the following command to change it root password:

use mysql;
update user set authentication_string=password('新密码') where user='root';
Copy after login

Please note that you need to replace 'new password' with the new password you want to set. After executing the above command, use the following command to make the update effective:

flush privileges;
Copy after login

Exit the MySQL command line prompt and use the following command to restart the MySQL server:

sudo systemctl restart mysql
Copy after login

Now you have successfully changed The password of the MySQL root user.

Summary

In this article, we introduced how to change the password of the MySQL root user in a Linux system environment. It should be noted that only users with administrative rights can change the root password, and before changing the password, the MySQL server must be stopped and the skip-grant-tables configuration must be added to my.cnf. After changing the password, remember to restart the MySQL server for the changes to take effect. Through this article, you can better protect the security of your MySQL database.

The above is the detailed content of How to change the password of MySQL root user in Linux. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!