1、编辑/etc/my.cnf文件,在末尾出增加 skip-grant-tables
bash
[mysqld]
datadir=/opt/data1/mysql8/data
socket=/var/lib/mysql/mysql.sock
log-error=/var/log/mysql/mysqld.log
pid-file=/run/mysqld/mysqld.pid
bind-address=0.0.0.0
skip-grant-tables
2、 重启mysql服务
systemctl restart mysqld
3、登录mysql,并重置root密码为空
bash
mysql -uroot
use mysql;
update user set authentication_string='' where User='root';
flush privileges;
quit;
4、将 /etc/my.cnf 中 的 skip-grant-tables 去掉;
5、再次重启mysql服务
systemctl restart mysqld
6、再次登录mysql,登录密码为空,然后修改root密码
mysql -uroot
use mysql;
ALTER user 'root'@'%' IDENTIFIED WITH mysql_native_password BY '你的密码';
flush privileges;