一、概述
官方:
https://hub.docker.com/_/mysql
注意:
-
容器启动以后,mysql需要等待一会儿才能使用。
-
映射生成的文件都是文件夹,删除my.cnf文件夹,新建my.cnf空白文件,删掉容器重启。
-
重新部署记得清除数据。
-
删除所有停止服务:
docker-compose rm
docker-compose脚本
mysql5.7
默认加密插件:mysql_native_password
手动指定:command中加参数--default-authentication-plugin=mysql_native_password
version: '3'
services:
mysql:
image: "mysql:5.7"
privileged: true
container_name: "docker-mysql-5.7"
restart: always
ports:
- "23306:3306"
environment:
MYSQL_ROOT_PASSWORD: "111111"
MYSQL_DATABASE: "test"
MYSQL_USER: "wangfugui"
MYSQL_PASSWORD: "123456"
TZ: Asia/Shanghai
command:
--wait_timeout=28800
--interactive_timeout=28800
--max_connections=1000
volumes:
- "./mysql-5.7/data:/var/lib/mysql"
- "./mysql-5.7/config/my.cnf:/etc/mysql/my.cnf"
mysql8.0.30
默认加密插件:caching_sha2_password
version: '3'
services:
mysql:
image: "mysql:8.0.30"
privileged: true
container_name: "docker-mysql-8.0.30"
restart: always
ports:
- "23306:3306"
environment:
MYSQL_ROOT_PASSWORD: "111111"
MYSQL_DATABASE: 'test'
MYSQL_USER: 'wangfugui'
MYSQL_PASSWORD: '123456'
TZ: Asia/Shanghai
command:
--wait_timeout=28800
--interactive_timeout=28800
--max_connections=1000
volumes:
- "./mysql-8.0.30/data:/var/lib/mysql"
- "./mysql-8.0.30/config/my.cnf:/etc/mysql/my.cnf"
mysql:latest
最新版配置文件路径发生了变化:/var/lib/mysql-files
version: '3'
services:
mysql:
image: "mysql:latest"
privileged: true
container_name: "docker-mysql-latest"
restart: always
ports:
- "23306:3306"
environment:
MYSQL_ROOT_PASSWORD: "111111"
MYSQL_DATABASE: 'test'
MYSQL_USER: 'wangfugui'
MYSQL_PASSWORD: '123456'
TZ: Asia/Shanghai
command:
--wait_timeout=28800
--interactive_timeout=28800
--max_connections=1000
volumes:
- "./mysql-latest/data:/var/lib/mysql-files"
- "./mysql-latest/config/my.cnf:/etc/mysql/my.cnf"
文件拷贝
# 传入
# docker cp /路径/文件名 容器ID:/上传路径
docker cp db.sql 0236abb0865b:/root
# 传出
# docker cp 容器ID:/上传路径 /路径/文件名
导入数据
# 进入容器
docker exec -it 0236abb0865b /bin/bash
mysql -uroot -P23306 -p
use test;
> source /root/db.sql
异常
问题:拉取镜像失败。
ERROR: error pulling image configuration: Get https://production.cloudflare.docker.com/registry-v2/docker/registry/v2/blobs/sha256/ff/ff3b5098b416cc4294d8d5c43c2f0f8251e91711347318e73cb290ffe2783bcb/data?verify=1662001548-xUBxwxWg3NEn3np%2BUeFhOhQ1pow%3D: Service Unavailable
解决:
vim /etc/resolv.conf
nameserver 8.8.8.8
service docker restart
问题:无法启动。
ERROR: for docker_mysql Cannot start service mysql: OCI runtime create failed: container_linux.go:380: starting container process caused: process_linux.go:545: container init caused: rootfs_linux.go:76: mounting "/host_mnt/Users/wangfugui/Downloads/dev/middleware/db/docker/mysql/conf/my.cnf" to rootfs at "/etc/my.cnf" caused: mount through procfd: not a directory: unknown: Are you trying to mount a directory onto a file (or vice-versa)? Check if the specified host path exists and is the expected type
解决:
映射生成的文件都是文件夹,my.cnf改成文件即可。