问题产生
第一次使用docker创建mysql的时候,拉取的为最新镜像 8.0.27,我并不知道5.7和8.0的mysql使用是有区别的。
区别在于8.0的版本如果设置了文件挂载,那么需要指定 /var/lib/mysql-files的外部目录,如果没有指定,将会产生如下错误:
docker logs 容器id
查看错误日志
2022-03-23 07:20:02+00:00 [Note] [Entrypoint]: Entrypoint script for MySQL Server 8.0.27-1debian10 started.
2022-03-23 07:20:02+00:00 [Note] [Entrypoint]: Switching to dedicated user ‘mysql’
2022-03-23 07:20:02+00:00 [Note] [Entrypoint]: Entrypoint script for MySQL Server 8.0.27-1debian10 started.
2022-03-23 07:20:02+00:00 [Note] [Entrypoint]: Initializing database files
2022-03-23T07:20:02.969389Z 0 [System] [MY-013169] [Server] /usr/sbin/mysqld (mysqld 8.0.27) initializing of server in progress as process 43
2022-03-23T07:20:02.976288Z 1 [System] [MY-013576] [InnoDB] InnoDB initialization has started.
2022-03-23T07:20:03.322083Z 1 [System] [MY-013577] [InnoDB] InnoDB initialization has ended.
2022-03-23T07:20:04.037840Z 0 [Warning] [MY-013746] [Server] A deprecated TLS version TLSv1 is enabled for channel mysql_main
2022-03-23T07:20:04.037870Z 0 [Warning] [MY-013746] [Server] A deprecated TLS version TLSv1.1 is enabled for channel mysql_main
2022-03-23T07:20:04.147741Z 6 [Warning] [MY-010453] [Server] root@localhost is created with an empty password ! Please consider switching off the --initialize-insecure option.
2022-03-23 07:20:06+00:00 [Note] [Entrypoint]: Database files initialized
2022-03-23 07:20:06+00:00 [Note] [Entrypoint]: Starting temporary server
mysqld: Error on realpath() on ‘/var/lib/mysql-files’ (Error 2 - No such file or directory)
2022-03-23T07:20:06.479553Z 0 [ERROR] [MY-010095] [Server] Failed to access directory for --secure-file-priv. Please make sure that directory exists and is accessible by MySQL Server. Supplied value : /var/lib/mysql-files
2022-03-23 07:20:06+00:00 [ERROR] [Entrypoint]: Unable to start server.
解决办法
指定 /var/lib/mysql-files的外部目录
全部流程
# 停止容器
sudo docker stop 容器id
# 删除容器
sudo docker rm 容器id
# 拉取mysql镜像
sudo docker pull mysql:8.0.27
# 重新启动容器
sudo docker run -p 3306:3306 --name mysql \
-v /mydata/mysql/conf:/etc/mysql \
-v /mydata/mysql/logs:/var/log/mysql \
-v /mydata/mysql/data:/var/lib/mysql \
-v /mydata/mysql/mysql-files:/var/lib/mysql-files \
-e MYSQL_ROOT_PASSWORD=root \
-d mysql
接下来就是进入容器,修改mysql密码和访问权限
docker exec -it mysql /bin/bash # 进入mysql容器终端
mysql -uroot -proot # 如果提示密码错误 尝试不加密码 mysql -uroot
use mysql;
alter USER 'root'@'localhost' IDENTIFIED BY '你的密码';
# 允许远程连接root
update user set host = "%" where user='root';
flush privileges;
然后使用Navicat连接数据库即可
–end–