Home > Database > Mysql Tutorial > body text

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

Mary-Kate Olsen
Release: 2024-11-03 10:22:03
Original
644 people have browsed it

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"
Copy after login

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
Copy after login
Copy after login

Step 4: Access MySQL Shell

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

mysql -u root
Copy after login

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';
Copy after login

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
Copy after login
Copy after login

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!

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
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template