Deploy mysql using Docker
-
- Preface
- 1. Build the mysql file directory structure
- 2. The initialized sql file is placed in the db file directory.
- 3.dockfile path and content
- 4. Create an environment variable file in the outermost layer (refer to the screenshot above, created under the home level)
- 5.docker-compose file
- 6. Write sh execution script
Preface
基于若依框架,慢慢学习,总结笔记
1. Build the mysql file directory structure
2. The initialized sql file is placed in the db file directory.
3.dockfile path and content
# 基础镜像
FROM mysql:5.7
# author
MAINTAINER ruoyi
# 执行sql脚本
ADD ./db/*.sql /docker-entrypoint-initdb.d/
4. Create an environment variable file in the outermost layer (refer to the screenshot above, created under the home level)
- File name: hosts-prod (used for docker-compose and other files to obtain unified environment variables)
127.0.0.1 master
本机内网IP mysql.host
- File name: prod.env (used for environment variable settings when the sh script is started using docker-compose)
version=1.0.0 #当前版本
active=prod #测试环境test 生产环境prod
5.docker-compose file
# 描述 Compose 文件的版本信息
version : '3.8'
# 定义服务,可以多个
services:
mysql: # 服务名称
container_name: mysql # 容器名称
image: mysql:5.7 # 创建容器时所需的镜像
build:
context: ./mysql # 可以是dockerfile路径,或者时执行git仓库的url地址
network_mode: "host" # host 网络模式
volumes: # 数据卷,目录挂载
- ./mysql/logs:/logs
- ./mysql/data:/var/lib/mysql
- ./mysql/conf:/etc/mysql/conf.d
- "./../hosts-${active}:/etc/hosts" # 获取环境变量映射进容器内
command: [ # 执行命令,会覆盖容器启动后默认执行的命令(会覆盖dockefile中的CMD指令)
'mysqld',
'--innodb-buffer-pool-size=80M',
'--character-set-server=utf8mb4',
'--collation-server=utf8mb4_unicode_ci',
'--default-time-zone=+8:00',
'--lower-case-table-names=1',
'--sql_mode=STRICT_TRANS_TABLES,NO_ZERO_IN_DATE,NO_ZERO_DATE,ERROR_FOR_DIVISION_BY_ZERO,NO_ENGINE_SUBSTITUTION' # 设置所支持的SQL语法
]
environment: # 添加环境变量
MYSQL_DATABASE: '数据库名'
MYSQL_ROOT_PASSWORD: "数据库密码"
restart: always # 容器总是重新启动
6. Write sh execution script
docker-compose -f docker-compose-mysql.yml --env-file ./../prod.env -p mysql down
docker-compose -f docker-compose-mysql.yml --env-file ./../prod.env -p mysql build
docker-compose -f docker-compose-mysql.yml --env-file ./../prod.env -p mysql up -d
Next, just execute the sh script. Pay attention to see whether the deployment is successful.