Recover MySQL Root Password

How to recover a MySQL root password without a password.

  1. Stop MySQL.
  2. Start MySQL Safe Mode.
  3. Login to MySQL as root without password.
  4. Change root password.

# Stop MySQL.
service mysql stop
# MySQL Safe Mode.
mysqld_safe --skip-grant-tables &
# Login to MySQL without password. Set new password.
mysql -u root -p
use mysql;
# For MySQL 5.6 or lower
UPDATE mysql.user SET Password=PASSWORD('newpassword') WHERE User='root';
# For MySQL 5.7 or higher
SET PASSWORD FOR 'root'@'localhost' = PASSWORD("newpassword");
# Kill mysqld
killall mysqld
# Restart MySQL
service mysql start

I ran into issues running MySQL Safe mode. I got a “UNIX socket file don’t exists” error. Here’s the fix.

mkdir -p /var/run/mysqld
chown mysql:mysql /var/run/mysqld

Change MySQL Password

The following are instructions on how to change your MySQL password from the Unix command line.

Login to MySQL and access MySQL table.

mysql -u root -p
use mysql;

Change the password for any user, flush privileges and quit.

update user set password=PASSWORD('new_password') where User='root';
flush privileges;

Make sure to make password changes on your applications as well.

In WordPress, edit the wp-config.php file to change database password.