docker mysql8 忘记root密码解决方法
使用docker 搭建mysql ,docker-compose.yml
version: "2.1"
services:
mysql:
image: mysql:8.0.35
container_name: mysql8
environment:
MYSQL_ROOT_PASSWORD: 2pq8zWaT@zuM
ports:
- 3306:3306
volumes:
- /opt/data/docker-data/mysql8/conf/my.cnf:/etc/mysql/conf.d/my.cnf
- /opt/data/docker-data/mysql8/data:/var/lib/mysql
- /opt/data/docker-data/mysql8/logs:/var/log/mysql
restart: always
启动后,不知什么回事,root密码不对了,解决方法如下:
1、mysql 配置文件my.cnf, mysqld分组下增加一行,即编辑文件 /opt/data/docker-data/mysql8/conf/my.cnf
skip-grant-tables
2、重启mysql,docker restart mysql8
4、进入mysql :mysql -uroot -p 不用输密码,直接回车,进入mysql
5、授权
FLUSH PRIVILEGES; -- 删除原来用户 DROP USER 'root'@'localhost'; DROP USER 'root'@'%'; -- 新增用户并设置密码 CREATE USER 'root'@'%' IDENTIFIED WITH mysql_native_password BY 'DW2Qn5vRBqpE'; CREATE USER 'root'@'localhost' IDENTIFIED WITH mysql_native_password BY 'DW2Qn5vRBqpE'; -- 授权所有数据库 GRANT ALL PRIVILEGES ON *.* TO 'root'@'localhost' WITH GRANT OPTION; GRANT ALL PRIVILEGES ON *.* TO 'root'@'%' WITH GRANT OPTION; FLUSH PRIVILEGES;