记录一次设置mysql数据库root密码的经历:当我下载mysql8后,想要设置root密码。搜索网上教程,一般说是用mysqld_safe采用安全启动方式启动后进行设置:mysqld_safe --skip-grant-tables &。但是我采用这种方法,报错:cannot find mysqld_safe

搜了一圈,一般都是说是mysqld_safe没放到PATH,数据库没安装对等问题。直到后面我看到一个帖子(https://superuser.com/questions/1837653/how-can-i-reset-my-mysql-root-password-in-mysql-8-0-36-on-red-hat-8-if-mysqld-s),才解决了我的疑惑。

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.

原来不是每一个mysql都会拥有mysqld_safe,感觉就是我的情况呢!于是跟着帖子的步骤操作,成功修改root密码了。步骤如下: 1、设置MYSQLD_OPTS安全模模式,并启动

1
2
3
sudo service mysqld stop;
sudo systemctl set-environment MYSQLD_OPTS="--skip-grant-tables --skip-networking";
sudo service mysqld start;

2、无需密码登陆root

1
mysql -u root;

3、修改root密码

1
2
3
4
FLUSH PRIVILEGES;
ALTER USER 'root'@'localhost' IDENTIFIED WITH caching_sha2_password BY '[password]';
FLUSH PRIVILEGES;
exit;

4、还原配置,重启

1
2
3
sudo service mysqld stop;
sudo systemctl unset-environment MYSQLD_OPTS;
sudo service mysqld start;

perfect!

btw,我的mysql版本:

1
2
mysql --version                                                                                              
mysql  Ver 8.0.37 for Linux on x86_64 (MySQL Community Server - GPL)