Introduction
You’ve forgotten the MySQL root password or it has mysteriously changed 🤔. Thankfully there is a way around this using the --skip-grant-tables
option.
Before you begin, if you are having problems logging into phpMyAdmin and getting an error Access denied for user ‘root’@’localhost’ , but you’re certain your root password is correct, please refer to this article first: Can’t log into phpMyAdmin: mysqli_real_connect(): (HY000/1698): Access denied for user ‘root’@’localhost’
1. Confirm MySQL version
Firstly, you must confirm which version of MySQL on Ubuntu you are running as commands will be different.
mysql -V
If on MySQL version 8, you will see something like:
mysql Ver <span class="red">8.0.20</span>-0ubuntu0.20.04.1 for Linux on x86_64 ((Ubuntu))
If you are on MySQL version 5, you will see something similar to:
mysql Ver 14.14 Distrib <span class="red">5.7.36</span>, for Linux (x86_64) using EditLine wrapper
2. Restart MySQL with skip-grant-table
In order to skip the grant tables and reset the root password, we must first stop the MySQL service. Enter your Linux password if prompted.
sudo /etc/init.d/mysql stop
Ensure the directory /var/run/mysqld
exists and correct owner set.
sudo mkdir /var/run/mysqld
sudo chown mysql /var/run/mysqld
Now start MySQL with the --skip-grant-tables
option. The &
is required here.
sudo mysqld_safe --skip-grant-tables&
You should see something similar:
[1] 1283
user@server:~$ 2019-02-12T11:15:59.872516Z mysqld_safe Logging to syslog.
2019-02-12T11:15:59.879527Z mysqld_safe Logging to '/var/log/mysql/error.log'.
2019-02-12T11:15:59.922502Z mysqld_safe Starting mysqld daemon with databases from /var/lib/mysql
Now press ENTER
to return to the Linux BASH prompt.
3. Change MySQL Root Password
You can now log in to the MySQL root account without a password.
sudo mysql --user=root mysql
Once logged in, you will see the mysql>
prompt.
MySQL 8 – Reset Root Password
For MySQL 8 on Ubuntu, run following commands.
UPDATE mysql.user SET authentication_string=null WHERE User='root';
flush privileges;
Replace your_password_here
with your own. (Generate a strong password here)
ALTER USER 'root'@'localhost' IDENTIFIED WITH mysql_native_password BY '<span class="red">your_password_here</span>';
Flush privileges again.
flush privileges;
Exit MySQL.
exit
Now skip to Step 4 below.
MySQL 5.7 – Reset Root Password
For MySQL 5.7 on Ubuntu, run this command to change the root password. Replace your_password_here
with your own. (Generate a strong password here)
update user set authentication_string=PASSWORD('<span class="red">your_password_here</span>') where user='root';
Change the auth plugin to mysql_native_password
.
update user set plugin="mysql_native_password" where User='root';
Flush privileges.
flush privileges;
Exit MySQL.
exit
Now skip to Step 4 below.
MySQL 5.6 – Reset Root Password
For MySQL 5.6 on Ubuntu, run this command to change the root password. Replace your_password_here
with your own. (Generate a strong password here)
update user set Password=PASSWORD('<span class="red">your_password_here</span>') where user='root';
Change the auth plugin to mysql_native_password
.
update user set plugin="mysql_native_password" where User='root';
Flush privileges.
flush privileges;
Exit MySQL.
exit
Now skip to Step 4 below.
4. Test New Root Password
Make sure all MySQL processes are stopped before starting the service again.
sudo killall -u mysql
If you see a message similar to below, press ENTER
to continue.
2020-05-30T07:23:38.547616Z mysqld_safe mysqld from pid file /var/lib/mysql/ubuntu.pid ended
Start MySQL again.
sudo /etc/init.d/mysql start
Log in to MySQL again and you should now be prompted for a password.
sudo mysql -p -u root
Enter your MySQL root password. If correct, you should see something like:
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 8
Server version: 8.0.20-0ubuntu0.20.04.1 (Ubuntu)
Copyright (c) 2000, 2020, Oracle and/or its affiliates. All rights reserved.
Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
mysql>
You’re all done!
Let me know if this helped. Follow me on Twitter, Facebook and YouTube, or 🍊 buy me a smoothie.
Thanks
After hours, try a guide that works.
Ubuntu 23.04 MySQL 8.0.34
Thank you from Germany
Thanks for this! This helped me get back in to MySQL after I updated to Zabbix 6.4!
https://mysql.com
Thank you very much. I’ve been around the world twice trying to figure this out. Thank you!!!!!!!!!!!!!
thank you so much may God bless you
A new installation on Kubuntu 23.04 and I was expecting the usual interminable struggle but your guidance for mysql 8.0.33 wass PERFECT!
Many many thanks.
Thanks for this! This helped me get back in to MySQL after I updated to Zabbix 6.4!
Thanks a million times. I have been struggling to reset the password, which magically disappeared, for a while. Lots of “solutions” on the web have been unsuccessful until I discovered this one. Worked perfectly the first time. Kudos.
thank you very much, after many hours and tutorials that didn’t solve it, it finally worked.
Great, thankyou so much
This is an excellent help. Very much appreciated. However I still can’t connect my mysql workbench to MySQL database. It will be nice if you can bel on this,
Thanks very much
thank you so much mister.. but i have problem this
The phpMyAdmin configuration storage is not completely configured, some extended features have been deactivated. Find out why.
Or alternatively go to ‘Operations’ tab of any database to set it up there.
Open new phpMyAdmin window
mysqli::real_connect(): (HY000/1045): Access denied for user ‘phpmyadmin’@’localhost’ (using password: YES)
Connection for controluser as defined in your configuration failed.
Thanks. It solved my problem.
Yes!!! Thank You!
Thanks for the direction. It really helped and saved me a lot of production hours.
Thanks from Brussels, you saved my night (:
Thank you so much!!! God bless you
This is fantastic and thank you
Thanks a lot, worked perfectly!
Good work fella!! This is the best Howto for this problem that I found. And I tried a lot of them!
Super, Thank you so much.
very very nice. thanks a lot !