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:
<code class="shell">sudo service mysql stop</code>
<code class="shell">sudo mysqld_safe --skip-grant-tables --skip-syslog --skip-networking</code>
Open a new terminal and execute:
<code class="shell">mysql -u root</code>
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'.
<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!