docker-compose 实现mysql主从节点

step1:

  • 首先docker-compose搭建三个mysql, 基础操作
services:
    mysql-master:
       image: mysql:5.7.18
       networks:
          mysqlnet:
            ipv4_address: 172.19.4.24
       environment:
         - MYSQL_ROOT_PASSWORD=123456
       ports:
         - "3307:3306"
       volumes:
         - /var/lib/mysql1:/var/lib/mysql
         - /root/mysqlCluster/conf/my-m.cnf:/etc/mysql/my.cnf:ro
       privileged: true
       restart: always
    mysql-slave1:
       image: mysql:5.7.18
       networks:
          mysqlnet:
            ipv4_address: 172.19.4.25
       environment:
         - MYSQL_ROOT_PASSWORD=123456
       ports:
         - "3308:3306"
       volumes:
         - /var/lib/mysql2:/var/lib/mysql
         - /root/mysqlCluster/conf/my-s1.cnf:/etc/mysql/my.cnf:ro
       privileged: true
       restart: always
       depends_on:
         - mysql-master
    mysql-slave2:
       image: mysql:5.7.18
       networks:
          mysqlnet:
            ipv4_address: 172.19.4.26
       environment:
         - MYSQL_ROOT_PASSWORD=123456
       ports:
         - "3309:3306"
       depends_on:
         - mysql-master
       volumes:
         - /var/lib/mysql3:/var/lib/mysql
         - /root/mysqlCluster/conf/my-s2.cnf:/etc/mysql/my.cnf:ro
       privileged: true
       restart: always
networks:
  mysqlnet:
    driver: bridge
    ipam:
      driver: default
      config:
      - subnet: 172.19.4.0/26

step2

  • 映射my.conf
  • master节点:my-m.conf
# 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]
log-bin=mysql-bin
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
!includedir /etc/mysql/conf.d/
  • slave节点
  • my-s1.conf
# 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=2
relay_log_recovery=0
# Disabling symbolic-links is recommended to prevent assorted security risks
symbolic-links=0

# Custom config should go here
!includedir /etc/mysql/conf.d/

slave2 同上区别在 server-id=2这儿不能重复

step3

docker-compose up -d

step4 主节点操作

docker-compose exec mysql-master /bin/sh
mysql -uroot -p123456

或用navicat 输入以下

GRANT REPLICATION SLAVE ON *.* TO '在这儿设置你的账号'@'%' IDENTIFIED BY '这儿设置你的密码';
可以通过
show master status;
查看状态

step5 slave节点操作

  • 首先进入slave1 节点
stop slave;
CHANGE MASTER TO
MASTER_HOST='172.19.4.24',
MASTER_PORT=3306,
MASTER_USER='你设置的账号',
MASTER_PASSWORD='你设置的密码',
MASTER_LOG_FILE='mysql-bin.000001',
MASTER_LOG_POS=1718;
start slave;

执行以下
show slave status;

show slave status\G
可以看到

*************************** 1. row ***************************
               Slave_IO_State: Waiting for master to send event
                  Master_Host: 192.168.1.1
                  Master_User: root
                  Master_Port: 3306
                Connect_Retry: 60
              Master_Log_File: mysql-bin.000002
          Read_Master_Log_Pos: 2240619
               Relay_Log_File: eab8e7c281e7-relay-bin.000002
                Relay_Log_Pos: 97203
        Relay_Master_Log_File: mysql-bin.000002
             Slave_IO_Running: Yes
            Slave_SQL_Running: Yes
              Replicate_Do_DB: 
          Replicate_Ignore_DB: 
           Replicate_Do_Table: 
       Replicate_Ignore_Table: 
      Replicate_Wild_Do_Table: 
  Replicate_Wild_Ignore_Table: 
                   Last_Errno: 0
                   Last_Error: 
                 Skip_Counter: 0
          Exec_Master_Log_Pos: 2240619
              Relay_Log_Space: 97417
              Until_Condition: None
               Until_Log_File: 
                Until_Log_Pos: 0
           Master_SSL_Allowed: No
           Master_SSL_CA_File: 
           Master_SSL_CA_Path: 
              Master_SSL_Cert: 
            Master_SSL_Cipher: 
               Master_SSL_Key: 
        Seconds_Behind_Master: 0
Master_SSL_Verify_Server_Cert: No
                Last_IO_Errno: 0
                Last_IO_Error: 
               Last_SQL_Errno: 0
               Last_SQL_Error: 
  Replicate_Ignore_Server_Ids: 
             Master_Server_Id: 1
                  Master_UUID: a1ce2ca7-cc5b-11e8-a2e4-0242ac120002
             Master_Info_File: /var/lib/mysql/master.info
                    SQL_Delay: 0
          SQL_Remaining_Delay: NULL
      Slave_SQL_Running_State: Slave has read all relay log; waiting for more updates
           Master_Retry_Count: 86400
                  Master_Bind: 
      Last_IO_Error_Timestamp: 
     Last_SQL_Error_Timestamp: 
               Master_SSL_Crl: 
           Master_SSL_Crlpath: 
           Retrieved_Gtid_Set: 
            Executed_Gtid_Set: 
                Auto_Position: 0
         Replicate_Rewrite_DB: 
                 Channel_Name: 
           Master_TLS_Version: 
1 row in set (0.00 sec)

则设置成功。
下一步的读写分离后续附上

几个搭建时的注意点

  1. 主从应该只是拷贝了操作语句来执行,从表与主表的结构一定要完全一致,包括:数据库结构、表结构、等构建好主从节点后两边一致,再做修改,从数据库都会修改的。
  2. 主节点的 Position: 会因为数据库修改而不断变动,等待一个稳定时机,或者停止主节点的修改,再执行绑定。

读写分离

  1. 目前使用最简单的读写分离,select走slave 更新操作走master节点

bug解决

  • 生产环境几次后出现的问题
    1872 - Slave failed to initialize relay log info structure from the repository, Time: 0.005000s导致无法启动slave节点
    解决方案:reset slave all重置就好了

参考链接:https://blog.csdn.net/fighterandknight/article/details/80601968

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值