Docker MySQL官方镜像启动默认初始化创建数据库,docker MySQL初始化sql脚本执行,初始化用户sql脚本
version: "3.8"
networks:
vRouter:
external: true #外部网络
services:
mysql:
image: mysql:5.7.35
container_name: mysql
hostname: mysql
restart: always
ports:
- 3306:3306
networks:
vRouter:
aliases:
- mysql.server
command: [
'--character-set-server=utf8mb4',
'--collation-server=utf8mb4_unicode_ci'
]
environment:
- MYSQL_ROOT_PASSWORD=root
- LANG=C.UTF-8 #解决导入中文乱码问题
volumes:
- ./data:/var/lib/mysql
- ./conf.d:/etc/mysql/conf.d
- ./init.sql:/docker-entrypoint-initdb.d/init.sql
- /usr/share/zoneinfo/Asia/Shanghai:/etc/localtime
deploy:
resources:
limits:
memory: 1048M
关键配置
- ./init.sql:/docker-entrypoint-initdb.d/init.sql
说明:
在MySQL官方镜像中,
/docker-entrypoint-initdb.d/
目录下的sql脚本会在容器初次运行时候执行
根据上面的规则,只需要在init.sql文件中编写一个创建数据库的sql命令即可
create database typecho DEFAULT CHARACTER SET utf8 COLLATE utf8_general_ci;
官方原话
When a container is started for the first time, a new database with the specified name will be created and initialized with the provided configuration variables. Furthermore, it will execute files with extensions .sh, .sql and .sql.gz that are found in /docker-entrypoint-initdb.d. Files will be executed in alphabetical order. You can easily populate your mysql services by mounting a SQL dump into that directory and provide custom images with contributed data. SQL files will be imported by default to the database specified by the MYSQL_DATABASE variable.
意思就是挂载到/docker-entrypoint-initdb.d/
目录下面的文件(格式为.sh .sql sql.gz)都会执行
关联文章:
http://blog.xqlee.com/article/2312191918508462.html