zoukankan      html  css  js  c++  java
  • mysql双主双从技术

    一、准备环境

    [root@localhost ~]# vim /etc/hosts

    192.168.40.154 master1     192.168.40.129 master2                                                                                         
    192.168.40.138 slave1       192.168.40.128 slave2
    注:四台虚拟机配置都相同,且防火墙关闭

    二、双主设置

    master1(其数据库中提前准备点库)

    1、[root@localhost ~]# vim /etc/my.cnf

    log_bin
    server-id=1
    gtid_mode=ON
    enforce_gtid_consistency=1

    2、[root@localhost ~]# systemctl restart mysqld

    3、授权:mysql>grant replication slave,replication client on *.* to 'rep'@'192.168.40.%' identified by 'Zjz@5740';

    4、mysql>  flush privileges;(刷新)

    5、备份:mysqldump -p'Zjz@5740' --all-databases --single-transaction --master-data=2 --flush-logs > `date +%F`-mysql-all.sql

    6、#scp -r 2017-1-1-mysql-all.sql master2:/tmp

    7、观察二进制日志分割点

    CHANGE MASTER TO MASTER_LOG_FILE='localhost-bin.000002', MASTER_LOG_POS=154;

    master2

    1、[root@localhost ~]#vim /etc/my.cnf( 启动二进制日志,服务器ID,GTID)

    log_bin
    server-id=2
    gtid_mode=ON
    enforce_gtid_consistency=1

    2、[root@localhost ~]# systemctl restart mysqld

     还恢复手动同步数据

    mysql>set sql_log_bin=0;

    mysql>source /tmp/2017-1-1-mysql-full.sql

    mysql>select * from master1db.master1tab;

    3、设置主服务器

    mysql> change master to
    master_host='master1',
    master_user='rep',
    master_password='Zjz@5740',
    master_auto_position=1;

    start slave;

    show slave statusG;

    master1认master2为主

    master2授权:mysql>grant replication slave,replication client on *.* to 'rep'@'192.168.40.%' identified by 'Zjz@5740';

    flush privileges;(刷新)

    master1:设置主服务器

    mysql> change master to
    master_host='master2',
    master_user='rep',
    master_password='Zjz@5740',
    master_auto_position=1;

    start slave;

    show slave statusG;

    mysqldump -p'Zjz@5740' --all-databases --single-transaction --master-data=2  --flush-logs > `date +%F`-mysql-all.sql

    scp -r 2017-8-9-mysql-all.sql slave1:/tmp

    scp -r 2017-8-9-mysql-all.sql slave2:/tmp

    三、双从设置

    slave1    # mysql -p'Zjz@5740' < /tmp/2017-8-9-mysql-all.sql

    slave2     #mysql -p'Zjz@5740' < /tmp/2017-8-9-mysql-all.sql

    启动从服务器ID,gtid

    slave1                                                              slave2

    #vim /etc/my.cnf                                              # vim /etc/my.cnf
    server-id=3                                                       server-id=4
    gtid_mode=ON                                                 gtid_mode=ON
    enforce_gtid_consistency=1                            enforce_gtid_consistency=1
    master-info-repository=TABLE                        master-info-repository=TABLE
    relay-log-info-repository=TABLE                     relay-log-info-repository=TABLE

    #systemctl restart mysqld                                  #systemctl restart mysqld  

    设置主服务器

    slave1(认两个主服务器)

    mysql> change master to                                             mysql> change master to
    master_host='master1',                                                master_host='master2',
    master_user='rep',                                                        master_user='rep',
    master_password='Zjz@5740',                                    master_password='Zjz@5740',
    master_auto_position=1 for channel 'master1';            master_auto_position=1 for channel 'master1';

    mysql>start slave;

    mysql>show slave statusG;(双yes成功)

    slave2同上操作

    四、总结

    1、出现connettion,重启电脑试试

    2、有些步骤先停止slave再操作

    3、[root@slave1 ~]#  mysql -p'Zjz@5740' < /tmp/2019-08-29-mysql-all.sqlmysql:

    [Warning] Using a password on the command line interface can be insecure.

    ERROR 1840 (HY000) at line 24: @@GLOBAL.GTID_PURGED can only be set when @@GLOBAL.GTID_EXECUTED is empty.

    解决办法:mysql> reset master;(进入slave1)    

    Query OK, 0 rows affected (0.10 sec)    

    mysql> q 

    Bye

    4、Slave_IO_Running: No

      Slave_SQL_Running: Yes

    报错:Last_IO_Error: Got fatal error 1236 from master when reading data from binary log: 'The slave is connecting using CHANGE MASTER TO MASTER_AUTO_POSITION = 1, but the master has purged binary logs containing GTIDs that the slave requires.'

    解决:这个应该是由于你在主库上执行过purge binary logs,然后当从库change master的时候,却要执行那些事务。
    你可以在主库上先查找哪些gtid被purge了。
    show global variables like 'gtid_purged';
    然后拿着这个value,去从库上依次
    stop slave;
    set global gtid_purged = 'xxx'; # xxx是你主库上查到的value。
    start slave;
    这样能跳过执行被主库已经purge的事务了。

     https://www.iirwt.com/2020/08/1178.html  容器版主从复制

  • 相关阅读:
    shell脚本批量检查网站是否正常
    测试运维-linux中常用的操作命令以及工作思路
    软件自动化测试工程师面试题集锦(2)
    UI自动化测试常用操作函数(3)
    软件自动化测试工程师面试题集锦(1)
    UI自动化测试常用操作函数(2)
    UI自动化测试常用操作函数(1)
    滑屏找元素
    代码报错解析
    心情20.4.6
  • 原文地址:https://www.cnblogs.com/zjz20/p/11427650.html
Copyright © 2011-2022 走看看