一 GITD介绍

1.1 gtid的含义

Global Transaction Identifier,全局事务标识

阿里云的rds目前已经使用gtid

基于gtid的主从复制原理

每个mysql数据库上都有一个唯一uuid

每个事务生成一个id

gtid由上面两者组合: uuid+事务id

1.2 优势

相对使用binlog+位置的方法来说

gtid让配置主从更加方便

从提升为主时比较方便

二 配置

2.1 主库的配置

[mysqld]
bind-address=0.0.0.0
port=
datadir=/data/mysql
socket=/data/mysql/mysql.sock
user=mysql
skip-name-resolve
slow_query_log=on
long_query_time=
slow_query_log_file=/data/mysql/mysql-slow.log
innodb-file-per-table=
innodb_flush_log_at_trx_commit =
log_warnings =
connect_timeout =
net_read_timeout =
performance_schema_max_table_instances =
server-id =
gtid_mode=on
enforce_gtid_consistency=on
log-slave-updates=
log-bin=master-bin
log-bin-index = master-bin.index
relay-log = relay-log
relay-log-index = relay-log.index
binlog_format=row [mysqld_safe]
log-error=/data/mysql/mysqld.log
pid-file=/data/mysql/mysqld.pid

[root@master mysql]# vim /etc/my.cnf

[root@master mysql]# systemctl stop mysqld

[root@master mysql]# rm -rf /data/mysql/*

[root@master mysql]# mysqld --initialize --user=mysql

查看uuid

[root@master mysql]# pwd
/data/mysql
[root@master mysql]# ll

查看数据库的uuid

在数据目录的auto.cnf

[root@master mysql]# cat auto.cnf

在mysql里面使用命令查看show global variables like 'server_uuid'

[root@master mysql]# systemctl start mysqld
[root@master mysql]# mysql -u root -p
Enter password: rrV.sL)!d9zE
mysql> show global variables like 'server_uuid'; #查看uuid
+---------------+--------------------------------------+
| Variable_name | Value |
+---------------+--------------------------------------+
| server_uuid | ad5ee77e-9f2e-11e9-a76d-000c2991dd19 |
+---------------+--------------------------------------+
row in set (0.01 sec)

2.2 从库配置

[mysqld]
bind-address=0.0.0.0
port=
datadir=/data/mysql
socket=/data/mysql/mysql.sock
user=mysql
skip-name-resolve
slow_query_log=on
long_query_time=
slow_query_log_file=/data/mysql/mysql-slow.log
innodb-file-per-table=
innodb_flush_log_at_trx_commit =
log_warnings =
connect_timeout =
net_read_timeout =
performance_schema_max_table_instances =
server-id =
gtid_mode=on
enforce_gtid_consistency=on
log-slave-updates=
log-bin=master-bin
log-bin-index = master-bin.index
relay-log = relay-log
relay-log-index = relay-log.index
binlog_format=row [mysqld_safe]
log-error=/data/mysql/mysqld.log
pid-file=/data/mysql/mysqld.pid

开启数据库,查看uuid

[root@slave ~]# systemctl stop mysqld
[root@slave ~]# rm -rf /data/mysql/*
[root@slave ~]# mysqld --initialize --user=mysql
2019-07-05T14:24:23.975473Z 0 [Warning] No existing UUID has been found, so we assume that this is the first time that this server has been started. Generating a new UUID: 96435cd6-9f30-11e9-a575-000c2963fd11.
2019-07-05T14:24:23.977416Z 0 [Warning] Gtid table is not ready to be used. Table 'mysql.gtid_executed' cannot be opened.
2019-07-05T14:24:23.980860Z 1 [Note] A temporary password is generated for root@localhost: RN=hsqIFX2BO
[root@slave ~]# systemctl start mysql
Failed to start mysql.service: Unit not found.
[root@slave ~]# systemctl start mysqld
[root@slave ~]# mysql -uroot -p
Enter password:
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 2
Server version: 5.7.26-log
mysql> ALTER USER 'root'@'localhost' IDENTIFIED BY '123456';
Query OK, 0 rows affected (0.00 sec)
mysql> show global variables like 'server_uuid';
+---------------+--------------------------------------+
| Variable_name | Value |
+---------------+--------------------------------------+
| server_uuid | 96435cd6-9f30-11e9-a575-000c2963fd11 |
+---------------+--------------------------------------+
1 row in set (0.00 sec)

[root@slave ~]# cd /data/mysql/

[root@slave mysql]# cat auto.cnf

三 配置主从

3.1 主库给从库复制权限

mysql> grant replication slave on *.* to 'replication'@'192.168.132.122' identified by '';
Query OK, rows affected, warning (0.01 sec)
mysql> flush privileges;
Query OK, rows affected (0.01 sec)
mysql> show master logs;
+-------------------+-----------+
| Log_name | File_size |
+-------------------+-----------+
| master-bin. | |
| master-bin. | |
+-------------------+-----------+
mysql> show binlog events in 'master-bin.000002';
+-------------------+-----+----------------+-----------+-------------+----------------------------------------------------------------------------------------------------------------------------------------------------------+
| Log_name | Pos | Event_type | Server_id | End_log_pos | Info |
+-------------------+-----+----------------+-----------+-------------+----------------------------------------------------------------------------------------------------------------------------------------------------------+
| master-bin. | | Format_desc | | | Server ver: 5.7.-log, Binlog ver: |
| master-bin. | | Previous_gtids | | | |
| master-bin. | | Gtid | | | SET @@SESSION.GTID_NEXT= 'ad5ee77e-9f2e-11e9-a76d-000c2991dd19:1' | # 事件类型是GTID 数据库的uuid+事务ID
| master-bin. | | Query | | | ALTER USER 'root'@'localhost' IDENTIFIED WITH 'mysql_native_password' AS '*6BB4837EB74329105EE4568DDA7DC67ED2CA2AD9' |
| master-bin. | | Gtid | | | SET @@SESSION.GTID_NEXT= 'ad5ee77e-9f2e-11e9-a76d-000c2991dd19:2' |
| master-bin. | | Query | | | GRANT REPLICATION SLAVE ON *.* TO 'replication'@'192.168.132.122' IDENTIFIED WITH 'mysql_native_password' AS '*6A7A490FB9DC8C33C2B025A91737077A7E9CC5E5' |
| master-bin. | | Gtid | | | SET @@SESSION.GTID_NEXT= 'ad5ee77e-9f2e-11e9-a76d-000c2991dd19:3' |
| master-bin. | | Query | | | flush privileges |
+-------------------+-----+----------------+-----------+-------------+----------------------------------------------------------------------------------------------------------------------------------------------------------+

3.2 从库使用命令进行复制

mysql> change master to master_host='192.168.132.121',master_port=,master_user='replication',master_password='',master_auto_position = ;
Query OK, rows affected, warnings (0.01 sec) mysql> start slave;
Query OK, rows affected (0.01 sec) mysql> show slave status\G;
*************************** . row ***************************
Slave_IO_State: Waiting for master to send event
Master_Host: 192.168.132.121
Master_User: replication
Master_Port:
Connect_Retry:
Master_Log_File: master-bin.
Read_Master_Log_Pos:
Relay_Log_File: relay-log.
Relay_Log_Pos:
Relay_Master_Log_File: master-bin.
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:
Last_Error:
Skip_Counter:
Exec_Master_Log_Pos:
Relay_Log_Space:
Until_Condition: None
Until_Log_File:
Until_Log_Pos:
Master_SSL_Allowed: No
Master_SSL_CA_File:
Master_SSL_CA_Path:
Master_SSL_Cert:
Master_SSL_Cipher:
Master_SSL_Key:
Seconds_Behind_Master:
Master_SSL_Verify_Server_Cert: No
Last_IO_Errno:
Last_IO_Error:
Last_SQL_Errno:
Last_SQL_Error:
Replicate_Ignore_Server_Ids:
Master_Server_Id:
Master_UUID: ad5ee77e-9f2e-11e9-a76d-000c2991dd19 #主库的UUID
Master_Info_File: /data/mysql/master.info
SQL_Delay:
SQL_Remaining_Delay: NULL
Slave_SQL_Running_State: Slave has read all relay log; waiting for more updates
Master_Retry_Count:
Master_Bind:
Last_IO_Error_Timestamp:
Last_SQL_Error_Timestamp:
Master_SSL_Crl:
Master_SSL_Crlpath:
Retrieved_Gtid_Set: ad5ee77e-9f2e-11e9-a76d-000c2991dd19:-3 #执行的GTID位置
Executed_Gtid_Set: 96435cd6-9f30-11e9-a575-000c2963fd11:,
ad5ee77e-9f2e-11e9-a76d-000c2991dd19:-
Auto_Position:
Replicate_Rewrite_DB:
Channel_Name:
Master_TLS_Version:

3.3 观察从的binlog日志

mysql> show master logs;
+-------------------+-----------+
| Log_name | File_size |
+-------------------+-----------+
| master-bin. | |
| master-bin. | |
+-------------------+-----------+
mysql> show binlog events in 'master-bin.000002';
+-------------------+------+----------------+-----------+-------------+----------------------------------------------------------------------------------------------------------------------------------------------------------+
| Log_name | Pos | Event_type | Server_id | End_log_pos | Info |
+-------------------+------+----------------+-----------+-------------+----------------------------------------------------------------------------------------------------------------------------------------------------------+
| master-bin. | | Format_desc | | | Server ver: 5.7.-log, Binlog ver: |
| master-bin. | | Previous_gtids | | | |
| master-bin. | | Gtid | | | SET @@SESSION.GTID_NEXT= '96435cd6-9f30-11e9-a575-000c2963fd11:1' |
| master-bin. | | Query | | | ALTER USER 'root'@'localhost' IDENTIFIED WITH 'mysql_native_password' AS '*6BB4837EB74329105EE4568DDA7DC67ED2CA2AD9' |
| master-bin. | | Gtid | | | SET @@SESSION.GTID_NEXT= 'ad5ee77e-9f2e-11e9-a76d-000c2991dd19:1' |
| master-bin. | | Query | | | ALTER USER 'root'@'localhost' IDENTIFIED WITH 'mysql_native_password' AS '*6BB4837EB74329105EE4568DDA7DC67ED2CA2AD9' |
| master-bin. | | Gtid | | | SET @@SESSION.GTID_NEXT= 'ad5ee77e-9f2e-11e9-a76d-000c2991dd19:2' |
| master-bin. | | Query | | | GRANT REPLICATION SLAVE ON *.* TO 'replication'@'192.168.132.122' IDENTIFIED WITH 'mysql_native_password' AS '*6A7A490FB9DC8C33C2B025A91737077A7E9CC5E5' |
| master-bin. | | Gtid | | | SET @@SESSION.GTID_NEXT= 'ad5ee77e-9f2e-11e9-a76d-000c2991dd19:3' |
| master-bin. | | Query | | | flush privileges |
+-------------------+------+----------------+-----------+-------------+----------------------------------------------------------------------------------------------------------------------------------------------------------+ 

四 验证

增加数据来验证是否同步成功

4.1 建库验证

主创建一个数据库

mysql> create database darren;

mysql> show binlog events in 'master-bin.000002';

从端查看

mysql> show databases;

mysql> show slave status\G;

4.2 建表验证

主创建一个表

mysql> use darren;
Database changed
mysql> create table test (id int);
Query OK, rows affected (0.01 sec) mysql> show binlog events in 'master-bin.000002';
+-------------------+------+----------------+-----------+-------------+----------------------------------------------------------------------------------------------------------------------------------------------------------+
| Log_name | Pos | Event_type | Server_id | End_log_pos | Info |
+-------------------+------+----------------+-----------+-------------+----------------------------------------------------------------------------------------------------------------------------------------------------------+
| master-bin. | | Gtid | | | SET @@SESSION.GTID_NEXT= 'ad5ee77e-9f2e-11e9-a76d-000c2991dd19:3' |
| master-bin. | | Query | | | flush privileges |
| master-bin. | | Gtid | | | SET @@SESSION.GTID_NEXT= 'ad5ee77e-9f2e-11e9-a76d-000c2991dd19:4' |
| master-bin. | | Query | | | create database darren |
| master-bin. | | Gtid | | | SET @@SESSION.GTID_NEXT= 'ad5ee77e-9f2e-11e9-a76d-000c2991dd19:5' |
| master-bin. | | Query | | | use `darren`; create table test (id int) #建表,同是增长ID |
+-------------------+------+----------------+-----------+-------------+----------------------------------------------------------------------------------------------------------------------------------------------------------+

从端:

mysql> show slave status\G;

4.3.插改验证

mysql> insert into test values (1);

mysql> update test set id = 3 where id = 1;

mysql> show binlog events in 'master-bin.000002';

从端查看

mysql> show slave status\G;

mysql> select * from darren.test;

删除

mysql> delete from test;

mysql> drop database darren;

mysql> show binlog events in 'master-bin.000002';

从端查看

mysql> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| mysql |
| performance_schema |
| sys |
+--------------------+
rows in set (0.00 sec)
mysql> show binlog events in 'master-bin.000002';

基本的配置验证完成

五 日志名未指定,主机名改变导致主从失效

前面介绍过类似的情况,因为配置文件没有指定日志名,导致MySQL在重启的时候,日志名发生改变,导致同步失败

从端的slave日志未指定,解决

stop slave;    #停掉salve

reset slave;   #重置salve

change master to master_host='192.168.132.121',master_port=3306,master_user='replication',master_password='1234567',master_auto_position = 1;   #重新配从端复制数据

start slave;    #开启slave

主端未指定,修复主从

stop slave;   #停掉slave

reset slave;   #重置slave

reset master;  #重置master,必须做,很重要

change master to  master_host='192.168.132.121',master_port=3306, master_user='replication',master_password='123456',master_auto_position = 1;  3重新配置主从

start slave; #开启slave

mysql主从之基于gtid的主从复制的更多相关文章

  1. percona mysql server5.7基于gtid的主从复制

    配置mysql基于gtid主从复制架构 一.二进制安装mysql [root@node5 data]# --Linux.x86_64.ssl101.tar.gz [root@node5 data]# ...

  2. MySQL 5.7基于GTID的主从复制

            since i've broken down the replication enviornment by "reset master;" yesterday.th ...

  3. MySQL 5.7基于GTID的主从复制环境搭建(一主一从)

      Preface       As wel all know,replication base on GTID with row format of binary log is comprehens ...

  4. Mysql5.7实现主从复制、基于GTID的主从复制、并行复制

    (一.主从复制) 一.mysql主从复制原理    mysql的默认复制方式是主从复制.Mysql内建的复制功能是构建大型,高性能应用程序的基础.将Mysql的数据分布到多个系统上去,这种分布的机制, ...

  5. MySQL 5.7基于GTID复制的常见问题和修复步骤(二)

    [问题二] 有一个集群(MySQL5.7.23)切换后复制slave报1236,其实是不小心在slave上执行了事务导致 Got fatal error 1236 from master when r ...

  6. Windows系统环境下创建mysql主从数据库方法(双向主从复制)

    创建mysql主从数据库方法(双向主从复制) (一)Windows系统下的MySQL主从复制(单向复制) (1)环境说明: 1,Mysql版本:mysql5.7.20(主从机mysql版本必须一致) ...

  7. 实现mysql的读写分离(mysql-proxy)____1(mysql的主从复制,基于gtid的主从复制,半同步复制,组复制)

    主从复制原理: 从库生成两个线程,一个I/O线程,一个SQL线程: i/o线程去请求主库 的binlog,并将得到的binlog日志写到relay log(中继日志) 文件中:主库会生成一个 log ...

  8. MySQL 5.7 基于GTID主从复制+并行复制+半同步复制

    环境准备 IP HOSTNAME SERVICE SYSTEM 192.168.131.129 mysql-master1 mysql CentOS7.6 192.168.131.130 mysql- ...

  9. MySQL5.7.18基于GTID的主从复制过程实现

    GTID是5.6时加入的,在5.7中被进一步完善,生产环境建议在5.7版本中使用.GTID全称为Global Transaction Identifiers,全局事务标识符.GTID的复制完全是基于事 ...

随机推荐

  1. 算法导论笔记:18B树

    磁盘作为辅存,它的容量要比内存大得多,但是速度也要慢许多,下面就是磁盘的的结构图: 磁盘驱动器由一个或多个盘片组成,它们以固定的速度绕着主轴旋转,数据存储于盘片的表面,磁盘驱动器通过磁臂末尾的磁头来读 ...

  2. 自动编码(AE)器的简单实现

    一.目录 自动编码(AE)器的简单实现 一.目录 二.自动编码器的发展简述 2.1 自动编码器(Auto-Encoders,AE) 2.2 降噪自编码(Denoising Auto-Encoders, ...

  3. React Native声明属性和属性确认

    属性声明 因为用React Native创建的自定义组件可以复用, 我们开发过程中可能一个项目组有多个人同时开发,其他同事可能会用到我们自定义的组件, 但是他们使用的时候很容易忘记使用某些属性,这时候 ...

  4. python---异常处理与反射

    一.异常处理 1.异常基础 在编程过程中为了增加友好性,在程序出现bug时一般不会将错误信息显示给用户,而是现实一个提示的页面,通俗来说就是不让用户看见大黄页!!! try: pass except ...

  5. Jquery常用方法汇总(转)

    https://blog.csdn.net/lucky___star/article/details/87883888

  6. 操作SDO_GEOMETRY字段

    读取SDO_GEOMETRY字段 select  to_char(regexp_replace(sdo_util.to_gmlgeometry(t.intsxn_geom),'</?[^> ...

  7. behavior planning——inputs to transition functions

    the answer is that we have to pass all  of the data into transition function except for the previous ...

  8. jsp获取后端java路由返回值

    html: <input type="hidden" id="tableName" value="${id}"> javascr ...

  9. 阿里云ECS服务器活动99元一年,最高可买三年

    这几天阿里云 99一年.279三年的服务器活动如火如荼,和之前腾讯三年的服务器非常类似,非常低的价格换取非常高的价值,当然,通常情况下便宜没好货的,想要玩一下的老铁可以进阿里云去看看,阿里云270三年 ...

  10. SQL 循环语句

    一.if语句 二.while语句 练习: 三.case when 四.练习 1. 2. 3. 4.