MySQL高可用-gtid

课前回顾

MySQL高可用MHA

#!/bin/bash

mysql_conn='mysql -uuser -ppwd -h127.0.0.1 -P2345'
mha_log_file="/etc/mha/app1/manager.log"
down_master_ip=`sed -nr 's#^mha.*failover (.*)\(.* (.*)\(.* succeeded$#\1#gp' $mha_log_file`
new_master_ip=`sed -nr 's#^mha.*failover (.*)\(.* (.*)\(.* succeeded$#\2#gp' $mha_log_file`
new_master_id=`$mysql_conn -e 'select * from backends'|grep "$new_master_ip"|awk '{print $1}'`



$mysql_conn -e "remove backend $new_master_id"
$mysql_conn -e "add slave $down_master_ip:3306"
$mysql_conn -e "save config"


mysql> select * from backends;
+-------------+------------------+-------+------+
| backend_ndx | address          | state | type |
+-------------+------------------+-------+------+
|           1 | 172.16.1.55:3306 | up    | rw   |
|           2 | 172.16.1.54:3306 | up    | ro   |
|           3 | 172.16.1.53:3306 | up    | ro   |
|           4 | 172.16.1.52:3306 | up    | ro   |
+-------------+------------------+-------+------+


[root@db04 ~]# mysql -uuser -ppwd -h127.0.0.1 -P2345 -e "select * from backends"
mysql: [Warning] Using a password on the command line interface can be insecure.
+-------------+------------------+-------+------+
| backend_ndx | address          | state | type |
+-------------+------------------+-------+------+
|           1 | 172.16.1.55:3306 | up    | rw   |
|           2 | 172.16.1.53:3306 | up    | ro   |
|           3 | 172.16.1.52:3306 | up    | ro   |
|           4 | 172.16.1.54:3306 | up    | ro   |
+-------------+------------------+-------+------+


From:
(172.16.1.54)(172.16.1.54:3306) (current master)
 +--172.16.1.51(172.16.1.51:3306)
 +--172.16.1.52(172.16.1.52:3306)
 +--172.16.1.53(172.16.1.53:3306)

To:
172.16.1.51(172.16.1.51:3306) (new master)
 +--172.16.1.52(172.16.1.52:3306)
 +--172.16.1.53(172.16.1.53:3306)

基于GTID的主从复制

1)什么是GTID?
GTID(Global Transaction ID)全局事务标识符:是一个唯一的标识符,它创建并与源服务器(主)上提交的每个事务相关联。
此标识符不仅对其发起的服务器是唯一的,而且在给定复制设置中的所有服务器上都是唯一的。 所有交易和所有GTID之间都有1对1的映射。
GTID实际上是由UUID+TID组成的。其中UUID是一个MySQL实例的唯一标识。TID代表了该实例上已经提交的事务数量,并且随着事务提交单调递增。
下面是一个GTID的具体形式:
3E11FA47-71CA-11E1-9E33-C80AA9429562:23

2)GTID新特性
(1).支持多线程复制:事实上是针对每个database开启相应的独立线程,即每个库有一个单独的(sql thread).

(2).支持启用GTID,在配置主从复制,传统的方式里,你需要找到binlog和POS点,然后change master to指向.
在mysql5.6里,无须再知道binlog和POS点,只需要知道master的IP/端口/账号密码即可,因为同步复制是自动的,MySQL通过内部机制GTID自动找点同步.

(3).基于Row复制只保存改变的列,大大节省Disk Space/Network resources和Memory usage.

(4).支持把Master 和Slave的相关信息记录在Table中
原来是记录在文件里,记录在表里,增强可用性

(5).支持延迟复制

## 传统主从复制
change master to
master_host=
master_user=
master_password=
master_log_file=
master_log_pos=





## GTID主从复制
change master to
master_host=
master_user=
master_password=
master_autoposition=1

部署gtid主从复制

## 需要修改配置文件,报错
mysql> change master to
master_host='172.16.1.53',
master_user='slave',
master_password='123',
master_auto_position=1;
ERROR 1777 (HY000): CHANGE MASTER TO MASTER_AUTO_POSITION = 1 cannot be executed because @@GLOBAL.GTID_MODE = OFF.



#####################  每一个数据库都要加该配置  ####################################

# 1.查看GTID相关配置
mysql> show variables like '%gtid%';
  ---------------------------------+-----------+
| Variable_name                    | Value     |
+----------------------------------+-----------+
| binlog_gtid_simple_recovery      | ON        |
| enforce_gtid_consistency         | OFF       |
| gtid_executed_compression_period | 1000      |
| gtid_mode                        | OFF       |
| gtid_next                        | AUTOMATIC |
| gtid_owned                       |           |
| gtid_purged                      |           |
| session_track_gtids              | OFF       |
+----------------------------------+-----------+


| gtid_mode                        | OFF       |  GTID模块关闭
| enforce_gtid_consistency         | OFF       |  GTID强一致性


# 2.修改配置文件(MySQL5.7)
[root@db04 ~]# vim /etc/my.cnf
[mysqld]
log-bin=mysql-bin
gtid_mode=ON
enforce_gtid_consistency

# 2.修改配置文件(MySQL5.6)
[root@db04 ~]# vim /etc/my.cnf
[mysqld]
log-bin=mysql-bin
gtid_mode=ON
enforce_gtid_consistency
log-slave-updates

# 3.重启数据库
[root@db04 ~]# /etc/init.d/mysqld restart


log-slave-updates:日志,从库,更新,   更新从库上的binlog
posted @ 2022-08-22 18:36  Gabydawei  阅读(150)  评论(0编辑  收藏  举报