mysql主从与zabbix监控

  • 1. 主从简介
  • 1.1 主从作用
  • 1.2 主从形式
  • 2. 主从复制原理
  • 3. 主从复制配置
  • 3.1mysql安装
  • 3.2 mysql主从配置
  • 3.2.1 确保从数据库与主数据库里的数据一样
  • 3.2.2 在主数据库里创建一个同步账号授权给从数据库使用
  • 3.2.3 配置主数据库
  • 3.2.4 配置从数据库
  • 3.2.5 测试验证
  • 3.3mysql主从监控
  • 4.GTID主从的配置
  • 4.1原理:
  • 4.2作用:
  • 4.3GTID的搭建:
  • 4.3.1安装mysql
  • 4.3.2配置GTID参数
  • 4.3.3授权同步账号
  • 4.3.4配置
  • 4.4验证
  • 5.传统主从与GTID主从区别


1. 主从简介

在现代企业中,数据显得尤为重要,而存储数据的数据库选择又五花八门,但无论是何种数据库,均存在着一种隐患。

想几个问题:

  • 用一台数据库存放数据,若此数据库服务器宕机了导致数据丢失怎么办?
  • 业务量大了,数据多了,访问的人多了,一台数据库无法保证服务质量了怎么办?

1.1 主从作用

  • 实时灾备,用于故障切换
  • 读写分离,提供查询服务
  • 备份,避免影响业务

1.2 主从形式

mysql主库链接时起始的binlog偏移量 如何查询_数据库

  • 一主一从
  • 主主复制
  • 一主多从—扩展系统读取的性能,因为读是在从库读取的
  • 多主一从—5.7开始支持
  • 联级复制

2. 主从复制原理

mysql主库链接时起始的binlog偏移量 如何查询_mysql_02

主从复制步骤:

  • 主库将所有的写操作记录到binlog日志中并生成一个log dump线程,将binlog日志传给从库的I/O线程
  • 从库生成两个线程,一个I/O线程,一个SQL线程
  • I/O线程去请求主库的binlog,并将得到的binlog日志写到relay log(中继日志) 文件中
  • SQL线程,会读取relay log文件中的日志,并解析成具体操作,来实现主从的操作一致,达到最终数据一致的目的

3. 主从复制配置

主从复制配置步骤:

  1. 确保从数据库与主数据库里的数据一样
  2. 在主数据库里创建一个同步账号授权给从数据库使用
  3. 配置主数据库(修改配置文件)
  4. 配置从数据库(修改配置文件)

需求:
搭建两台MySQL服务器,一台作为主服务器,一台作为从服务器,主服务器进行写操作,从服务器进行读操作

环境说明:

数据库角色

IP

应用与系统版本

有无数据

主数据库

192.168.110.21

centos7/redhat7

mysql-5.7

有数据

从数据库

192.168.110.20

centos7/redhat7

mysql-5.7

无数据

3.1mysql安装

3.2 mysql主从配置

3.2.1 确保从数据库与主数据库里的数据一样

//主数据库
[root@lw ~]# systemctl start mysqld
[root@lw ~]# mysql
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 2
Server version: 5.7.23-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> use liuwei;
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -A

Database changed
mysql> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| liuwei             |
| mysql              |
| performance_schema |
| sys                |
| zabbix             |
+--------------------+
6 rows in set (0.00 sec)

mysql> quit
Bye
//全备主库时需要另开一个终端,给数据库加上读锁,避免在备份期间有其他人在写入导致数据不一致
mysql> FLUSH TABLES WITH READ LOCK;
Query OK, 0 rows affected (0.02 sec)
//此锁表的终端必须在备份完成以后才能退出

//备份主库并将备份文件传送到从库
[root@lw ~]# mysqldump --all-databases > all.sql
[root@lw ~]# vim all.sql
[root@lw ~]# scp all.sql 192.168.110.20:/root/
root@192.168.110.20's password: 
all.sql                                                         100% 7460KB   3.6MB/s   00:02
mysql> select * from liuwei.student;
+----+------------+------+
| id | name       | age  |
+----+------------+------+
|  1 | tom        |   18 |
|  2 | liumingjun |   22 |
|  3 | liuwei     |   21 |
|  4 | lizhao     |   19 |
+----+------------+------+
4 rows in set (0.01 sec)

//从数据库
//在从库上恢复主库的备份并查看从库有哪些库,确保与主库一致
mysql> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| mysql              |
| performance_schema |
| sys                |
+--------------------+
4 rows in set (0.01 sec)

mysql> quit
Bye
[root@liuwei ~]# mysql -uroot -plw1234 <all.sql 
mysql: [Warning] Using a password on the command line interface can be insecure.
[root@liuwei ~]# mysql
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 7
Server version: 5.7.25 MySQL Community Server (GPL)

Copyright (c) 2000, 2019, 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 databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| liuwei             |
| mysql              |
| performance_schema |
| sys                |
| zabbix             |
+--------------------+
6 rows in set (0.01 sec)

mysql> select * from liuwei.studeent;
ERROR 1146 (42S02): Table 'liuwei.studeent' doesn't exist
mysql> select * from liuwei.student;
+----+------------+------+
| id | name       | age  |
+----+------------+------+
|  1 | tom        |   18 |
|  2 | liumingjun |   22 |
|  3 | liuwei     |   21 |
|  4 | lizhao     |   19 |
+----+------------+------+
4 rows in set (0.00 sec)

3.2.2 在主数据库里创建一个同步账号授权给从数据库使用

//主数据库授权
mysql> grant replication slave on *.* to 'lw'@'192.168.110.20' identified by 'lw1234';
Query OK, 0 rows affected, 1 warning (0.03 sec)
mysql> flush privileges;  //刷新数据库
Query OK, 0 rows affected (0.02 sec)

//从数据库查看
[root@liuwei ~]# mysql -ulw -plw1234 -h192.168.110.21 
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 7
Server version: 5.7.23-log MySQL Community Server (GPL)

Copyright (c) 2000, 2019, 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>

3.2.3 配置主数据库

//主数据库添加
[root@lw ~]# tail -2 /etc/my.cnf
server-id = 1   //数据库服务器唯一标识符,主库的server-id值必须比从库的小
log-bin = mysql-bin  //启用binlog日志

//重启mysql服务
[root@lw ~]# systemctl restart mysqld
[root@lw ~]# ss -antl
State      Recv-Q Send-Q    Local Address:Port                   Peer Address:Port              
LISTEN     0      128                   *:22                                *:*                  
LISTEN     0      10            127.0.0.1:25                                *:*                  
LISTEN     0      5                     *:873                               *:*                  
LISTEN     0      32                   :::21                               :::*                  
LISTEN     0      128                  :::22                               :::*                  
LISTEN     0      5                    :::873                              :::*                  
LISTEN     0      80                   :::3306                             :::*

//查看主库的状态
mysql> show master status;
+------------------+----------+--------------+------------------+-------------------+
| File             | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set |
+------------------+----------+--------------+------------------+-------------------+
| mysql-bin.000012 |      154 |              |                  |                   |
+------------------+----------+--------------+------------------+-------------------+
1 row in set (0.00 sec)

3.2.4 配置从数据库

[root@liuwei ~]# tail -2 /etc/my.cnf
server-id=2
relay-log=mysql-relay-bin

//重启从库的mysql服务
[root@liuwei ~]# systemctl restart mysqld
[root@liuwei ~]# ss -antl
State      Recv-Q Send-Q    Local Address:Port                   Peer Address:Port              
LISTEN     0      128                   *:22                                *:*                  
LISTEN     0      100           127.0.0.1:25                                *:*                  
LISTEN     0      32                   :::21                               :::*                  
LISTEN     0      128                  :::22                               :::*                  
LISTEN     0      100                 ::1:25                               :::*                  
LISTEN     0      80                   :::3306                             :::*        

//配置并启动主从复制
mysql> change master to master_host='192.168.110.21',master_user='lw',master_password='lw1234',master_log_file='mysql-bin.000012',master_log_pos=154;
Query OK, 0 rows affected, 2 warnings (0.19 sec)

mysql> start slave;
Query OK, 0 rows affected (0.02 sec)

mysql> show slave status\G
*************************** 1. row ***************************
               Slave_IO_State: Waiting for master to send event
                  Master_Host: 192.168.110.21
                  Master_User: lw
                  Master_Port: 3306
                Connect_Retry: 60
              Master_Log_File: mysql-bin.000012
          Read_Master_Log_Pos: 154
               Relay_Log_File: mysql-relay-bin.000002
                Relay_Log_Pos: 320
        Relay_Master_Log_File: mysql-bin.000012
             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: 154
              Relay_Log_Space: 527
              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: 87b6b4ac-34e6-11e9-b93c-000c29e97649
             Master_Info_File: /var/lib/mysql/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: 
                Auto_Position: 0
         Replicate_Rewrite_DB: 
                 Channel_Name: 
           Master_TLS_Version: 
1 row in set (0.07 sec)

3.2.5 测试验证

在主服务器的liuwei库的student表中插入数据:

//主数据库
mysql> use liuwei;
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -A

Database changed
mysql> insert student values (5,'chengshuai',3),(6,'pengye',18);
Query OK, 2 rows affected (0.06 sec)
Records: 2  Duplicates: 0  Warnings: 0

mysql> select * from student;
+----+------------+------+
| id | name       | age  |
+----+------------+------+
|  1 | tom        |   18 |
|  2 | liumingjun |   22 |
|  3 | liuwei     |   21 |
|  4 | lizhao     |   19 |
|  5 | chengshuai |    3 |
|  6 | pengye     |   18 |
+----+------------+------+
6 rows in set (0.00 sec)

//从数据库
mysql> use liuwei;
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -A

Database changed
mysql> select * from student;
+----+------------+------+
| id | name       | age  |
+----+------------+------+
|  1 | tom        |   18 |
|  2 | liumingjun |   22 |
|  3 | liuwei     |   21 |
|  4 | lizhao     |   19 |
|  5 | chengshuai |    3 |
|  6 | pengye     |   18 |
+----+------------+------+
6 rows in set (0.00 sec)

3.3mysql主从监控

延迟:

//脚本
[root@liuwei ~]# cat /scripts/check_status.sh 
#!/bin/bash
a=$(mysql -uroot -plw1234 -e 'show slave status\G' 2>/dev/null |grep 'Read_Master_Log_Pos'|awk -F: '{print $2}')
b=$(mysql -uroot -plw1234 -e 'show slave status\G' 2>/dev/null |grep 'Exec_Master_Log_Pos'|awk -F: '{print $2}')
c=$(echo $[$a - $b])
if [ $c -eq 0 ];then
    echo 0
else
    echo $c 
fi 

[root@liuwei ~]# pkill zabbix
[root@liuwei ~]# zabbix_agentd 
[root@liuwei ~]# tail -2 /usr/local/etc/zabbix_agentd.conf
UserParameter=check_slave_mysql,/scripts/check_status.sh 
UserParameter=check_IO_SQL_mysql,/scripts/check_IO_SQL.sh

添加监控项:

mysql主库链接时起始的binlog偏移量 如何查询_mysql_03

添加触发器:

mysql主库链接时起始的binlog偏移量 如何查询_传统主从_04

将脚本中正常的改为1

mysql主库链接时起始的binlog偏移量 如何查询_mysql_05

将脚本修改正常

mysql主库链接时起始的binlog偏移量 如何查询_主从_06

状态:

//脚本
[root@liuwei ~]# cat /scripts/check_IO_SQL.sh 
#!/bin/bash
d=$(mysql -uroot -plw1234 -e 'show slave status\G' 2>/dev/null |grep 'Slave_IO_Running'|awk -F: '{print $2}')
e=$(mysql -uroot -plw1234 -e 'show slave status\G' 2>/dev/null |grep 'Slave_SQL_Running:'|awk -F: '{print $2}')
g=$(echo $d)
f=$(echo $e)
if [ $g == 'Yes' -a $f == 'Yes' ];then
    echo 0
else 
    echo 1
fi
[root@liuwei ~]# pkill zabbix
[root@liuwei ~]# zabbix_agentd
[root@liuwei ~]# tail -1 /usr/local/etc/zabbix_agentd.conf
UserParameter=check_IO_SQL_mysql,/scripts/check_IO_SQL.sh

添加监控项:

mysql主库链接时起始的binlog偏移量 如何查询_数据库_07


添加触发器:

mysql主库链接时起始的binlog偏移量 如何查询_GTID_08


关闭主从复制

stop slave

mysql主库链接时起始的binlog偏移量 如何查询_传统主从_09

开启主从复制

start slave

mysql主库链接时起始的binlog偏移量 如何查询_GTID_10

4.GTID主从的配置

4.1原理:

从服务器连接到主服务器之后,把自己执行过的GTID(Executed_Gtid_Set)<SQL线程> 、获取到的GTID(Retrieved_Gtid_Set)<IO线程>发给主服务器,主服务器把从服务器缺少的GTID及对应的transactions发过去补全即可。当主服务器挂掉的时候,找出同步最成功的那台从服务器,直接把它提升为主即可。如果硬要指定某一台不是最新的从服务器提升为主, 先change到同步最成功的那台从服务器, 等把GTID全部补全了,就可以把它提升为主了。

4.2作用:

GTID 的使用不单单是用单独的标识符替换旧的二进制日志文件和位置,它也采用了新的复制协议。旧的协议往往简单直接,即:首先从服务器上在一个特定的偏移量位置连接到主服务器上一个给定的二进制日志文件,然后主服务器再从给定的连接点开始发送所有的事件。
新协议稍有不同:支持以全局统一事务 ID (GTID) 为基础的复制。当在主库上提交事务或者被从库应用时,可以定位和追踪每一个事务。GTID 复制是全部以事务为基础,使得检查主从一致性变得非常简单。如果所有主库上提交的事务也同样提交到从库上,一致性就得到了保证。
GTID 相关操作:默认情况下将一个事务记录进二进制文件时,首先记录它的 GTID,而且 GTID 和事务相关信息一并要发送给从服务器,由从服务器在本地应用认证,但是绝对不会改变原来的事务 ID 号。因此在 GTID 的架构上就算有了N层架构,复制是N级架构,事务 ID 依然不会改变,有效的保证了数据的完整和安全性。

4.3GTID的搭建:

主数据库:192.168.110.21
从数据库:192.168.110.20

4.3.1安装mysql

4.3.2配置GTID参数

在传统主从的基础之下进行配置:

//主数据库
server-id = 1
 11 log-bin = mysql-bin
 12 gtid-mode = ON
 13 enforce-gtid-consistency = true


//从数据库
[root@liuwei ~]# vim /etc/my.cnf
server-id=2
relay-log=mysql-relay-bin
gtid-mode = ON
enforce-gtid-consistency =true

4.3.3授权同步账号

mysql> grant replication slave on *.* to 'lw'@'192.168.110.20' identified by 'lw1234';
Query OK, 0 rows affected, 1 warning (0.03 sec)
mysql> flush privileges;  //刷新数据库
Query OK, 0 rows affected (0.02 sec)

4.3.4配置

//主数据库
[root@lw ~]# mysql
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 12
Server version: 5.7.23-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> set global enforce_gtid_consistency=on;
Query OK, 0 rows affected (0.00 sec)

mysql> show variables like '%enforce_gtid_consistency%';
+--------------------------+-------+
| Variable_name            | Value |
+--------------------------+-------+
| enforce_gtid_consistency | ON    |
+--------------------------+-------+
1 row in set (0.01 sec)

mysql> set global gtid_mode=on_permissive;
Query OK, 0 rows affected (0.12 sec)

mysql> set global gtid_mode=on;
Query OK, 0 rows affected (0.06 sec)

mysql> show variables like '%gtid_mode%';
+---------------+-------+
| Variable_name | Value |
+---------------+-------+
| gtid_mode     | ON    |
+---------------+-------+
1 row in set (0.01 sec)

mysql> show variables like '%gtid%';
+----------------------------------+-----------+
| Variable_name                    | Value     |
+----------------------------------+-----------+
| binlog_gtid_simple_recovery      | ON        |
| enforce_gtid_consistency         | ON        |
| gtid_executed_compression_period | 1000      |
| gtid_mode                        | ON        |
| gtid_next                        | AUTOMATIC |
| gtid_owned                       |           |
| gtid_purged                      |           |
| session_track_gtids              | OFF       |
+----------------------------------+-----------+
8 rows in set (0.01 sec)

mysql> show variables like '%gtid%';
+----------------------------------+-----------+
| Variable_name                    | Value     |
+----------------------------------+-----------+
| binlog_gtid_simple_recovery      | ON        |
| enforce_gtid_consistency         | ON        |
| gtid_executed_compression_period | 1000      |
| gtid_mode                        | ON        |
| gtid_next                        | AUTOMATIC |
| gtid_owned                       |           |
| gtid_purged                      |           |
| session_track_gtids              | OFF       |
+----------------------------------+-----------+
8 rows in set (0.00 sec)

//从数据库
[root@liuwei scripts]# systemctl restart mysqld
[root@liuwei scripts]# mysql -uroot -plw1234
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.25 MySQL Community Server (GPL)

Copyright (c) 2000, 2019, 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 variables like '%enforce_gtid_consistency%';
+--------------------------+-------+
| Variable_name            | Value |
+--------------------------+-------+
| enforce_gtid_consistency | ON    |
+--------------------------+-------+
1 row in set (0.01 sec)

mysql> set global enforce_gtid_consistency=on;
Query OK, 0 rows affected (0.00 sec)

mysql> show variables like '%enforce_gtid_consistency%';
+--------------------------+-------+
| Variable_name            | Value |
+--------------------------+-------+
| enforce_gtid_consistency | ON    |
+--------------------------+-------+
1 row in set (0.01 sec)

mysql> set global gtid_mode=on_permissive;
Query OK, 0 rows affected (0.00 sec)

mysql>  set global gtid_mode=on;
Query OK, 0 rows affected (0.00 sec)

mysql>  show variables like '%gtid_mode%';
+---------------+-------+
| Variable_name | Value |
+---------------+-------+
| gtid_mode     | ON    |
+---------------+-------+
1 row in set (0.00 sec)

mysql> show global status like '%ongoing_anonymous%';
+-------------------------------------+-------+
| Variable_name                       | Value |
+-------------------------------------+-------+
| Ongoing_anonymous_transaction_count | 0     |
+-------------------------------------+-------+
1 row in set (0.05 sec)

mysql> show variables like '%gtid%';
+----------------------------------+--------------------------------------------+
| Variable_name                    | Value                                      |
+----------------------------------+--------------------------------------------+
| binlog_gtid_simple_recovery      | ON                                         |
| enforce_gtid_consistency         | ON                                         |
| gtid_executed_compression_period | 1000                                       |
| gtid_mode                        | ON                                         |
| gtid_next                        | AUTOMATIC                                  |
| gtid_owned                       |                                            |
| gtid_purged                      | 87b6b4ac-34e6-11e9-b93c-000c29e97649:1-228 |
| session_track_gtids              | OFF                                        |
+----------------------------------+--------------------------------------------+
8 rows in set (0.01 sec)

mysql>  stop slave;
Query OK, 0 rows affected (0.01 sec)

mysql> change master to  master_auto_position=1;
Query OK, 0 rows affected (0.03 sec)

mysql> start slave;
Query OK, 0 rows affected (0.01 sec)

mysql> show slave status\G
*************************** 1. row ***************************
               Slave_IO_State: Waiting for master to send event
                  Master_Host: 192.168.110.21
                  Master_User: lw
                  Master_Port: 3306
                Connect_Retry: 60
              Master_Log_File: mysql-bin.000018
          Read_Master_Log_Pos: 4965
               Relay_Log_File: mysql-relay-bin.000004
                Relay_Log_Pos: 5178
        Relay_Master_Log_File: mysql-bin.000018
             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: 4965
              Relay_Log_Space: 37775
              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: 87b6b4ac-34e6-11e9-b93c-000c29e97649
             Master_Info_File: /var/lib/mysql/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: 87b6b4ac-34e6-11e9-b93c-000c29e97649:236-344
            Executed_Gtid_Set: 87b6b4ac-34e6-11e9-b93c-000c29e97649:1-344
                Auto_Position: 1
         Replicate_Rewrite_DB: 
                 Channel_Name: 
           Master_TLS_Version: 
1 row in set (0.00 sec)

4.4验证

//主数据库
mysql> use liuwei;
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -A

Database changed
mysql> insert stuent values (8,'lw',19);
ERROR 1146 (42S02): Table 'liuwei.stuent' doesn't exist
mysql> insert student values (8,'lw',19);
Query OK, 1 row affected (0.01 sec)

mysql> select * from student;
+----+------------+------+
| id | name       | age  |
+----+------------+------+
|  1 | tom        |   18 |
|  2 | liumingjun |   22 |
|  3 | liuwei     |   21 |
|  4 | lizhao     |   19 |
|  5 | chengshuai |    3 |
|  6 | pengye     |   18 |
|  7 | chengshuai |    3 |
|  8 | lw         |   19 |
+----+------------+------+
8 rows in set (0.00 sec)

//从数据库
mysql> use liuwei;
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -A

Database changed
mysql> select * from student;
+----+------------+------+
| id | name       | age  |
+----+------------+------+
|  1 | tom        |   18 |
|  2 | liumingjun |   22 |
|  3 | liuwei     |   21 |
|  4 | lizhao     |   19 |
|  5 | chengshuai |    3 |
|  6 | pengye     |   18 |
|  7 | chengshuai |    3 |
|  8 | lw         |   19 |
+----+------------+------+
8 rows in set (0.00 sec)

验证成功,主从数据库数据一致。

5.传统主从与GTID主从区别

1.不用手动定位pos和binlog的位置,添加参数master_auto_position=1即可
2.多线程并发复制,Slave-parallel-workers=0,1,2(禁用,单线程,两个线程)
master数据库有改动,比如DML,存入bin-log中,备份到主数据库中,网络I/O才去master给slave去取数据
所以经典模式下一般手动设置给slave备份,因为一定要保证主备数据一样,自动容易出问题。