1. 安装 Docker-compose
curl -L "https://github.com/docker/compose/releases/download/v2.17.2/docker-compose-\$(uname -s)-\$(uname -m)" -o /usr/local/bin/docker-compose
chmod +x /usr/local/bin/docker-compose
ln -s /usr/local/bin/docker-compose /usr/bin/docker-compose
2.拉取镜像
docker pull registry.cn-hangzhou.aliyuncs.com/ns-2023/lib-2023:mysql-5.7
3. 配置文件 my.conf
[mysqld]
user=mysql # MySQL启动用户
default-storage-engine=INNODB # 创建新表时将使用的默认存储引擎
character-set-server=utf8mb4 # 设置mysql服务端默认字符集
pid-file = /var/run/mysqld/mysqld.pid # pid文件所在目录
socket = /var/run/mysqld/mysqld.sock # 用于本地连接的socket套接字
datadir = /var/lib/mysql # 数据文件存放的目录
#log-error = /var/log/mysql/error.log
#bind-address = 127.0.0.1 # MySQL绑定IP
# Disabling symbolic-links is recommended to prevent assorted security risks
symbolic-links=0
sql_mode=STRICT_TRANS_TABLES,NO_ZERO_IN_DATE,NO_ZERO_DATE,ERROR_FOR_DIVISION_BY_ZERO,NO_ENGINE_SUBSTITUTION # 定义mysql应该支持的sql语法,数据校验等!
# 允许最大连接数
max_connections=200
# ================= ↓↓↓ mysql主从同步配置start ↓↓↓ =================
# 同一局域网内注意要唯一
server-id=3306
# 开启二进制日志功能 & 日志位置存放位置`/var/lib/mysql`
#log-bin=mysql-bin
log-bin=/var/lib/mysql/mysql-bin
# binlog格式
# 1. STATEMENT:基于SQL语句的模式,binlog 数据量小,但是某些语句和函数在复制过程可能导致数据不一致甚至出错;
# 2. MIXED:混合模式,根据语句来选用是 STATEMENT 还是 ROW 模式;
# 3. ROW:基于行的模式,记录的是行的完整变化。安全,但 binlog 会比其他两种模式大很多;
binlog_format=ROW
# FULL:binlog记录每一行的完整变更 MINIMAL:只记录影响后的行
binlog_row_image=FULL
# 日志文件大小
# max_binlog_size=1G
max_binlog_size=100M
# 定义清除过期日志的时间(这里设置为7天)
expire_logs_days=7
# ================= ↑↑↑ mysql主从同步配置end ↑↑↑ =================
[mysql]
default-character-set=utf8mb4
[client]
default-character-set=utf8mb4 # 设置mysql客户端默认字符集
4. docker-compose.yml
version: "3"
services:
mysql:
image: registry.cn-hangzhou.aliyuncs.com/ns-2023/lib-2023:mysql-5.7 # 原镜像`mysql:5.7`
container_name: mysql_3306 # 容器名为'mysql_3306'
restart: unless-stopped # 指定容器退出后的重启策略为始终重启,但是不考虑在Docker守护进程启动时就已经停止了的容器
volumes: # 数据卷挂载路径设置,将本机目录映射到容器目录
- "./mysql/my.cnf:/etc/mysql/my.cnf"
- "./mysql/data:/var/lib/mysql"
# - "./mysql/conf.d:/etc/mysql/conf.d"
- "./mysql/log/mysql/error.log:/var/log/mysql/error.log"
- "./mysql/docker-entrypoint-initdb.d:/docker-entrypoint-initdb.d" # 可执行初始化sql脚本的目录 -- tips:`/var/lib/mysql`目录下无数据的时候才会执行(即第一次启动的时候才会执行)
environment: # 设置环境变量,相当于docker run命令中的-e
TZ: Asia/Shanghai
LANG: en_US.UTF-8
MYSQL_ROOT_PASSWORD: root # 设置root用户密码
MYSQL_DATABASE: demo # 初始化的数据库名称
ports: # 映射端口
- "3306:3306"
mysql/docker-entrypoint-initdb.d/init.sql
SET NAMES utf8mb4;
SET FOREIGN_KEY_CHECKS = 0;
create database zq;
SET FOREIGN_KEY_CHECKS = 1;
5. 运行
docker-compose -f docker-compose.yml -p mysql57 up -d
docker exec -it mysql_3306 /bin/bash
mysql -uroot -proot
标签:bin,compose,log,mysql5.7,mysql,var,docker,44
From: https://www.cnblogs.com/ckh2023/p/18070158