1. 停止mysql服务
1
sudo systemctl stop mysql

如果是docker环境中可以直接stop容器

1
docker stop mysql
  1. 编辑mysql配置文件,在mysqld配置片段中添加:
1
skip-grant-tables
  1. 重新启动mysql服务
1
sudo systemctl start mysql

docker环境:

1
docker start mysql
  1. 无密码登录mysql
1
mysql -u root

如果是docker环境先进入mysql容器内部

1
docker exec -it mysql bash
  1. 设置新密码
1
2
FLUSH PRIVILEGES;
ALTER USER 'root'@'%' IDENTIFIED BY '新密码';
  1. 退出并重新启动mysql服务,并还原第二步添加的配置。
1
systemctl restart mysql

docker环境执行如下命令:

1
docker restart mysql