Last Updated: December 01, 2022
·
74.39K
· fluxsauce

Reset the MySQL 5.7 root password in Ubuntu 16.04 LTS

This is a bit gnarly. If you have a better method of updating the password without triggering a warning about PASSWORD being deprecated, I'm all ears.

# Stop MySQL
sudo service mysql stop
# Make MySQL service directory.
sudo mkdir /var/run/mysqld
# Give MySQL user permission to write to the service directory.
sudo chown mysql: /var/run/mysqld
# Start MySQL manually, without permission checks or networking.
sudo mysqld_safe --skip-grant-tables --skip-networking &
# Log in without a password.
mysql -uroot mysql

Update the password for the root user.

UPDATE mysql.user SET authentication_string=PASSWORD('YOURNEWPASSWORD'), plugin='mysql_native_password' WHERE User='root' AND Host='%';
EXIT;
# Turn off MySQL.
sudo mysqladmin -S /var/run/mysqld/mysqld.sock shutdown
# Start the MySQL service normally.
sudo service mysql start

5 Responses
Add your response

Thousand thanks!!! It really helped me!

over 1 year ago ·

Hey,

just signed up here to be able to comment here.

Thanks for the post, this really saved me today. I only had to replace '%' with 'localhost' because I only had the root created with "localhost" ability. After that, I could login and add the root-user as well for "%" (any host) and now I can connect easily and add other users.
Thanks again, I already gist'ed it ;)

over 1 year ago ·

I could get in my precious MySQL after resetting this this command (for ubuntu 17.10 and mysql-5.7 the command on the post didn´ t work)

UPDATE mysql.user SET authentication_string=PASSWORD('YOURNEWPASSWORD'), plugin='mysql_native_password' WHERE User='root' AND Host='localhost';
over 1 year ago ·

Saved the day!
Thanks @morhook, mine couldn't recognize '%' too

over 1 year ago ·

@moelha @morhook @ebumstengel @Jan Sarman could have used `UPDATE mysql.user SET authenticationstring=PASSWORD('YOURNEWPASSWORD'), plugin='mysqlnativepassword' WHERE User='root'` to update all root passwords regardless of their host.

over 1 year ago ·