MySQL – How to Reset MySQL Root Password in MySQL 8.0.36 on Red Hat 8

bashlinuxMySQLredhat-enterprise-linux

For years I have been using mysqld_safe (like this) with --skip-grant-tables so I can safely reset my MySQL root password:

sudo mysqld_safe  --skip-grant-tables &

But recently, while attempting to install MySQL 8.0.36 on Red Hat 8 I get:

mysqld_safe: command not found

What the heck? If the mysqld_safe command is not found, how am I supposed to set/change the root password?

Best Answer

There are definitely a lot of answers on the Internet pertaining to how to change the MySQL root user password. And the main takeaway I got from them in 2024 is this:

The mysqld_safe (binary/shell script) is definitley no longer a part of the MySQL 8 package (8.0.36) in Red Hat 8 and possible other Linux distros.

It might be gone (as a binary/shell script) from the MySQL install on systemd managed systems since at least MySQL 5.7.6 according to this other answer on Stack Overflow:

“As of MySQL 5.7.6, for MySQL installation using an RPM distribution, server startup and shutdown is managed by systemd on several Linux platforms. On these platforms, mysqld_safe is no longer installed because it is unnecessary. For more information, see Section 2.5.10, “Managing MySQL Server with systemd”.”

Regardless of the reason, if you get this message when attempting to run mysqld_safe:

mysqld_safe: command not found

And your system runs systemd (aka: systemctl) then this is definitely the modern answer that will solve your problems in 4 easy steps! Here we go!

  1. Stop mysqld and restart it with the proper MYSQLD_OPTS:
    sudo service mysqld stop;
    sudo systemctl set-environment MYSQLD_OPTS="--skip-grant-tables --skip-networking";
    sudo service mysqld start;
    
  2. Once MySQL has started, login to MySQL as root with no password:
    mysql -u root;
    
  3. Now, in MySQL run these commands; note that [password] should be changed to whatever MySQL password you want it yo be set to:
    FLUSH PRIVILEGES;
    ALTER USER 'root'@'localhost' IDENTIFIED WITH caching_sha2_password BY '[password]';
    FLUSH PRIVILEGES;
    exit;
    
    Note, I believe the first FLUSH PRIVILEGES; is connected starting up MySQL with the MYSQLD_OPTS set to --skip-grant-tables. Regardless, running FLUSH PRIVILEGES; doesn’t pose any risk/danger.
  4. Now, roll back the MYSQLD_OPTS and restart MySQL as follows:
    sudo service mysqld stop;
    sudo systemctl unset-environment MYSQLD_OPTS;
    sudo service mysqld start;
    

Now with all that done, try to login to MySQL as root with a password like this:

mysql -u root -p

Hit Enter/Return, then enter your newly set root password and… et voilà! You should be logged into MySQL as root!


Note: I chose to set the password with caching_sha2_password authentication plugin, but if you want to be more modern, you can use mysql_native_password as follows:

ALTER USER 'root'@'localhost' IDENTIFIED WITH mysql_native_password BY '[password]';
Related Question