zoukankan      html  css  js  c++  java
  • docker搭建主从复制mysql

    步骤:

    ---启动两个master和slave容器

    ---修改配置master和slave的my.cnf

    ---master添加从服务器要要复制的用户并授权

    ---slave添加主服务器相关信息,并开启slave

    1.启动两个容器

    docker run -d --name mysql_master -p 3306:3306 -e MYSQL_ROOT_PASSWORD=root mysql
    
    docker run -d --name mysql_slave -p 3307:3306 -e MYSQL_ROOT_PASSWORD=root mysql
    

     2.修改两个配置文件

    master

    # 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
    log-bin=mysql-bin
    server_id=1
    # Custom config should go here
    !includedir /etc/mysql/conf.d/
    

     slave

    # 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=233
    relay_log=mysql-relay-bin 
    # Custom config should go here
    !includedir /etc/mysql/conf.d/
    

     3.添加授权用户,记录master状态

    create user slave'@'%' identified by '123456';
    
    grant replication slave on *.* to 'slave'@'%';
    
    mysql> show master status;
    +---------------+----------+--------------+------------------+-------------------+
    | File          | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set |
    +---------------+----------+--------------+------------------+-------------------+
    | binlog.000002 |     1249 |              |                  |                   |
    +---------------+----------+--------------+------------------+-------------------+
    1 row in set (0.00 sec)
    

    4.添加主服务器信息,开启slave模式

    change master to master_host='172.17.0.2', master_user='slave', master_password='123456', master_port=3306, master_log_file='binlog.000002', master_log_pos=1249, master_connect_retry=30;  
    
    start slave;
    

    主服务器创建数据库验证

    报错

    1.可能会有下面的报错,解决方法:https://blog.csdn.net/sunbocong/article/details/81634296

    The slave I/O thread stops because master and slave have equal MySQL server UUIDs

    2.以前有过slave复制会开启失败,解决方法如下:

     
    stop slave;
     
    reset slave;
     
    然后再重复上面的操作就可以了
     
    参考文章:

    https://juejin.im/post/5b0f91ab518825153749b1db

    https://www.jianshu.com/p/ab20e835a73f

  • 相关阅读:
    C++ Primer Plus(三)
    C++ Primer Plus(二)
    C++ Primer Plus(一)
    C Primer Plus(三)
    C++ 函数重载,函数模板和函数模板重载,选择哪一个?
    Spring IoC 公共注解详解
    Spring IoC @Autowired 注解详解
    Spring IoC 容器的扩展
    Spring IoC bean 的初始化
    Spring IoC 属性赋值阶段
  • 原文地址:https://www.cnblogs.com/soymilk2019/p/13409170.html
Copyright © 2011-2022 走看看