Home  >  Article  >  Database  >  How to reset MySQL root password on Linux?

How to reset MySQL root password on Linux?

PHPz
PHPzOriginal
2023-04-21 14:17:272325browse

As a website operation and maintenance personnel, when we need to manage the MySQL server, we may need to modify the MySQL root password due to security, change and maintenance reasons. But sometimes you forget your password, what should you do? This article explains how to reset the MySQL root password on Linux.

Resetting the MySQL root password on Linux requires the following two steps:

  1. Stop the MySQL service
  2. Restart MySQL using the MySQL user who needs to reset the password, And reset the root password

The following are the detailed steps:

  1. Stop the MySQL service

Use the following command to stop the MySQL service:

# systemctl stop mysqld
  1. Restart MySQL using the MySQL user who needs to reset the password, and reset the root password

Use the following command to start MySQL, and set it to log in without a password:

# systemctl set-environment MYSQLD_OPTS="--skip-grant-tables"
# systemctl start mysqld

Next, use the following command to log in to MySQL:

# mysql -uroot

Since you have set up skip verification using --skip-grant-tables, MySQL will not ask for input at this time. password.

After successful login, use the following command to modify the password of the root user:

mysql> UPDATE mysql.user SET authentication_string=PASSWORD('newpassword') WHERE User='root';

After modification, execute the following command to refresh MySQL permissions:

mysql> FLUSH PRIVILEGES;

Finally, use the following command to exit MySQL and reset the MySQL service:

mysql> exit;
# systemctl unset-environment MYSQLD_OPTS
# systemctl restart mysqld

Resetting the MySQL root password on Linux is done. In a production environment, always remember to remove the --skip-grant-tables prefix from your MySQL configuration to increase database security.

Summary:

This article describes how to reset the MySQL root password on Linux. By using the --skip-grant-tables prefix when MySQL is started, we can log into MySQL without the root password, thereby resetting the root user's password. In a production environment, it is important to protect the security of MySQL.

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

Statement:
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