Home >Database >Mysql Tutorial >How to Recover a Forgotten MySQL Root Password?

How to Recover a Forgotten MySQL Root Password?

Linda Hamilton
Linda HamiltonOriginal
2024-11-05 18:51:02396browse

How to Recover a Forgotten MySQL Root Password?

How to Retrieve the Forgotten MySQL Root Password

Problem:

Users may encounter situations where they've forgotten their MySQL root password. This can create a significant obstacle, especially for system administrators. Without the root password, accessing and managing MySQL becomes impossible.

Solution:

To recover the root password, a series of steps can be followed:

  1. Stop the MySQL Service:
<code class="shell">sudo service mysql stop</code>
  1. Launch MySQL in Safe Mode:
<code class="shell">sudo mysqld_safe --skip-grant-tables --skip-syslog --skip-networking</code>
  1. Connect to MySQL:

Open a new terminal and execute:

<code class="shell">mysql -u root</code>
  1. Update Password:

Enter the following queries to change the password:

<code class="sql">UPDATE mysql.user SET authentication_string=PASSWORD('password') WHERE User='root';
FLUSH PRIVILEGES;</code>

Note: For MySQL versions 5.7 and later, the field name in the query should be 'authentication_string' instead of 'password'.

  1. Exit Safe Mode and Restart MySQL:
<code class="shell">mysqladmin shutdown
sudo service mysql start</code>

With these steps, the root password will be reset, and users can regain access to their MySQL database. It's important to keep a record of the new password for future reference.

The above is the detailed content of How to Recover a Forgotten MySQL Root Password?. 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