Reset MySQL root password in Ubuntu/Linux Command Line

These are the terminal commands to reset your MySQL password for your “root” user.

This is useful if you forgot your MySQL password on your local LAMP server. This is a fix for errors like these:

ERROR 1045: Access denied for user: 'root@localhost' (Using
password: NO)

ERROR 1045: Access denied for user: 'root@localhost' (Using
password: YES)

To reset your MySQL password, follow these steps and enter the commands in a terminal:

  1. Stop MySQL using this command:
    sudo /etc/init.d/mysql stop
  2. This command will let you connect to MySQL without entering a password:
    sudo /usr/sbin/mysqld --skip-grant-tables --skip-networking &
  3. Next, the terminal may “freeze,” so simply open a new terminal window (CTRL + ALT + ‘T’).

    In the new terminal window, enter the following commands:

  4. Connect to MySQL without a password:
    mysql -u root
  5. Flush privileges:
  6. Update your MySQL password to a new password with these 2 commands. Be sure to change ‘new_password’ on both commands to your own custom password.
    SET PASSWORD FOR root@'localhost' = PASSWORD('new_password');
    UPDATE mysql.user SET Password=PASSWORD('new_password') WHERE User='root';

    The response message should say that one or more rows were affected. This means your MySQL root password was successfully updated.

  7. Flush privileges again:
  8. Exit from MySQL:
  9. Stop and start MySQL one last time:
    sudo /etc/init.d/mysql stop;
    sudo /etc/init.d/mysql start;

That’s it. Now, you should be able to connect to MySQL normally, using your new password.

See more: ,

Questions and Comments are Welcome

Your email address will not be published. All comments will be moderated.

Please wrap code in "code" bracket tags like this: