Home >Database >Mysql Tutorial >How do I retrieve my lost MySQL root password?

How do I retrieve my lost MySQL root password?

Patricia Arquette
Patricia ArquetteOriginal
2024-11-05 17:39:02780browse

How do I retrieve my lost MySQL root password?

Retrieving the MySQL Root Password When Lost

If you've forgotten your MySQL root password, fret not. There are methods available to retrieve or reset it without knowing the original password.

Option 1: File-Based Retrieval

Traditionally, MySQL stores the root password in the ~/.mylogin.cnf file on Unix-based systems. However, you mentioned not having a directadmin directory locally, which may indicate that you're using a different system.

Option 2: Temporary Root Access

For Ubuntu users, you can temporarily gain root access to your MySQL database by following these steps:

  1. Stop the MySQL service: sudo service mysql stop
  2. Start MySQL in safe mode: sudo mysqld_safe --skip-grant-tables --skip-syslog --skip-networking
  3. Connect to MySQL without a password: mysql -u root
  4. Change the root password:

    UPDATE mysql.user SET authentication_string=PASSWORD('password') WHERE User='root';
    FLUSH PRIVILEGES;
  5. Quit safe mode and restart MySQL:

    mysqladmin shutdown
    sudo service mysql start

Note: This method works for MySQL 5.7 and below. For versions 8.0 and above, the password field in the mysql.user table has been removed. Instead, use the authentication_string field.

The above is the detailed content of How do I retrieve my lost 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