mysql传统与gtid的主从配置
主从作用
实时灾备,用于故障切换
读写分离,提供查询服务
备份,避免影响业务
传统主从和gtid主从的区别
传统主从
传统主从复制主要是基于二进制日志文件位置的复制,因此主必须启动二进制日志记录并建立唯一的服务器ID,复制组中的每个服务器都必须配置唯一的服务器ID。如果您省略server-id(或者明确地将其设置为其默认值0),则主设备将拒绝来自从设备的任何连接。
gtid主从
MySQL 5.6 的新特性之一,全局事务标识符(GTID)是创建的唯一标识符,并与在源(主)服务器上提交的每个事务相关联。此标识符不但是唯一的,而且在给定复制设置中的所有服务器上都是唯一的。所有交易和所有GTID之间都有一对一的映射关系 。它由服务器ID以及事务ID组合而成。这个全局事务ID不仅仅在原始服务器上唯一,在所有存在主从关系 的mysql服务器上也是唯一的。正是因为这样一个特性使得mysql的主从复制变得更加简单,以及数据库一致性更可靠。一个GTID在一个服务器上只执行一次,避免重复执行导致数据混乱或者主从不一致
在传统的主从复制slave端,binlog是不用开启的,但是在GTID中slave端的binlog是必须开启的,目的是记录执行过的GTID(强制)。GTID用来代替classic的复制方法,不在使用binlog+pos开启复制。而是使用master_auto_postion=1的方式自动匹配GTID断点进行复制
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、在解析过程中会判断是否有主键,如果有就用二级索引,如果没有就用全部扫描
主从的复制原理
主库讲所有写的操作记录到binlog日志生成一个log dump线程,讲binlog日志传给I/O线程
从库生成一个I/O线程和SQL线程
I/O线程去请求主库的binlog,并将得到的binlog日志写到relay log(中继日志) 文件中
SQL线程,会读取relay log文件中的日志,并解析成具体操作,来实现主从的操作一致,达到最终数据一致的目的
主从复制配置
主从复制配置步骤:
确保从数据库与主数据库里的数据一样
在主数据库里创建一个同步账号授权给从数据库使用
配置主数据库(修改配置文件)
配置从数据库(修改配置文件)
mysql的主从配置
#查看主库有那些库
[root@czh ~]# mysql -uroot -pCzh1.com -e 'show databases;'
mysql: [Warning] Using a password on the command line interface can be insecure.
+--------------------+
| Database |
+--------------------+
| information_schema |
| chenzihan |
| czh |
| mysql |
| performance_schema |
| sys |
+--------------------+
#查看第一个从库的内容
[root@czh ~]# mysql -uroot -pCzh1.com -e 'show databases;'
mysql: [Warning] Using a password on the command line interface can be insecure.
+--------------------+
| Database |
+--------------------+
| information_schema |
| mysql |
| performance_schema |
| sys |
+--------------------+
#全备主库
#全备主库时需要另开一个终端,给数据库加上读锁,避免在备份期间有其他人在写入导致数据不一致
#先上锁,在复制一个标签进行全备
mysql> flush tables with read lock;
Query OK, 0 rows affected (0.01 sec)
mysql> quit
Bye
[root@czh ~]# mysqldump -uroot -pCzh1.com --all-databases > all511.sql
把主数据库的全备内容传到数据库上面
[root@czh ~]# scp /root/all511.sql root@192.168.31.138:/opt
The authenticity of host '192.168.31.138 (192.168.31.138)' can't be established.
ECDSA key fingerprint is SHA256:bZLttAV6vmKUbi/+j61TJOXzr9qpcdRfi1/PVxi1vTc.
Are you sure you want to continue connecting (yes/no/[fingerprint])? yees
Please type 'yes', 'no' or the fingerprint: yes
Warning: Permanently added '192.168.31.138' (ECDSA) to the list of known hosts.
root@192.168.31.138's password:
all511.sql 100% 854KB 72.1MB/s 00:00
#传过去后 导入从数据库进行查看
[root@czh ~]# ll /opt/
总用量 860
-rw-r--r--. 1 root root 874545 5月 11 11:40 all511.sql
drwxr-xr-x. 5 mysql mysql 4096 5月 11 10:26 mysql_data
[root@czh ~]# mysql -uroot -pCzh1.com < /opt/all511.sql
mysql: [Warning] Using a password on the command line interface can be insecure.
[root@czh ~]# mysql -uroot -pCzh1.com -e 'show databases;'
mysql: [Warning] Using a password on the command line interface can be insecure.
+--------------------+
| Database |
+--------------------+
| information_schema |
| chenzihan |
| czh |
| mysql |
| performance_schema |
| sys |
+--------------------+
#查看是否和主的一样
[root@czh ~]# mysql -uroot -pCzh1.com -e 'select * from czh.student;'
mysql: [Warning] Using a password on the command line interface can be insecure.
+----+-------------+------+
| id | name | age |
+----+-------------+------+
| 1 | tom | 20 |
| 2 | jerry | 23 |
| 3 | wangqing | 25 |
| 4 | sean | 28 |
| 5 | zhangshan | 26 |
| 7 | lisi | 50 |
| 8 | chenshuo | 10 |
| 9 | wangwu | 100 |
| 10 | qiuyi | 15 |
| 11 | qiuxiaotian | 20 |
+----+-------------+------+
[root@czh2 ~]# mysql -uroot -pCzh1.com < /opt/all511.sql
mysql: [Warning] Using a password on the command line interface can be insecure.
[root@czh2 ~]# mysql -uroot -pCzh1.com -e 'show databases;'
mysql: [Warning] Using a password on the command line interface can be insecure.
+--------------------+
| Database |
+--------------------+
| information_schema |
| chenzihan |
| czh |
| mysql |
| performance_schema |
| sys |
+--------------------+
在主数据库创建一个同步帐号授权给从数据库
mysql> create user 'tom'@'192.168.31.138' identified by 'tom123';
Query OK, 0 rows affected (0.01 sec)
mysql> GRANT REPLICATION SLAVE ON *.* TO 'tom'@'192.168.31.138';
Query OK, 0 rows affected (0.01 sec)
mysql> CREATE USER 'zzz'@'192.168.31.139' IDENTIFIED BY 'zzz123';
Query OK, 0 rows affected (0.00 sec)
mys