Reset MySQL 5.7 root password on Ubuntu 16.04

First stop MySQL with command

service mysql stop

Now run

mysqld_safe --skip-grant-tables

On Ubuntu 16.04 server with MySQL 5.7, when i run, i get following error.

root@sok-06:~# mysqld_safe --skip-grant-tables
2018-12-29T06:13:26.191918Z mysqld_safe Logging to syslog.
2018-12-29T06:13:26.195961Z mysqld_safe Logging to '/var/log/mysql/error.log'.
2018-12-29T06:13:26.199396Z mysqld_safe Directory '/var/run/mysqld' for UNIX socket file don't exists.
root@sok-06:~#

To fix, this error, create a the folder and change ownership to user mysql

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

Now start a new terminal, login to MySQL with command

mysql -u root -p

You can press enter for password, you will be logged in with no password.

To reset MySQL root password, run

FLUSH PRIVILEGES;
ALTER USER 'root'@'localhost' IDENTIFIED BY 'YOUR_NEW_MYSQL_PW';
exit

Now kill the MySQL process with command

killall mysqld

Start MySQL normally with

service mysql start
Need help with Linux Server or WordPress? We can help!

Leave a Reply

Your email address will not be published. Required fields are marked *