Restoring the MySQL Root User and Password on macOS: A Comprehensive Guide
After accidentally removing the MySQL root user, regaining access can seem daunting, especially without additional user accounts. This article provides a step-by-step solution for macOS users utilizing MAMP's MySQL installation.
Steps to Restore Root Access:
-
Obtain SysAdmin Privileges: As the actions require administrative privileges, collaborate with an authorized user to execute the following commands.
-
Edit my.cnf Configuration File:
- Locate the MAMP installation directory (usually /Applications/MAMP/).
- Open the "conf/my.cnf" file in any text editor.
- Under the "[mysqld]" section, add the line "skip-grant-tables".
-
Restart MySQL:
- In Terminal, navigate to the "Applications/MAMP/Library/bin/" directory.
- Execute the command "sudo ./mysql.server restart".
-
Access MySQL without Password:
- Open a new Terminal window.
- Type "mysql" and press enter. You should be prompted for a password, but leave it blank.
-
Recreate 'root' User:
- In the MySQL console, run the following commands:
- "DELETE FROM mysql.user WHERE user = 'root' AND host = 'localhost';"
- "INSERT INTO mysql.user SET user = 'root', host = 'localhost', password = Password('yournewpassword'), ... (remaining privileges)";
-
Update my.cnf Configuration:
- Return to the "my.cnf" file and remove the "skip-grant-tables" line.
- Save and close the file.
-
Restart MySQL:
- Execute the command "sudo ./mysql.server restart" again.
-
Reconnection with Root Password:
- Open phpMyAdmin or another MySQL client using the new root password.
By following these steps, you can restore root access to your MAMP MySQL installation on macOS.
The above is the detailed content of How Can I Restore My MySQL Root User and Password on macOS Using MAMP?. For more information, please follow other related articles on the PHP Chinese website!