How to Reset a Forgotten MySQL Password on Linux

MySQL is a popular open-source database management system used by many developers and organizations to store and manage data. However, if you have forgotten the password for your MySQL database on a Linux system, it can be quite frustrating to regain access. In this article, we will guide you through the process of resetting a forgotten MySQL password on Linux.

Method 1: Reset Password using MySQL Safe Mode

  1. First, stop the MySQL service by running the following command in the terminal:

    sudo systemctl stop mysqld
  2. Start MySQL in safe mode by running the following command:

    sudo mysqld_safe --skip-grant-tables&
  3. Connect to MySQL using the root user without a password by running the following command:

    mysql -u root
  4. Once you are logged into MySQL, switch to the MySQL database by running the command:

    use mysql;
  5. Change the root password with the following command:
    update user set authentication_string=password('new_password') where user='root';

Replace new_password with your desired new password.

  1. Flush the privileges to ensure the changes take effect:

    flush privileges;
  2. Exit MySQL by typing:

    exit;
  3. Restart the MySQL service by running the command:

    sudo systemctl start mysqld
  4. You should now be able to log in to MySQL with the new password you set.

Method 2: Reset Password by Editing MySQL Configuration File

  1. Stop the MySQL service using the following command:

    sudo systemctl stop mysqld
  2. Edit the MySQL configuration file using a text editor like nano or vim:

    sudo nano /etc/mysql/my.cnf
  3. Add the following line under the [mysqld] section:

    skip-grant-tables
  4. Save and exit the editor.

  5. Restart the MySQL service:

    sudo systemctl start mysqld
  6. Log in to MySQL as root without a password:

    mysql -u root
  7. Change the root password with the following command:
    use mysql;
    update user set authentication_string=password('new_password') where user='root';
    flush privileges;
    exit;

Replace new_password with your desired new password.

  1. Remove the skip-grant-tables line from the MySQL configuration file.

  2. Restart the MySQL service:

    sudo systemctl restart mysqld
  3. You should now be able to log in to MySQL with the new password.

By following either of these methods, you should be able to reset a forgotten MySQL password on a Linux system. Remember to choose a strong and secure password to protect your database from unauthorized access.

Tags: 1094109410941094