• MySQL主从GTID复制


    一、GTID简介

    GTID是对于一个已提交事务的唯一编号,并且是一个全局(主从复制)唯一的编号!

    GTID官方定义如下:

    GTID = source_id :transaction_id
    7E11FA47-31CA-19E1-9E56-C43AA21293967:29
    

    什么是sever_uuid,和Server-id 区别?
    核心特性: 全局唯一,具备幂等性!

    二、GTID重要参数

    gtid-mode=on #启用gtid类型,否则就是普通的复制架构 
    enforce-gtid-consistency=true #强制GTID的一致性 
    log-slave-updates=1 #slave更新是否记入日志 
    

    三、实验环境

    OS IP hostname service
    centos 7.5 192.168.1.1 db01 mysql 5.7.29
    centos 7.5 192.168.1.2 db02 mysql 5.7.29
    centos 7.5 192.168.1.3 db03 mysql 5.7.29

    搭建mysql过程略!

    四、清理环境

    [root@db01 ~]# systemctl stop mysqld
    [root@db01 ~]# rm -rf /usr/local/mysql/data/*
    #删除原始数据库下的目录,并且保证数据库是停止状态
    

    五、准备配置文件

    master(db01)
    [root@db01 ~]# cat > /etc/my.cnf << EOF
    [mysqld]
    basedir=/usr/local/mysql
    datadir=/usr/local/mysql/data
    socket=/usr/local/mysql/mysql.sock
    server_id=1
    port=3306
    secure-file-priv=/tmp
    autocommit=0
    log_bin=/usr/local/mysql/data/mysql-bin
    binlog_format=row
    gtid-mode=on
    enforce-gtid-consistency=true
    log-slave-updates=1
    [mysql]
    prompt=db01[\d]>
    EOF
    
    slave1(db02)
    [root@db02 ~]# cat > /etc/my.cnf << EOF
    [mysqld]
    basedir=/usr/local/mysql
    datadir=/usr/local/mysql/data
    socket=/usr/local/mysql/mysql.sock
    server_id=2
    port=3306
    secure-file-priv=/tmp
    autocommit=0
    log_bin=/usr/local/mysql/data/mysql-bin
    binlog_format=row
    gtid-mode=on
    enforce-gtid-consistency=true
    log-slave-updates=1
    [mysql]
    prompt=db02[\d]>
    EOF
    
    slave2(db03)
    [root@db03 ~]# cat > /etc/my.cnf << EOF
    [mysqld]
    basedir=/usr/local/mysql
    datadir=/usr/local/mysql/data
    socket=/usr/local/mysql/mysql.sock
    server_id=3
    port=3306
    secure-file-priv=/tmp
    autocommit=0
    log_bin=/usr/local/mysql/data/mysql-bin
    binlog_format=row
    gtid-mode=on
    enforce-gtid-consistency=true
    log-slave-updates=1
    [mysql]
    prompt=db03[\d]>
    EOF
    

    六、初始化数据、启动数据库

    [root@db01 ~]# mysqld --initialize-insecure --user=mysql --basedir=/usr/local/mysql  --datadir=/usr/local/mysql/data
    [root@db01 ~]# systemctl start mysqld
    #三台数据都需进行操作
    

    七、构建主从

    db01[(none)]>grant replication slave on *.* to repl@'192.168.1.%' identified by '123';
    #主库创建主从复制专用用户
    db02[(none)]>change master to
      master_host='192.168.1.1',
      master_user='repl',
      master_password='123',
      master_auto_position=1;
    db02[(none)]> start slave;  
    db03[(none)]>change master to
      master_host='192.168.1.1',
      master_user='repl',
      master_password='123',
      master_auto_position=1;
    db03[(none)]> start slave;    
    

    八、验证主从复制

    db01[(none)]>show master status;
    +------------------+----------+--------------+------------------+----------------------------------------+
    | File             | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set                      |
    +------------------+----------+--------------+------------------+----------------------------------------+
    | mysql-bin.000002 |      447 |              |                  | 8365c1b0-813e-11ea-a031-000c29667213:1 |
    +------------------+----------+--------------+------------------+----------------------------------------+
    #注意GTID号
    db02[(none)]>show slave status G
               Retrieved_Gtid_Set: 8365c1b0-813e-11ea-a031-000c29667213:1   #需要恢复的GTID号
                Executed_Gtid_Set: 8365c1b0-813e-11ea-a031-000c29667213:1   #已经执行的GTID号
    db03[(none)]>show slave status G
               Retrieved_Gtid_Set: 8365c1b0-813e-11ea-a031-000c29667213:1
                Executed_Gtid_Set: 8365c1b0-813e-11ea-a031-000c29667213:1
    #从库上的GTID号与主库上的GTID号保持一致,表示同步没有问题			
    

    九、GTID 复制和普通复制的区别

    CHANGE MASTER TO
    MASTER_HOST='192.168.1.1',
    MASTER_USER='repl',
    MASTER_PASSWORD='123',
    MASTER_PORT=3307,
    MASTER_LOG_FILE='mysql-bin.000001',
    MASTER_LOG_POS=444,
    MASTER_CONNECT_RETRY=10;
    
    change master to 
    master_host='192.168.1.1',
    master_user='repl',
    master_password='123' ,
    MASTER_AUTO_POSITION=1;
    
    0)在主从复制环境中,主库发生过的事务,在全局都是由唯一GTID记录的,更方便Failover
    1)额外功能参数(3个)
    2)change master to 的时候不再需要binlog 文件名和position号,MASTER_AUTO_POSITION=1;
    3)在复制过程中,从库不再依赖master.info文件,而是直接读取最后一个relaylog的 GTID号
    4) mysqldump备份时,默认会将备份中包含的事务操作,以以下方式
        SET @@GLOBAL.GTID_PURGED='8c49d7ec-7e78-11e8-9638-000c29ca725d:1';
        告诉从库,我的备份中已经有以上事务,你就不用运行了,直接从下一个GTID开始请求binlog就行。
    

    注意:gtid的主从复制,第一次开启时,读取relaylog的最后gtid+gtid_purge参数,确认复制起点!

    *************** 当你发现自己的才华撑不起野心时,就请安静下来学习吧!***************
  • 相关阅读:
    追求一个人怎么这么难
    基于PHPstream扩展手动实现一个redis客户端
    常见final修饰类
    位运算
    HTTP Status 404
    Hibernate Junit 运行报错:org.hibernate.service.spi.ServiceException: Unable to create requested service [org.hibernate.engine.jdbc.env.spi.JdbcEnvironment
    根据银行卡号码获取银行卡归属行以及logo图标
    进程间通信IPC(InterProcess Communication)
    脏读、幻读、不可重复读和可重复读
    数据库锁知识总结
  • 原文地址:https://www.cnblogs.com/lvzhenjiang/p/14197265.html
Copyright © 2020-2023  润新知