gtid部署mysql主从复制

gtid原理:

1.当一个事务在主库端执行并提交时,产生GTID,一同记录到binlog日志中;

2.binlog传输到slave,并存储到slave的relaylog后,读取这个GTID的这个值设置gtid_next变量,即告诉Slave,下一个要执行的GTID值;

3.sql线程从relay log中获取GTID,然后对比slave端的binlog是否有该GTID;

4.如果有记录,说明该GTID的事务已经执行,slave会忽略;

5.如果没有记录,slave就会执行该GTID事务,并记录该GTID到自身的binlog,在读取执行事务前会先检查其他session持有该GTID,确保不被重复执行;

6.在解析过程中会判断是否有主键,如果没有就用二级索引,如果没有就用全部扫描

相比传统主从优点:

更简单的搭建主从复制;
比传统的复制更加安全;
GTID是连续的没有空洞的,保证数据的一致性,零丢失。
数据库 Gtid 主从复制如下, 如需实现多从, 将以下实验从案例再配置一次即可
操作系统MySQL版本IP角色
Centos7.4MysQL5.7192.168.230.16master
Centos7.4MysQL5.7192.168.230.15slave

主库配置

修改配置文件
[root@master ~]# vim /etc/my.cnf
log-bin=mysql-bin
server-id=1
gtid-mode = ON
enforce_gtid_consistency = 1
重启mysql
[root@master ~]# service mysqld restart
准备数据库
mysql> create database test;
mysql> use test;
mysql> create table jaja(id int,name varchar(20));
mysql> insert into jaja values (1,'aa'),(2,'bb');
mysql> select * from jaja;
+------+------+
| id   | name |
+------+------+
|    1 | aa   |
|    2 | bb   |
+------+------+
授权允许能够远程连接的主机
mysql> grant replication slave on *.* to 'rep'@'192.168.230.15' identified by 'rep123.com';
mysql> flush privileges;
导出当前数据库
[root@master ~]# mysqldump -uroot -pcs123.com --all-databases --single-transaction --master-data=1 --flush-logs > $(date +%F-%H-%M)-all.sql
[root@master ~]# ls
2019-02-28-19-21-all.sql
将备份文件发送给slave
[root@master ~]# scp 2019-02-28-19-21-all.sql root@192.168.230.15:/root/

从库配置

注意,如果之前在从库上面部署过传统主从,请先初始化数据库环境

检查是否能使用远程账户登录
[root@slave ~]# mysql -urep -p'rep123.com' -h 192.168.230.16
修改配置文件
[root@slave ~]# vim /etc/my.cnf
server-id = 2
gtid-mode = ON
enforce_gtid_consistency = 1
重启服务
[root@slave ~]# service mysqld restart
导入数据
[root@slave ~]# mysql -uroot -pcs123.com -e "source 2019-02-28-19-21-all.sql"
mysql> use test;
mysql> select * from jaja;
+------+------+
| id   | name |
+------+------+
|    1 | aa   |
|    2 | bb   |
+------+------+

指向master,gtid自动协商同步
mysql> change master to master_host='192.168.230.16',master_user='rep',master_password='rep123.com',master_auto_position=1;
启动slave角色
mysql> start slave;
查看角色是否同步
mysql> show slave status\G
*************************** 1. row ***************************
               Slave_IO_State: Waiting for master to send event
                  Master_Host: 192.168.230.16
                  Master_User: rep
                  Master_Port: 3306
                Connect_Retry: 60
              Master_Log_File: mysql-bin.000008
          Read_Master_Log_Pos: 194
               Relay_Log_File: slave-relay-bin.000002
                Relay_Log_Pos: 367
        Relay_Master_Log_File: mysql-bin.000008
             Slave_IO_Running: Yes
            Slave_SQL_Running: Yes
              Replicate_Do_DB: 
          Replicate_Ignore_DB: 
           Replicate_Do_Table: 
       Replicate_Ignore_Table: 
      Replicate_Wild_Do_Table: 
  Replicate_Wild_Ignore_Table: 
                   Last_Errno: 0
                   Last_Error: 
                 Skip_Counter: 0
          Exec_Master_Log_Pos: 194
              Relay_Log_Space: 574
              Until_Condition: None
               Until_Log_File: 
                Until_Log_Pos: 0
           Master_SSL_Allowed: No
           Master_SSL_CA_File: 
           Master_SSL_CA_Path: 
              Master_SSL_Cert: 
            Master_SSL_Cipher: 
               Master_SSL_Key: 
        Seconds_Behind_Master: 0
Master_SSL_Verify_Server_Cert: No
                Last_IO_Errno: 0
                Last_IO_Error: 
               Last_SQL_Errno: 0
               Last_SQL_Error: 
  Replicate_Ignore_Server_Ids: 
             Master_Server_Id: 1
                  Master_UUID: b27045f6-36bb-11e9-b404-000c29c87115
             Master_Info_File: /opt/data/master.info
                    SQL_Delay: 0
          SQL_Remaining_Delay: NULL
      Slave_SQL_Running_State: Slave has read all relay log; waiting for more updates
           Master_Retry_Count: 86400
                  Master_Bind: 
      Last_IO_Error_Timestamp: 
     Last_SQL_Error_Timestamp: 
               Master_SSL_Crl: 
           Master_SSL_Crlpath: 
           Retrieved_Gtid_Set: 
            Executed_Gtid_Set: b27045f6-36bb-11e9-b404-000c29c87115:1-7539
                Auto_Position: 1
         Replicate_Rewrite_DB: 
                 Channel_Name: 
           Master_TLS_Version: 

主要看这个位置,两个yes就对了

Slave_IO_Running: Yes
Slave_SQL_Running: Yes

验证

主库添加数据
mysql> use test;
mysql> insert into jaja values (3,'aaa'),(4,'bbb');
mysql> select * from jaja;
+------+------+
| id   | name |
+------+------+
|    1 | aa   |
|    2 | bb   |
|    3 | aaa  |
|    4 | bbb  |
+------+------+

从库查看
mysql> select * from jaja;
+------+------+
| id   | name |
+------+------+
|    1 | aa   |
|    2 | bb   |
|    3 | aaa  |
|    4 | bbb  |
+------+------+
4 rows in set (0.01 sec)
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值