我创建了个 mysql5.7 版本的 dokcer 容器取名 mysql ,但是我忘记了密码。

# 进入容器
docker exec -it mysql bash
# 设置跳过权限表的加载 
# 警告:这就意味着任何用户都能登录进来,并进行任何操作,相当不安全。
echo "skip-grant-tables" >> /etc/mysql/conf.d/docker.cnf
# 退出容器
exit
# 重启容器
docker restart mysql
# 再次进入容器
docker exec -it mysql bash
# 登录 mysql(无需密码)
mysql -uroot 
# 更新权限
flush privileges;
# 修改密码
alter user 'root'@'localhost' identified by '123456';
# 退出mysql
exit
# 替换掉刚才加的跳过权限表的加载参数
sed -i "s/skip-grant-tables/ /" /etc/mysql/conf.d/docker.cnf
# 退出容器
exit
# 重启容器
docker restart mysql

完成,现在可以用 root 和刚才设置的密码 登录

执行过程中出错了

mysql> alter user 'root'@'%' identified by '123456';
ERROR 1290 (HY000): The MySQL server is running with the --skip-grant-tables option so it cannot execute this statement

这个时候我们只需要 mysql> flush privileges ;

转载于:https://my.oschina.net/u/2322202/blog/3090119

Logo

权威|前沿|技术|干货|国内首个API全生命周期开发者社区

更多推荐