How to reset MySQL 8 root password on CentOS 7 and 8

Occasionally you come up with the world's most unbreakable root password for a MySQL 8 installation and you get distracted before you record it somewhere.... and you remain distracted for 3 months.

Then you return to the project where this database was needed and ... whoops! What was that totally awesome password?

Here's how you can reset your root password. This was based on a page that had a similar instructions except none of them worked - lots of errors and incorrect names - like referring to mysql instead of mysqld when stopping the service.

Here we go:

All as root user:

Create a file in /var/lib/mysql called mysql-pwd with the following contents:

ALTER USER 'root'@'localhost' IDENTIFIED BY 'anotherPwd28^$94';

Shutdown mysql
  systemctl stop mysqld

Run MySQL executing the given init file under the mysql user
  mysqld --init-file=/var/lib/mysql/mysql-pwd --user=mysql &

Check the MySQL log to make sure no errors occurred:

  vi /var/log/mysqld.log

Stop the mysqld that was started directly by first finding its PROCESSID:

  ps -efa | grep mysqld

Stop it:

  kill PROCESSID

Start up mysql the normal systemd way:
  systemctl start mysqld

Log in using the password contained in the mysql-pwd file
  mysql -u root -p

You should now be logged in as root user

Edit the mysql-pwd file to remove the password value so that root password is not available to people viewing that file.

Comments

Post a Comment

Please add your comment:

Popular posts from this blog

Java package name structure and organization - best practice and conventions

Classic software engineering mistakes: To Greenfield or Refactor Legacy code?