Category Archives: Bash

Reset MySQL Root/Admin/AnyUser forgoten password using SSH

Stop MySQL

1
2
3
4
5
#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:

1
2
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

1
mysql> mysql -u root

Tell MySQL which database to use

1
mysql> use mysql;

Check list of users from user

1
mysql> select User, Password from user;

Reset Password

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

Flush the privileges

1
mysql> flush privileges;

Exit MySQL

1
mysql> quit

Stop and start MySQL

1
2
3
4
5
6
7
#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

1
mysql -u root -p