美文网首页
MySQL双主双从配置-Docker

MySQL双主双从配置-Docker

作者: 有内涵的Google | 来源:发表于2020-11-29 21:21 被阅读0次

    环境搭建

    由于我的电脑配置不是很高, 这里就使用docker搭建环境. 如果你的电脑配置够好也是可以使用4台虚拟机的.

    宿主机: Win10

    VMware: VMware® Workstation 15 Pro

    虚拟机系统: Centos7

    Docker: 19.03.13

    MySQL: 5.7

    初始化

    • 1.在/docker/mysql目录下创建4个文件夹分别是: master1, master2, slave1, slave2分别在每个目录下建立data, conf, logs用于数据持久化, 目录结构如下
    mysql/
    ├── master1
    │   ├── conf
    │   ├── data
    │   │   ├── ibdata1
    │   │   └── ib_logfile101
    │   └── logs
    ├── master2
    │   ├── conf
    │   ├── data
    │   └── logs
    ├── slave1
    │   ├── conf
    │   ├── data
    │   └── logs
    └── slave2
        ├── conf
        ├── data
        └── logs
        └── logs
    
    • 2.初始化容器

    master1

    docker run -p 4306:3306 --name master01 \
    -v /docker/mysql/master1/conf:/etc/mysql/conf.d \
    -v /docker/mysql/master1/logs:/var/log/mysql \
    -v /docker/mysql/master1/data:/var/lib/mysql \
    -e MYSQL_ROOT_PASSWORD=MASTERROOT@12345678  -itd mysql:5.7
    

    master2

    docker run -p 4307:3306 --name master02 \
    -v /docker/mysql/master2/conf:/etc/mysql/conf.d \
    -v /docker/mysql/master2/logs:/var/log/mysql \
    -v /docker/mysql/master2/data:/var/lib/mysql \
    -e MYSQL_ROOT_PASSWORD=MASTERROOT@12345678  -itd mysql:5.7
    

    salve1

    docker run -p 5306:3306 --name slave01 \
    -v /docker/mysql/slave1/conf:/etc/mysql/conf.d \
    -v /docker/mysql/slave1/logs:/var/log/mysql \
    -v /docker/mysql/slave1/data:/var/lib/mysql \
    -e MYSQL_ROOT_PASSWORD=SLAVEROOT@12345678  -itd mysql:5.7
    

    salve2

    docker run -p 5307:3306 --name slave02 \
    -v /docker/mysql/slave2/conf:/etc/mysql/conf.d \
    -v /docker/mysql/slave2/logs:/var/log/mysql \
    -v /docker/mysql/slave2/data:/var/lib/mysql \
    -e MYSQL_ROOT_PASSWORD=SLAVEROOT@12345678  -itd mysql:5.7
    

    验证

    docker ps # 检查是否将mysql容器创建成功
    

    完成后的环境

    查看容器ip可以使用下面的命令

    docker exec -it master01 sh   # 进入到容器终端
    cat /etc/hosts                # 查看容器ip
    # 也是可以使用下面的命令的
    docker inspect master01 | grep IPAddress # master01 为容器名
    
    编号 角色 IP地址
    1 Master01 172.17.0.5
    2 Master02 172.17.0.4
    3 Slave01 172.17.0.3
    4 Slave02 172.17.0.2

    配置

    双主机配置

    • 1.master01
    [root@localhost ~]# touch /docker/mysql/master1/conf/my.cnf
    

    配置内容如下:

    [mysqld]
    
    # master01主服务器01唯一ID
    server-id=4306
    
    # 启用二进制日志
    log-bin=mysql-bin
    
    #从库的中继日志,主库日志写到中继日志,中继日志再重做到从库
    # relay-log=myslql-relay-bin
    
    # binlog保留时间7天
    expire_logs_days=7
    
    # binlog 文件的大小
    max_binlog_size=1G
    
    #设置logbin格式。取值:STATEMENT (默认),ROW,MIXED
    binlog_format=ROW
    
    # 设置不要赋值的数据
    binlog-ignore-db=mysql
    binlog-ignore-db=information_schema
    binlog-ignore-db=performance_schema
    binlog-ignore-db=sys
    
    # 设置需要复制的数据(可选)
    # 如果配置了此项,就是只复制那个数据库, 如果不指定就是所有
    # binlog-do-db=需要复制的主数据库1
    
    # 设置login格式
    binlog_format=STATEMENT
    
    # 在作为从数据库的时候,有写入操作也要更新二进制日志文件
    log-slave-updates
    
    # 该从库是否写入二进制日志。如果需要成为多主则可启用。只读可以不需要
    log-slave-updates=1
    
    #表示自增长字段每次递增的量,指自增字段的起始值,其默认值是1,取值范围是1 .. 65535
    auto-increment-increment=2
    
    # 表示自增长字段从哪个数开始,指字段一次递增多少,他的取值范围是1 .. 65535
    auto-increment-offset=1
    
    • 2.master02
    [root@localhost ~]# touch /docker/mysql/master2/conf/my.cnf
    

    配置内容如下:

    [mysqld]
    
    # master01主服务器01唯一ID
    server-id=4307
    
    # 启用二进制日志
    log-bin=mysql-bin
    
    #从库的中继日志,主库日志写到中继日志,中继日志再重做到从库
    # relay-log=myslql-relay-bin
    
    # binlog保留时间7天
    expire_logs_days=7
    
    # binlog 文件的大小
    max_binlog_size=1G
    
    #设置logbin格式。取值:STATEMENT (默认),ROW,MIXED
    binlog_format=ROW
    
    # 设置不要赋值的数据
    binlog-ignore-db=mysql
    binlog-ignore-db=information_schema
    binlog-ignore-db=performance_schema
    binlog-ignore-db=sys
    
    # 设置需要复制的数据(可选)
    # 如果配置了此项,就是只复制那个数据库, 如果不指定就是所有
    # binlog-do-db=需要复制的主数据库1
    
    # 设置login格式
    binlog_format=STATEMENT
    
    # 在作为从数据库的时候,有写入操作也要更新二进制日志文件
    log-slave-updates
    
    # 该从库是否写入二进制日志。如果需要成为多主则可启用。只读可以不需要
    log-slave-updates=1
    
    #表示自增长字段每次递增的量,指自增字段的起始值,其默认值是1,取值范围是1 .. 65535
    auto-increment-increment=2
    
    # 表示自增长字段从哪个数开始,指字段一次递增多少,他的取值范围是1 .. 65535
    auto-increment-offset=1
    

    双从机配置

    • slave01
    touch /docker/mysql/slave1/conf/my.cnf
    

    配置内容如下:

    [mysqld]
    # 从服务唯一ID
    server-id=5306
    # 启用中继日志
    relay-log=mysql-relay
    
    • slave02
    touch /docker/mysql/slave2/conf/my.cnf
    

    配置内容如下:

    [mysqld]
    # 从服务唯一ID
    server-id=5307
    # 启用中继日志
    relay-log=mysql-relay
    

    重启容器

    docker restart $(docker ps -q)
    

    双主机账户配置

    • master01
    sql> grant replication slave on *.* TO 'master01slave'@'%' identified by 'ABC123456789@';
    sql>flush privileges;
    

    查看状态

    mysql> show master status\G;
    *************************** 1. row ***************************
                 File: mysql-bin.000001
             Position: 154
         Binlog_Do_DB:
     Binlog_Ignore_DB: mysql,information_schema,performance_schema,sys
    Executed_Gtid_Set:
    1 row in set (0.00 sec)
    
    • master02
    sql> grant replication slave on *.* TO 'master02slave'@'%' identified by 'ABC123456789@';
    sql>flush privileges;
    

    查看状态

    mysql> show master status\G;
    *************************** 1. row ***************************
                 File: mysql-bin.000001
             Position: 608
         Binlog_Do_DB:
     Binlog_Ignore_DB: mysql,information_schema,performance_schema,sys
    Executed_Gtid_Set:
    1 row in set (0.00 sec)
    

    从机复制主机

    slave01复制master01, slave02复制master02

    复制命令格式

    #复制主机的命令 
    CHANGE MASTER TO MASTER_HOST='主机的IP地址',
    MASTER_USER='username',
    MASTER_PASSWORD='passworld',
    MASTER_LOG_FILE='mysql-bin.具体数字',
    MASTER_LOG_POS=具体值;
    
    • slave01的复制命令
    mysql> change master to master_host='172.17.0.5',
        -> master_user='master01slave',
        -> master_password='ABC123456789@',
        -> master_log_file='mysql-bin.000001',
        -> master_log_pos=154;
    

    启用同步进程

    mysql> start slave;
    

    查看从库状态

    mysql> show slave status\G;
    *************************** 1. row ***************************
                   Slave_IO_State: Waiting for master to send event
                      Master_Host: 172.17.0.5
                      Master_User: master01slave
                      Master_Port: 3306
                    Connect_Retry: 60
                  Master_Log_File: mysql-bin.000001
              Read_Master_Log_Pos: 154
                   Relay_Log_File: mysql-relay.000002
                    Relay_Log_Pos: 320
            Relay_Master_Log_File: mysql-bin.000001
                 Slave_IO_Running: Yes
                Slave_SQL_Running: Yes
    

    当我们看到Slave_IO_Running 和Slave_SQL_Running 都为YES时, 说明我们就配置成功了.

    • slave02
    mysql> change master to master_host='172.17.0.4',
        ->  master_user='master02slave',
        ->  master_password='ABC123456789@',
        -> master_log_file='mysql-bin.000001',
        -> master_log_pos=608;
    

    开启同步进程

    mysql>start slave;
    

    查看从库状态

    mysql> show slave status\G;
    *************************** 1. row ***************************
                   Slave_IO_State: Waiting for master to send event
                      Master_Host: 172.17.0.4
                      Master_User: master02slave
                      Master_Port: 3306
                    Connect_Retry: 60
                  Master_Log_File: mysql-bin.000001
              Read_Master_Log_Pos: 608
                   Relay_Log_File: mysql-relay.000002
                    Relay_Log_Pos: 320
            Relay_Master_Log_File: mysql-bin.000001
                 Slave_IO_Running: Yes
                Slave_SQL_Running: Yes
    

    两主机互相复制

    master01复制master02, master2复制master01

    • master01
    mysql> change master to master_host='172.17.0.4',
        -> master_user='master02slave',
        -> master_password='ABC123456789@',
        -> master_log_file='mysql-bin.000001',
        -> master_log_pos=608;
    

    解析

    这里的IP地址为master02的ip,用户名也是master02提供的.

    开启同步进程

    start slave;
    

    查看状态

    mysql> show slave status\G;
    *************************** 1. row ***************************
                   Slave_IO_State: Waiting for master to send event
                      Master_Host: 172.17.0.4
                      Master_User: master02slave
                      Master_Port: 3306
                    Connect_Retry: 60
                  Master_Log_File: mysql-bin.000001
              Read_Master_Log_Pos: 608
                   Relay_Log_File: 65b73f7c9183-relay-bin.000002
                    Relay_Log_Pos: 320
            Relay_Master_Log_File: mysql-bin.000001
                 Slave_IO_Running: Yes
                Slave_SQL_Running: Yes
    
    • master02
    mysql> change master to master_host='172.17.0.5',
        -> master_user='master01slave',
        -> master_password='ABC123456789@',
        -> master_log_file='mysql-bin.000001',
        -> master_log_pos=154;
    

    启用同步进程

    mysql> start slave;
    

    查看状态

    mysql> show slave status\G;
    *************************** 1. row ***************************
                   Slave_IO_State: Waiting for master to send event
                      Master_Host: 172.17.0.5
                      Master_User: master01slave
                      Master_Port: 3306
                    Connect_Retry: 60
                  Master_Log_File: mysql-bin.000001
              Read_Master_Log_Pos: 154
                   Relay_Log_File: 0d5426412439-relay-bin.000002
                    Relay_Log_Pos: 320
            Relay_Master_Log_File: mysql-bin.000001
                 Slave_IO_Running: Yes
                Slave_SQL_Running: Yes
    

    测试

    在主库master01上创建一个数据库test_db

    mysql> CREATE DATABASE test_db;
    

    test_db上创建一个数据表user

    mysql> use test_db;
    mysql> CREATE TABLE IF NOT EXISTS `user`(
        -> `id` INT UNSIGNED AUTO_INCREMENT,
        -> `name` VARCHAR(30) NOT NULL,
        -> `age` INT(3) NOT NULL,
        -> PRIMARY KEY ( `id` )
        -> )ENGINE=InnoDB DEFAULT CHARSET=utf8;
    

    在数据表中插入数据

    mysql> INSERT INTO user (name, age) VALUES ("zhaoming", 18);
    
    mysql> SELECT * FROM user \G;
    *************************** 1. row ***************************
      id: 1
    name: zhaoming
     age: 18
    1 row in set (0.32 sec)
    
    MySQL双主双从配置成功

    相关文章

      网友评论

          本文标题:MySQL双主双从配置-Docker

          本文链接:https://www.haomeiwen.com/subject/fbflwktx.html