There are definitely a lot of answers here, and on the Internet in general, about how to change the MySQL root
user password. The main takeaway I got from this is that:
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:
“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!
- 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;
- Once MySQL has started, login to MySQL as
root
with no password:
mysql -u root;
- 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.
- 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]';
TOOR
, but I cannot see where you have used that. Would you show us the actual SQL you are running? – Minutiae