How to Restore Deleted 'root' User and Password for MySQL
Restoring a deleted MySQL 'root' user on macOS can be a daunting task, especially when there are no other users available to grant permissions. This article provides a step-by-step solution using the terminal, allowing you to recreate the 'root' user with a new password.
Steps to Restore 'root' User:
Configure my.cnf File:
skip-grant-tables
Restart MySQL:
sudo launchctl unload /Library/LaunchDaemons/com.mysql.mysql.plist sudo launchctl load /Library/LaunchDaemons/com.mysql.mysql.plist
Log in to MySQL without a Password:
mysql
Delete the Existing 'root' User:
DELETE FROM mysql.user WHERE user = 'root' AND host = 'localhost';
Create a New 'root' User:
INSERT INTO mysql.user SET user = 'root', host = 'localhost', password = PASSWORD('newpassword'), ... (Set all necessary privileges here)
Exit MySQL:
Remove 'skip-grant-tables' from my.cnf:
skip-grant-tables
Restart MySQL Again:
After completing these steps, you will have successfully restored the MySQL 'root' user and can log in using the new password specified in the query.
The above is the detailed content of How to Recover a Deleted MySQL \'root\' User and Password on macOS?. For more information, please follow other related articles on the PHP Chinese website!