GTID主从与传统主从复制
GTID主从与传统主从复制
1.主从复制
- 普通主从复制:
普通主从复制主要是基于二进制日志文件位置的复制,因此主必须启动二进制日志记录并建立唯一的服务器ID,复制组中的每个服务器都必须配置唯一的服务器ID。如果您省略server-id(或者明确地将其设置为其默认值0),则主设备将拒绝来自从设备的任何连接。
- GTID 主从:
MySQL 5.6 的新特性之一,全局事务标识符(GTID)是创建的唯一标识符,并与在源(主)服务器上提交的每个事务相关联。此标识符不但是唯一的,而且在给定复制设置中的所有服务器上都是唯一的。所有交易和所有GTID之间都有一对一的映射关系 。它由服务器ID以及事务ID组合而成。这个全局事务ID不仅仅在原始服务器上唯一,在所有存在主从关系 的mysql服务器上也是唯一的。正是因为这样一个特性使得mysql的主从复制变得更加简单,以及数据库一致性更可靠。一个GTID在一个服务器上只执行一次,避免重复执行导致数据混乱或者主从不一致。
2.靠什么同步
主从复制,默认是通过pos复制(postion),就是说在日志文档里,将用户进行的每一项操作都进行编号(pos),每一个event都有一个起始编号,一个终止编号,我们在配置主从复制时从节点时,要输入master的log_pos值就是这个原因,要求它从哪个pos开始同步数据库里的数据,这也是传统复制技术.
MySQL5.6之后增加了GTID复制,GTID就是类似于pos的一个作用,不过它是整个mysql复制架构全局通用的,就是说在这整个mysql冗余架构中,它们的日志文件里事件的GTID值是一致的.
3.pos与GTID的什么区别
两者都是日志文件里事件的一个标志,如果将整个mysql集群看作一个整体,pos就是局部的,GTID就是全局的.
上图就是一个mysql节点的集群,一主两从,在master,slave1,slave2日志文件里的pos,都各不相同,就是一个event,在master的日志里,pos可能是700,而在slave1,slave2里,pos可能就是300,400了,因为众多slave也可能不是同时加入集群的,不是从同一个位置进行同步.
而GTID,在master,slave1,slave2各自的日志文件里,同一个event的GTID值都是一样的.
大家都知道,这整个集群架构的节点,通常情况下,是master在工作,其他两个结点做备份,而且,各个节点的机器,性能不可能完全一致,所以,在做备份时,备份的速度就不一样,当master突然crash掉之后,马上会启用从节点机器,接管master的工作,当有多个从节点时,选择备份日志文件最接近master的那个节点
现在就出现情况了,当salve1变成主节点,那slave2就应该从slave1去获取日志文件,进行同步
如果使用的是pos,三者的pos不一致,slave2怎么去获取它当前要同步的事件在slave1里的pos呢,很难.
所以就有了GTID,全局的,将所有节点对于同一个event的标记完全一致,当master crash掉之后,slave2根据同一个GTID直接去读取slave1的日志文件,继续同步.
4.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、在解析过程中会判断是否有主键,如果有就用二级索引,如果没有就用全部扫描。
5.GTID参数配置
master:192.168.112.174
slave:192.168.112.175
服务端需要关闭防火墙和selinux
5.1 在主数据库里创建一个同步账号授权给从数据库使用
[root@localhost ~]# mysql -uroot -pcwh123!
mysql: [Warning] Using a password on the command line interface can be insecure.
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 3
Server version: 5.7.22 MySQL Community Server (GPL)
Copyright (c) 2000, 2018, Oracle and/or its affiliates. All rights reserved.
Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
mysql> GRANT REPLICATION SLAVE ON *.* TO 'repl'@'192.168.112.175' identified by 'repl123';
Query OK, 0 rows affected, 1 warning (0.00 sec)
mysql> flush privileges;
Query OK, 0 rows affected (0.00 sec)
5.2 配置主数据库
//写配置文件
[root@localhost ~]# vim /etc/my.cnf
[mysqld]
basedir = /usr/local/mysql
datadir = /opt/data
socket = /tmp/mysql.sock
port = 3306
pid-file = /opt/data/mysql.pid
user = mysql
skip-name-resolve
log-bin = mysql-bin //开启log-bin日志
server-id = 1 //定义server-id
gtid-mode = on //开启gtid复制
enforce-gtid-consistency = on //强制gtid一致
//重启mysql
[root@localhost ~]# /etc/init.d/mysqld restart
Shutting down MySQL.. SUCCESS!
Starting MySQL. SUCCESS!
//查看master状态
[root@localhost ~]# mysql -uroot -pcwh123!
mysql: [Warning] Using a password on the command line interface can be insecure.
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 2
Server version: 5.7.22-log MySQL Community Server (GPL)
Copyright (c) 2000, 2018, Oracle and/or its affiliates. All rights reserved.
Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
mysql> show master status;
+------------------+----------+--------------+------------------+-------------------+
| File | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set |
+------------------+----------+--------------+------------------+-------------------+
| mysql-bin.000002 | 154 | | | |
+------------------+----------+--------------+------------------+-------------------+
1 row in set (0.00 sec)
5.3配置从数据库
//写配置文件
[root@localhost ~]# vim /etc/my.cnf
[mysqld]
basedir = /usr/local/mysql
datadir = /opt/data
socket = /tmp/mysql.sock
port = 3306
pid-file = /opt/data/mysql.pid
user = mysql
skip-name-resolve
server-id = 6
gtid-mode = on
enforce-gtid-consistency = on
log-bin = mysql-bin
//重启服务
[root@localhost ~]# /etc/init.d/mysqld restart
Shutting down MySQL.. SUCCESS!
Starting MySQL. SUCCESS!
//配置并启动gtid主从复制
[root@localhost ~]# mysql -uroot -pcwh123!
mysql: [Warning] Using a password on the command line interface can be insecure.
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 2
Server version: 5.7.22-log MySQL Community Server (GPL)
Copyright (c) 2000, 2018, Oracle and/or its affiliates. All rights reserved.
Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
mysql> change master to master_host='192.168.112.174',master_user='repl',master_password='repl123',master_auto_position=1;
Query OK, 0 rows affected, 2 warnings (0.01 sec)
mysql> start slave;
Query OK, 0 rows affected (0.00 sec)
//查看从服务器状态
mysql> start slave;
Query OK, 0 rows affected (0.00 sec)
mysql> show slave status\G;
*************************** 1. row ***************************
Slave_IO_State: Waiting for master to send event
Master_Host: 192.168.112.174
Master_User: repl
Master_Port: 3306
Connect_Retry: 60
Master_Log_File: mysql-bin.000002
Read_Master_Log_Pos: 154
Relay_Log_File: localhost-relay-bin.000002
Relay_Log_Pos: 367
Relay_Master_Log_File: mysql-bin.000002
Slave_IO_Running: Yes
Slave_SQL_Running: Yes
5.4测试验证
//在主服务器中加入数据库cwh
[root@localhost ~]# mysql -uroot -pcwh123!
mysql: [Warning] Using a password on the command line interface can be insecure.
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 4
Server version: 5.7.22-log MySQL Community Server (GPL)
Copyright (c) 2000, 2018, Oracle and/or its affiliates. All rights reserved.
Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
mysql> create database cwh;
Query OK, 1 row affected (0.00 sec)
mysql> quit
mysql> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| cwh |
| mysql |
| performance_schema |
| sys |
+--------------------+
5 rows in set (0.00 sec)
//在从服务器上查看是否同步
mysql> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| cwh |
| mysql |
| performance_schema |
| sys |
+--------------------+
5 rows in set (0.00 sec)
//可以看出来成功了
最新文章
- Dapper:The member of type SeoTKD cannot be used as a parameter Value
- js生成GUID
- 数据源DBCP一二
- CodeIgniter类的使用
- /var/spool/postfix/maildrop 占用inode索引及磁盘空间解决办法
- 在sqlServer中把数据导出为insert脚本
- 微信公众平台应用开发:方法、技巧与案例--柳峰,Java语言版本
- LTP学习
- html中的body标签
- thinkphp 行为扩展
- 利用Graphviz 画结构图[转]
- python文件处理--笔记
- Hibernate 知识点复习
- PyCharm 使用简介
- epoll模型的使用
- Java语言基础组成
- vue+webpack 安装常见插件
- 爬虫-day01-基础知识
- go连接mysql
- [转]TA-Lib 安装
热门文章
- LeetCode_482. License Key Formatting
- 硬盘分区及Linux文件系统
- elk配置路径
- Difference between java.lang.RuntimeException and java.lang.Exception
- charles 4.2.1 Ubuntu破解版安装
- Linux下使用matlab在后台默默的运行.m文件(无界面形式)
- RDA的使用和说明
- git第一次上传push失败解决
- python 之 面向对象(元类、__call__、单例模式)
- Java中是使用增强for的null问题