1.需要的软件及版本
MySQL镜像版本 8.0
Docker版本 19.03.9
Docker-compose版本 1.25.5
2.准备配置文件
2.1 docker-compose.yml文件
稍后的命令
docker-compose up -d
会根据此文件创建docker网络,并根据其中的参数创建名为mysql_master和mysql_slave的两个容器
version: '2'
services:
mysql-master:
image: docker.io/mysql
networks:
jznet:
ipv4_address: 172.18.4.24
volumes:
- /root/data/mysql-master:/var/lib/mysql
- /home/xinyue/master.cnf:/etc/mysql/my.cnf
ports:
- "3306:3306"
environment:
- MYSQL_DATABASE=root
- MYSQL_ROOT_PASSWORD=123456
mysql-slave:
image: docker.io/mysql
networks:
jznet:
ipv4_address: 172.18.4.25
volumes:
- /root/data/mysql-slave:/var/lib/mysql
- /home/xinyue/slave.cnf:/etc/mysql/my.cnf
ports:
- "3307:3306"
environment:
- MYSQL_DATABASE=root
- MYSQL_ROOT_PASSWORD=123456
networks:
jznet:
driver: bridge
ipam:
driver: default
config:
- subnet: 172.18.4.0/26
2.2 Master主库配置文件
Master的配置文件,保存在/home/xinyue/目录下,名称为master.cnf
# Copyright (c) 2017, Oracle and/or its affiliates. All rights reserved.
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; version 2 of the License.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
#
# The MySQL Server configuration file.
#
# For explanations see
# http://dev.mysql.com/doc/mysql/en/server-system-variables.html
[mysqld]
pid-file = /var/run/mysqld/mysqld.pid
socket = /var/run/mysqld/mysqld.sock
datadir = /var/lib/mysql
secure-file-priv= NULL
server-id=1
# Disabling symbolic-links is recommended to prevent assorted security risks
symbolic-links=0
# Custom config should go here
!includedir /etc/mysql/conf.d/
2.3 Slave主库配置文件
Slave的配置文件,保存在/home/xinyue/目录下,名称为slave.cnf
# Copyright (c) 2017, Oracle and/or its affiliates. All rights reserved.
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; version 2 of the License.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation,