• mysql5.7主主(双主)复制


    1. 在server1上操作

    vi /etc/my.cnf
    修改或添加下面这几行:
    server-id=1

    log-bin=mysql-bin # 启用二进制日志
    auto-increment-increment = 2 #每次增长2
    auto-increment-offset = 1 #设置自动增长的字段的偏移量
    两个可选参数(2选1):
    binlog-do-db=db1,db2 #需要同步的库
    binlog-ignore-db=db1,db2 #忽略不同步的库

    保存后重启

    /etc/init.d/mysqld restart

    运行mysql客户端
    mysql -uroot -p

    创建同步账号

    grant replication slave on . to 'repl2'@'192.168.1.2' identified by '1234';
    flush privileges;

    锁表,不让数据写入

    flush tables with read lock;
    show master status;

    mysql> show master status;
    +------------------+----------+--------------+------------------+-------------------+
    | File | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set |
    +------------------+----------+--------------+------------------+-------------------+
    | mysql-bin.000001 | 613 | | mysql | |
    +------------------+----------+--------------+------------------+-------------------+
    1 row in set (0.00 sec)

    记录下二进制日志文件名和位置

    备份数据库

    在mysql1上执行
    mysqldump -uroot -p db1 > back.sql;

    把备份好的数据拷贝到从服务器

    scp back.sql 192.168.1.2:~

    1. 在server2操作

    vi /etc/my.cnf
    修改或增加:
    server-id=2 #这个数值不能和主一样
    log-bin=mysql-bin # 启用二进制日志
    auto-increment-increment = 2 #每次增长2
    auto-increment-offset = 2 #设置自动增长的字段的偏移量

    可选参数(2选1,这两个参数设置成和主一样):
    replicate-do-db=db1,db2
    replicate-ignore-db=db1,db2

    保存后重启

    /etc/init.d/mysqld restart

    把在server1上备份的数据导入server2

    在server2上执行
    [root@server2 ~]# mysqldump -uroot -p db1 < back.sql

    运行mysql客户端

    mysql -uroot -p

    创建同步账号

    grant replication slave on . to 'repl1'@'192.168.1.1' identified by '1234';
    flush privileges;

    show master status;

    mysql> show master status;
    +------------------+----------+--------------+------------------+-------------------+
    | File | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set |
    +------------------+----------+--------------+------------------+-------------------+
    | mysql-bin.000001 | 613 | | mysql | |
    +------------------+----------+--------------+------------------+-------------------+
    1 row in set (0.00 sec)

    记录下二进制日志文件名和位置

    执行以下命令
    stop slave;
    change master to master_host='192.168.1.1',master_user='repl2',master_password='1234',master_log_file='mysql-bin.000001',master_log_pos=613;
    (master_log_file和master_log_pos填上刚才记录下的二进制日志文件名和位置)

    start slave;

    查看从服务器的状态:

    show slave statusG

    mysql> show slave statusG

      1. row *
        Slave_IO_State: Waiting for master to send event
        Master_Host: 192.168.1.1
        Master_User: repl2
        Master_Port: 3306
        Connect_Retry: 60
        Master_Log_File: mysql-bin.000001
        Read_Master_Log_Pos: 613
        Relay_Log_File: server2-relay-bin.000002
        Relay_Log_Pos: 320
        Relay_Master_Log_File: mysql-bin.000001
        Slave_IO_Running: Yes
        Slave_SQL_Running: Yes
    1. 回到server1上操作

    运行mysql客户端
    mysql -uroot -p

    取消server1上数据库的锁定

    mysql> unlock tables;

    执行以下命令
    stop slave;
    change master to master_host='192.168.1.2',master_user='repl1',master_password='1234',master_log_file='mysql-bin.000001',master_log_pos=613;
    (master_log_file和master_log_pos填上刚才记录下的二进制日志文件名和位置)

    start slave;

    查看从服务器的状态:

    show slave statusG

    mysql> show slave statusG

      1. row *
        Slave_IO_State: Waiting for master to send event
        Master_Host: 192.168.1.2
        Master_User: repl1
        Master_Port: 3306
        Connect_Retry: 60
        Master_Log_File: mysql-bin.000001
        Read_Master_Log_Pos: 613
        Relay_Log_File: server1-relay-bin.000002
        Relay_Log_Pos: 320
        Relay_Master_Log_File: mysql-bin.000001
        Slave_IO_Running: Yes
        Slave_SQL_Running: Yes
    1. 主主复制测试

    经测试,主主复制配置成功。

  • 相关阅读:
    接收xml请求流并解析字符串的例子
    通过style来控制隔行显示不同颜色 .
    显示有模式的有页面的弹出窗口 .
    WSDL生成服务端JAVA代码.bat
    web上下文监听器ServletContextListener 例子 .
    使用监听器:定时清除map缓存的key value .
    Timer和TimerTask类 例子 .
    常用的正则表达式
    算法找出数组中出现次数超过一半的数
    C++讨厌的临时变量什么时候产生
  • 原文地址:https://www.cnblogs.com/sanduzxcvbnm/p/11639906.html
Copyright © 2020-2023  润新知