Home  >  Article  >  Database  >  How Can I Recover My MySQL Root Access After Accidental Deletion on OS X Using MAMP?

How Can I Recover My MySQL Root Access After Accidental Deletion on OS X Using MAMP?

Susan Sarandon
Susan SarandonOriginal
2024-11-25 19:45:12729browse

How Can I Recover My MySQL Root Access After Accidental Deletion on OS X Using MAMP?

Regaining Access to MySQL after Deleting 'root' User

Accidentally deleting the 'root' user in MySQL can leave you stranded without any means to access or manage your database. Fortunately, there's a method to restore the 'root' user and its privileges on OS X using MAMP.

Step-by-Step Instructions:

  1. Obtain Administrative Privileges: Seek assistance from a SysAdmin or someone with administrative rights to the system.
  2. Modify my.cnf: Locate the my.cnf file for MySQL, which is typically found in /private/etc/my.cnf. Under the [mysqld] section, add the following line:

    skip-grant-tables
  3. Restart MySQL: Restart MySQL to apply the changes to my.cnf.
  4. Access MySQL without Password: Open a terminal window and type:

    mysql

    This should prompt you for a password, but simply hit Enter to proceed.

  5. Delete and Insert 'root' User: Execute the following MySQL commands:

    DELETE FROM mysql.user 
    WHERE  user = 'root' 
    AND host = 'localhost'; 
    
    INSERT INTO mysql.user 
    SET user = 'root', 
        host = 'localhost', 
        password = Password('whatevernewpassword'), 
        ...
        (grant all necessary privileges)
        ...;

    Replace 'whatevernewpassword' with your desired password.

  6. Exit MySQL and Remove my.cnf Modification: Type exit to leave MySQL. Then, edit my.cnf again and remove the skip-grant-tables line.
  7. Restart MySQL Again: Restart MySQL to apply the changes and restore normal operation.

With these steps, you should have successfully restored the 'root' user and set a new password. You can now access and manage MySQL as needed.

The above is the detailed content of How Can I Recover My MySQL Root Access After Accidental Deletion on OS X Using MAMP?. 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