Home  >  Article  >  Database  >  How to Reset Your MySQL Root Password When You\'ve Forgotten It?

How to Reset Your MySQL Root Password When You\'ve Forgotten It?

Mary-Kate Olsen
Mary-Kate OlsenOriginal
2024-11-03 10:22:03645browse

How to Reset Your MySQL Root Password When You've Forgotten It?

Forgot MySQL Root Password: A Comprehensive Reset Guide

Losing your MySQL root password can be a frustrating experience. This article will provide a step-by-step solution to reset the password even if you have forgotten it.

Step 1: Locate MySQL Configuration File

Run the following command to find the MySQL configuration file path:

mysql --help | grep -A 1 "Default options"

Step 2: Enable Password Reset

Edit the configuration file (e.g., /etc/mysql/mysql.conf.d/mysqld.cnf). Add the line "skip-grant-tables" under the [mysqld] block. Save the changes.

Step 3: Restart MySQL Service

Restart the MySQL service:

sudo service mysql restart

Step 4: Access MySQL Shell

Log in to the MySQL shell as root without a password:

mysql -u root

Step 5: Reset Root Password

Execute the following SQL commands to flush privileges and change the root password:

FLUSH PRIVILEGES;
ALTER USER 'root'@'localhost' IDENTIFIED WITH mysql_native_password BY 'MyNewPass';

Step 6: Restore MySQL Configuration

Revert the changes made to the MySQL configuration file by removing or commenting out the "skip-grant-tables" line.

Step 7: Restart MySQL Service (Again)

Restart the MySQL service once more to apply the changes:

sudo service mysql restart

Conclusion

You have now successfully reset the MySQL root password. This procedure allows you to regain access to your database and avoid potential data loss.

The above is the detailed content of How to Reset Your MySQL Root Password When You\'ve Forgotten It?. 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