Tuesday, February 22, 2022

How to Reset the Root Password in MySQL 8 on Ubuntu 20.04 Server

First you need to stop the regular mysql service:

# service mysql stop

Next, you need to create a directory for the runtime of the safe_mysqld process:

# mkdir /var/run/mysqld

Then you need to make sure the newly created mysqld runtime file is owned by mysql

# chown mysql:mysql /var/run/mysqld

Then you need to start the safe_mysql process (without authentication checking and without networking, while running the background)

# mysqld_safe --skip-grant-tables --skip-networking &
<press ENTER>

Then you need to log into the safe_mysql process:

# mysql -u root -p
<press ENTER>

Then you need to update the root password:

mysql> UPDATE mysql.user SET authentication_string=null WHERE User='root';
mysql> FLUSH PRIVILEGES;
mysql> exit

Then you need to stop the safe_mysql process (and any stray mysqld processes as well)

#killall mysqld_safe mysqld

Then you need to restart the regular mysql service:

# service mysql start

Then you need to log into the regular mysql service:

# mysql -u root -p
<press ENTER>

Then you need to update the root password in the regular mysql:

mysql> ALTER USER 'root'@'localhost' IDENTIFIED BY '<password>';
mysql> FLUSH PRIVILEGES
mysql> exit

Finally, test that the password change was successful by logging into the mysql service normally

# mysql -u root -p
<enter password then press ENTER>

No comments:

Post a Comment