Category Archives: Bash

Reset MySQL Root/Admin/AnyUser forgoten password using SSH

Stop MySQL

#For Ubuntu or Debian
sudo /etc/init.d/mysql stop

#For CentOS, Fedora, and RHEL
sudo /etc/init.d/mysqld stop

Start MySQL in safe mode
start MySQL but skip the user privileges table, you will need to have sudo access for these commands so you don’t need to worry about any user being able to reset the MySQL root password:

sudo mysqld_safe --skip-grant-tables &
#ampersand (&) at the end of the command is required.

Login MySQL
No password is required at this stage as when we started MySQL we skipped the user privileges table

mysql> mysql -u root

Tell MySQL which database to use

mysql> use mysql;

Check list of users from user

mysql> select User, Password from user;

Reset Password

mysql> update user set password=PASSWORD("mynewpassword") where User='root';

Flush the privileges

mysql> flush privileges;

Exit MySQL

mysql> quit

Stop and start MySQL

#On Ubuntu and Debian:
sudo /etc/init.d/mysql stop
sudo /etc/init.d/mysql start

#On CentOS and Fedora and RHEL:
sudo /etc/init.d/mysqld stop
sudo /etc/init.d/mysqld start

Login with new Password

mysql -u root -p