docker-compose之mysql主从
一、通过一个工程来呈现打包过程和建立过程。
二、打包使用maven-assembly-plugin,配置如下
<?xml version='1.0' encoding='UTF-8'?> <assembly xmlns="http://maven.apache.org/ASSEMBLY/2.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/ASSEMBLY/2.0.0 http://maven.apache.org/xsd/assembly-2.0.0.xsd"> <!--打包名称,唯一标识--> <id>${project.build.finalName}</id> <!--打包格式,可以手动修改--> <formats> <format>tar.gz</format> </formats> <!-- 文件 --> <fileSets> <!-- 配置 --> <fileSet> <directory>${project.basedir}/config</directory> <outputDirectory>config</outputDirectory> <includes> <include>**</include> </includes> <excludes> <exclude>Dockerfile/*.yml</exclude> <exclude>assembly/**</exclude> </excludes> </fileSet> <!-- docker-compose --> <fileSet> <directory>${project.basedir}/config/Dockerfile</directory> <outputDirectory>/</outputDirectory> <includes> <include>*.yml</include> </includes> </fileSet> </fileSets> </assembly>
三、mysql主从的dockerfile
1、master
FROM mysql:8.0.19 MAINTAINER xbd RUN ln -sf /usr/share/zoneinfo/Asia/Shanghai /etc/localtime COPY ./config/db/xbd_master.sql /docker-entrypoint-initdb.d
xbd_master.sql:
CREATE USER 'root'@'xbd-slaver' IDENTIFIED BY 'root'; grant replication slave, replication client on *.* to 'root'@'xbd-slaver'; flush privileges;
2、slaver
FROM mysql:8.0.19 MAINTAINER xbd RUN ln -sf /usr/share/zoneinfo/Asia/Shanghai /etc/localtime COPY ./config/db/xbd_slaver.sql /docker-entrypoint-initdb.d
xbd_slaver.sql:
change master to master_host='xbd-master', master_user='root',master_password='root',master_port=3306;
start slave;
四、docker-compose.yml相关配置,主要是主从相关配置
version: "2" services: xbd-master: build: context: ./ dockerfile: ./config/Dockerfile/Dockerfile-master image: xbd-master restart: always container_name: xbd-master volumes: - /var/lib/mysql/xbd-master:/var/lib/mysql ports: - 3306:3306 environment: MYSQL_ROOT_PASSWORD: root privileged: true command: ['--server-id=1', '--log-bin=xbd-master-bin', '--binlog-ignore-db=mysql', '--binlog_cache_size=256M', '--binlog_format=mixed', '--lower_case_table_names=1', '--character-set-server=utf8', '--collation-server=utf8_general_ci', '--sql_mode=STRICT_TRANS_TABLES,NO_ZERO_IN_DATE,NO_ZERO_DATE,ERROR_FOR_DIVISION_BY_ZERO,NO_ENGINE_SUBSTITUTION'] xbd-slaver: build: context: ./ dockerfile: ./config/Dockerfile/Dockerfile-slaver image: xbd-slaver restart: always container_name: xbd-slaver volumes: - /var/lib/mysql/xbd-slaver:/var/lib/mysql ports: - 3307:3306 environment: MYSQL_ROOT_PASSWORD: root privileged: true command: ['--server-id=2', '--relay_log=xbd-slaver-relay', '--lower_case_table_names=1', '--character-set-server=utf8', '--collation-server=utf8_general_ci', '--sql_mode=STRICT_TRANS_TABLES,NO_ZERO_IN_DATE,NO_ZERO_DATE,ERROR_FOR_DIVISION_BY_ZERO,NO_ENGINE_SUBSTITUTION'] depends_on: - xbd-master
说明:主要配置
master:
# 服务ID,唯一值 server-id=1 # binlog日志名称 log-bin=xbd-master-bin.log # 排除备份数据库,可多个 binlog-ignore-db=mysql # binlog缓存长度,循环写入 binlog_cache_size=256M # 格式 STATEMENT/ROW/MIXED(默认) binlog_format=mixed
slaver:
# 服务唯一ID server-id=2 # 中继日志名称 relay_log=xbd-slaver-relay.log
五、测试:
六、总结:1)master的sql初始化的目的是建立账户,提供备份访问方式。2)slaver的sql开启备份3)docker-compose.yml的主要配置主要是配置主从相关。