MariaDB/Mysql的主主复制(主主互备)部署(Docker)

  • 拉取mariadb镜像

    docker pull mariadb

  • 创建好数据卷文件

    mkdir /home/lxj/hedisql/data/master

    mkdir /home/lxj/hedisql/data/slave1

  • 运行主容器

    docker run --name master -e MYSQL_ROOT_PASSWORD=123456 -v /home/lxj/heidisql/data/master:/var/lib/mysql -p 23306:3306 --restart always -d mariadb

  • 进入主容器执行(添加备份的账号)

    docker exec -it master /bin/bash

    mysql -uroot -p123456

    MariaDB [(none)]> grant replication slave on . to ‘backup’@’%’ identified by ‘123456’;

    MariaDB [(none)]> flush privileges;

  • 从容器中拷贝一份mariadb配置文件

    sudo docker cp master:/etc/mysql/my.cnf

    /home/lxj/hedisql/data/master_my.cnf

  • 修改master_my.cnf,在 [mysqld] 节点下添加

    [mysqld]
    server-id=1

    log_bin=master-bin

    binlog-ignore-db=mysql

    binlog-ignore-db=information_schema

    binlog-ignore-db=performance_schema

    binlog-ignore-db=test

    innodb_flush_log_at_trx_commit=1

    binlog_format=mixed

  • 复制master_my.cnf并覆盖master中的my.cnf

    docker cp /home/lxj/heidisql/data/master_my.cnf master:/etc/mysql/my.cnf

  • 重启master容器

    docker restart master

  • 运行主容器2

    docker run --name master2 -e MYSQL_ROOT_PASSWORD=123456 -v /home/lxj/heidisql/data/master2:/var/lib/mysql -p 23307:3306 --restart always -d mariadb

  • 进入主容器2执行(添加备份的账号)

    MariaDB [(none)]> grant replication slave on . to ‘backup’@’%’ identified by ‘123456’;

    MariaDB [(none)]> flush privileges;

  • 拷贝一份MySQL配置文件

    sudo docker cp master2:/etc/mysql/my.cnf /home/lxj/hedisql/data/master2_my.cnf

  • 修改master2_my.cnf,在 [mysqld] 节点下添加

    [mysqld]

    server-id=11

    log_bin=master2-bin

    binlog-ignore-db=mysql

    binlog-ignore-db=information_schema

    binlog-ignore-db=performance_schema

    binlog-ignore-db=test

    innodb_flush_log_at_trx_commit=1

    binlog_format=mixed

  • 复制master_my.cnf并覆盖master中的my.cnf

    docker cp /home/lxj/heidisql/data/master2_my.cnf master2:/etc/mysql/my.cnf

  • 重启master2容器

    docker restart master2

  • 进入master容器执行

    MariaDB [(none)]> SHOW MASTER status;

  • 查找master的log文件及其地址

    如File 为master-bin.000005
    Position为4719

  • 进入master2容器执行

    MariaDB [(none)]> SHOW MASTER status;

  • 查找master2的log文件及其地址

    如File 为master2-bin.000001
    Position为330

  • master容器执行

    MariaDB [(none)]> change master to
    MASTER_HOST=‘10.18.139.141’,
    master_user=‘root’,
    master_password=‘123456’,
    MASTER_PORT=23307,
    master_log_file=‘master2-bin.000001’,
    MASTER_LOG_POS=330;
    MariaDB [(none)]> start SLAVE;
    MariaDB [(none)]> show slave status;

  • master2容器执行

    MariaDB [(none)]> change master to
    MASTER_HOST=‘10.18.139.141’,
    master_user=‘root’,
    master_password=‘123456’,
    MASTER_PORT=23306,
    master_log_file=‘master-bin.000005’,
    MASTER_LOG_POS=4719;
    MariaDB [(none)]> start SLAVE;
    MariaDB [(none)]> show slave status;

  • 查看结果

    当结果中的Slave_IO_Running与Slave_SQL_Running都为Yes时表示配置成功。

  • 进行测试

    在master容器运行 MariaDB [(none)]> create database edslxj2;
    在master2容器能够同步。
    在master容器运行 MariaDB [(none)]> create database edslxj2222;
    在master2容器能够同步。

    部署成功

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值