docker-compos mysql5.7主从配置
docker-compose目录结构
配置文件
master/my.cnf
[client]
port = 3306
socket = /var/run/mysqld/mysqld.sock[mysqld_safe]
pid-file = /var/run/mysqld/mysqld.pid
socket = /var/run/mysqld/mysqld.sock
nice = 0[mysqld]
user = mysql
pid-file = /var/run/mysqld/mysqld.pid
socket = /var/run/mysqld/mysqld.sock
port = 3306
basedir = /usr
datadir = /var/lib/mysql
tmpdir = /tmp
secure-file-priv=/var/lib/mysql-files
lc-messages-dir = /usr/share/mysql
explicit_defaults_for_timestamplog-bin = mysql-bin
server-id = 1
slave/my.cn
[client]
port = 3306
socket = /var/run/mysqld/mysqld.sock[mysqld_safe]
pid-file = /var/run/mysqld/mysqld.pid
socket = /var/run/mysqld/mysqld.sock
nice = 0[mysqld]
user = mysql
pid-file = /var/run/mysqld/mysqld.pid
socket = /var/run/mysqld/mysqld.sock
port = 3306
basedir = /usr
datadir = /var/lib/mysql
tmpdir = /tmp
secure-file-priv=/var/lib/mysql-files
lc-messages-dir = /usr/share/mysql
explicit_defaults_for_timestamplog-bin = mysql-bin
server-id = 2
docker-compose.yml文件
version: '3.2'
services:mysql-master:image: "mysql:5.7"container_name: mysql-masterrestart: alwaysprivileged: trueenvironment:MYSQL_ROOT_PASSWORD: 123456 #主库root用户的密码command:--default-authentication-plugin=mysql_native_password--character-set-server=utf8mb4--collation-server=utf8mb4_general_ci--explicit_defaults_for_timestamp=true--lower_case_table_names=1--max_allowed_packet=128M;ports:- 3306:3306 #映射宿主机端口:容器端口volumes: #宿主机配置目录或文件挂载到容器- ./master/my.cnf:/etc/mysql/my.cnf- ./master/mysql-files:/var/lib/mysql-files- ./master/data:/var/lib/mysql- ./master/log:/var/log/networks:mysql-network:ipv4_address: 10.2.0.2mysql-slave:image: "mysql:5.7"container_name: mysql-slaverestart: alwaysprivileged: trueenvironment:MYSQL_ROOT_PASSWORD: 123456 #从库root用户的密码command:--default-authentication-plugin=mysql_native_password--character-set-server=utf8mb4--collation-server=utf8mb4_general_ci--explicit_defaults_for_timestamp=true--lower_case_table_names=1--max_allowed_packet=128M;ports:- 3307:3306 #映射宿主机端口:容器端口volumes: #宿主机配置目录或文件挂载到容器- ./slave/my.cnf:/etc/mysql/my.cnf- ./slave/mysql-files:/var/lib/mysql-files- ./slave/data:/var/lib/mysql- ./slave/log:/var/log/networks:mysql-network:ipv4_address: 10.2.0.3
networks: #定义容器连接网络mysql-network:driver: bridgeipam:driver: defaultconfig:- subnet: 10.2.0.0/24
运行
在配置文件目录下使用docker compose up -d
运行
主节点的mysql配置:
进入主节点docker容器
docker exec -it mysql-master bash
mysql -uroot -p123456
#创建账号用于从机连接主机
GRANT REPLICATION SLAVE ON *.* TO 'repl'@'%' identified by '123456';
权限立刻生效
flush privileges;
查看binglog信息
show master status \G;
从节点的mysql配置:
进入从节点
docker exec -it mysql-slave bash
mysql -uroot -p123456
配置连接
下面几个参数需要与主节点的信息对应
CHANGE MASTER TO MASTER_HOST='10.2.0.2', MASTER_USER='repl', MASTER_PASSWORD='123456', MASTER_LOG_FILE='mysql-bin.000003', MASTER_LOG_POS=589;
启动服务
start slave;
查看状态
show slave status \G;