How to reset the root password in MySQL 8.0

If you never set, forgot, or need to change your MySQL password, you’re in luck. This article walks you through these steps, so you’ll never be at a loss for that database root user password.

$ sudo service mysql stop
$ sudo mkdir /var/run/mysqld
$ sudo chown mysql: /var/run/mysqld
$ sudo mysqld_safe --skip-grant-tables --skip-networking &
$ mysql -uroot 
mysql> UPDATE mysql.user SET authentication_string=null WHERE User='root';
mysql> flush privileges;
mysql> ALTER USER 'root'@'localhost' IDENTIFIED WITH mysql_native_password BY 'password';
mysql> flush privileges;
mysql> EXIT;
$ sudo killall -9 mysqld
$ sudo mysqladmin -S /var/run/mysqld/mysqld.sock shutdown
$ sudo service mysql start
4.6
Rated 4.6 out of 5
4.6 out of 5 stars (based on 17 reviews)
Excellent71%
Very good23%
Average6%
Poor0%
Terrible0%