传统mysql主从+GTID主从+利用zabbix来监控

1. 传统主从简介

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

想几个问题:

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

1.1 主从作用

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

1.2 主从形式

传统mysql主从+GTID主从+利用zabbix来监控

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

2. 主从复制原理(重点)

传统mysql主从+GTID主从+利用zabbix来监控

主从复制步骤:

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

3. 主从复制配置

主从复制配置步骤:

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

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

环境:
主数据库:

  • IP:192.168.157.59
  • 系统版本:Redhat7
  • 有无数据:有

从数据库:

  • IP:192.168.157.19
  • 系统版本:Redhat7
  • 有无数据:无

3.1 mysql安装(见前文:lamp架构搭建)

过程略,之前的文章已经多次写过了

3.2 mysql主从配置

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

//全备主数据库并还原到从数据库中
//全备主数据库时,必须重新打开一个终端,给数据库加上读锁,避免在备份期间有其他人在写入导致数据不一致

mysql> FLUSH TABLES WITH READ LOCK;
Query OK, 0 rows affected (0.12 sec)
//此锁表的终端必须在备份完成以后才能关闭!!!

//全备主数据库,并还原到从数据库中

[[email protected] ~]# mysqldump -uroot -plizhaoq123!--all-databases > /root/all.sql
mysqldump: [Warning] Using a password on the command line interface can be insecure.
[[email protected] ~]# ls
all.sql
[[email protected] ~]# scp all.sql ro[email protected]:/root/master/
[email protected]'s password: 
all.sql                                                   100% 4988KB  14.2MB/s   00:00  

[[email protected] ~]# ls master/
all.sql

//在从数据库上恢复备份,并查看是否与主数据库一致

[[email protected] master]# mysql -uroot -plizhao123! < all.sql 
mysql: [Warning] Using a password on the command line interface can be insecure.

[[email protected] master]# mysql -e 'show databases;'
+--------------------+
| Database           |
+--------------------+
| information_schema |
| lizhao             |
| mysql              |
| performance_schema |
| sys                |
| zabbix             |
+--------------------+


[[email protected] master]# mysql -e 'select * from lizhao.student;'
+----+-------+------+
| id | name  | age  |
+----+-------+------+
|  1 | tom   |   20 |
|  2 | jerry |   23 |
|  3 | messi |   22 |
|  4 | leo   |   30 |
+----+-------+------+

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

[[email protected] ~]# mysql
mysql> grant replication slave on *.* to 'repl'@'192.168.157.19' identified by 'repl123';
Query OK, 0 rows affected, 1 warning (0.00 sec)

mysql> flush privileges;
Query OK, 0 rows affected (0.00 sec)

//在客户端检验:
[roo[email protected] ~]# mysql -urepl -prepl123 -h192.168.157.59(主数据库端IP)
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 8
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> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
+--------------------+
1 row in set (0.01 sec)

3.2.3 配置主数据库

[[email protected] ~]# vim /etc/my.cnf

添加以下内容:
server-id = 1		//主库的server-id值必须比从库的小
log-bin = mysql_log		//启用binlog日志

//重启服务,并查看主库的状态
[[email protected] ~]# service mysqld restart 
Shutting down MySQL.. SUCCESS! 
Starting MySQL.. SUCCESS! 


[[email protected] ~]# mysql -e 'show master status;'
+------------------+----------+--------------+------------------+-------------------+
| File             | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set |
+------------------+----------+--------------+------------------+-------------------+
| mysql_log.000011 |      154 |              |                  |                   |
+------------------+----------+--------------+------------------+-------------------+

3.2.4 配置从数据库

[[email protected] ~]# vim /etc/my.cnf

添加以下内容:
server-id = 3		//从库的server-id必须比主库的大!
relay-log = mysql_relay_log		//启用中继日志relay-log

//重启服务
[[email protected] ~]# service mysqld restart 
Shutting down MySQL.. SUCCESS! 
Starting MySQL. SUCCESS! 

//配置并启动主从复制

mysql> CHANGE MASTER TO
-> MASTER_HOST='192.168.157.59',
-> MASTER_USER='repl',
-> MASTER_PASSWORD='repl123',
-> MASTER_LOG_FILE='mysql_log.000011',(查看主库的状态得知:show master status;)
-> MASTER_LOG_POS=154;
//除了最后的数字不需要引号,其他的都必须要引号

//查看从服务器的状态

mysql> show slave status\G
*************************** 1. row ***************************
               Slave_IO_State: Waiting for master to send event
                  Master_Host: 192.168.157.59
                  Master_User: repl
                  Master_Port: 3306
                Connect_Retry: 60		//如果连接失败,则每隔60s重连一次
              Master_Log_File: mysql_log.000011
          Read_Master_Log_Pos: 154		//在主库上:show master status,可验证
               Relay_Log_File: mysql_relay_log.000002
                Relay_Log_Pos: 320
        Relay_Master_Log_File: mysql_log.000011
             Slave_IO_Running: Yes		//此处必须为Yes
            Slave_SQL_Running: Yes		//此处必须为Yes
          Exec_Master_Log_Pos: 154

(正常情况下,Read_Master_Log_Pos的值和Exec_Master_Log_Pos应该一致,如果不一致,则说明网络有延迟。)
(一个是读取到数据的位置,一个是执行数据的位置。)

3.2.5测试验证

//在主数据库的lizhao库的student表中插入数据:

mysql> select * from student;
+----+-------+------+
| id | name  | age  |
+----+-------+------+
|  1 | tom   |   20 |
|  2 | jerry |   23 |
|  3 | messi |   22 |
|  4 | leo   |   30 |
+----+-------+------+
4 rows in set (0.00 sec)

mysql> insert into student values(5,'james',34),(6,'lebron',35);
Query OK, 2 rows affected (0.01 sec)
Records: 2  Duplicates: 0  Warnings: 0

mysql> select * from student;
+----+--------+------+
| id | name   | age  |
+----+--------+------+
|  1 | tom    |   20 |
|  2 | jerry  |   23 |
|  3 | messi  |   22 |
|  4 | leo    |   30 |
|  5 | james  |   34 |
|  6 | lebron |   35 |
+----+--------+------+
6 rows in set (0.01 sec)

//在从数据库中查看数据是否同步

mysql> use lizhao;
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 tables;
+------------------+
| Tables_in_lizhao |
+------------------+
| DrinkWater       |
| soccer           |
| student          |
+------------------+
3 rows in set (0.00 sec)

mysql> select * from student;
+----+--------+------+
| id | name   | age  |
+----+--------+------+
|  1 | tom    |   20 |
|  2 | jerry  |   23 |
|  3 | messi  |   22 |
|  4 | leo    |   30 |
|  5 | james  |   34 |
|  6 | lebron |   35 |
+----+--------+------+
6 rows in set (0.00 sec)

4. 利用zabbix监控mysql主从是否存在故障或延迟

//首先编辑agent端(slave端)的zabbix配置文件,手动添加key,并重启服务

[[email protected] scripts]# vim /usr/local/etc/zabbix_agentd.conf
UserParameter=mysql_replication,/scripts/ckmysql.sh	//添加此内容用来监控是否存在故障
UserParameter=mysql_wait,/scripts/mysqlwait.sh	//添加此内容用来监控是否有延迟
[[email protected] ~]# pkill zabbix
[[email protected] ~]# zabbix_agentd 

//写两个脚本,分别用来测试故障和延迟,并赋予执行权限,修改属主属组为zabbix

//监控是否故障的脚本:ckmysql.sh

[[email protected] scripts]# cat ckmysql.sh 
#! /bin/bash

a=$(mysql -uroot -plizhao123! -e 'show slave status\G' 2>/dev/null| grep 'Slave_IO_Running:' | awk -F: '{print $2}') 
b=$(mysql -uroot -plizhao123! -e 'show slave status\G' 2>/dev/null|grep 'Slave_SQL_Running:'| awk -F: '{print $2}')
c=$(echo $a,$b)
echo $c|grep 'Yes'|wc -w
//如果统计的Yes数量为2,则表示正常

//监控是否有延迟的脚本:mysqlwait.sh

[[email protected] scripts]# cat mysqlwait.sh 
#! /bin/bash

a=$(mysql -uroot -plizhao123! -e 'show slave status\G' 2>/dev/null |grep 'Read_Master_Log_Pos:' |awk -F: '{print $2}')

b=$(mysql -uroot -plizhao123! -e 'show slave status\G' 2>/dev/null |grep 'Exec_Master_Log_Pos:' |awk -F: '{print $2}')

c=$[ $a - $b ]
echo $c
//如果为0,则表示没有延迟,如果大于0,则表示有延迟

[[email protected] scripts]# ll
-rwxr-xr-x 1 zabbix zabbix  289 2月  28 02:49 ckmysql.sh
-rwxr-xr-x 1 zabbix zabbix  277 2月  28 03:44 mysqlwait.sh

//在zabbix-server端,手动检验是否能够从agent端获取到值

[[email protected] ~]# zabbix_get -s 192.168.157.19 -k mysql_replication
2
[[email protected] ~]# zabbix_get -s 192.168.157.19 -k mysql_wait
0

//在zabbix界面添加监控项和触发器

传统mysql主从+GTID主从+利用zabbix来监控传统mysql主从+GTID主从+利用zabbix来监控传统mysql主从+GTID主从+利用zabbix来监控传统mysql主从+GTID主从+利用zabbix来监控
//基于昨天创建的邮件告警,现在在slave端关闭slave,检验是否会有告警邮件

[[email protected] scripts]# mysql -e 'stop slave;'

传统mysql主从+GTID主从+利用zabbix来监控传统mysql主从+GTID主从+利用zabbix来监控附件内容:

Problem started at 03:37:49 on 2019.02.28
Problem name: mysql主从发生故障
Host: web服务器-192.168.157.19
Severity: High

Original problem ID: 119

//测试延迟,我们手动修改slave端的mysqlwait.sh文件,让它不输出0,查看是否会有邮件告警

传统mysql主从+GTID主从+利用zabbix来监控传统mysql主从+GTID主从+利用zabbix来监控附件内容:

Problem started at 03:42:50 on 2019.02.28
Problem name: mysql存在延迟
Host: web服务器-192.168.157.19
Severity: Not classified

Original problem ID: 120

5. GTID主从备份(更好)

5.1 server端配置

[[email protected] ~]# vim /etc/my.cnf
[[email protected] ~]# tail -2 /etc/my.cnf
gtid_mode=ON		//添加此内容
enforce-gtid-consistency=true	//添加此内容

[[email protected] ~]# service mysqld restart 
Shutting down MySQL.. SUCCESS! 
Starting MySQL.. SUCCESS! 

//position并不唯一,一直在变更
[[email protected] ~]# mysql -e 'show master status;'
+------------------+----------+--------------+------------------+-------------------+
| File             | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set |
+------------------+----------+--------------+------------------+-------------------+
| mysql_log.000016 |      154 |              |                  |                   |
+------------------+----------+--------------+------------------+-------------------+

//查看GTID

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

		
mysql> show global variables like 'gtid%';
+----------------------------------+----------------------------------------+
| Variable_name                    | Value                                  |
+----------------------------------+----------------------------------------+
| gtid_executed                    | 9cf9ccfb-34e1-11e9-839d-000c29758baa:1 |
| gtid_executed_compression_period | 1000                                   |
| gtid_mode                        | ON                                     |
| gtid_owned                       |                                        |
| gtid_purged                      |                                        |
+----------------------------------+----------------------------------------+
5 rows in set (0.00 sec)

5.2 slave端配置

[[email protected] ~]# vim /etc/my.cnf
[[email protected] ~]# tail -2 /etc/my.cnf
gtid_mode=ON		//添加此内容
ecforce-gtid-consistency=true	//添加此内容

[[email protected] ~]# vim /etc/my.cnf
[[email protected] ~]# service mysqld restart 
Shutting down MySQL.. SUCCESS! 
Starting MySQL.. SUCCESS! 


mysql> CHANGE MASTER TO
-> MASTER_HOST='192.168.157.59',
-> MASTER_USER='repl',
-> MASTER_PASSWORD='repl123',
-> MASTER_LOG_FILE='mysql_log.000016',
-> MASTER_LOG_POS=154;
Query OK, 0 rows affected, 2 warnings (0.03 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.157.59
                  Master_User: repl
                  Master_Port: 3306
                Connect_Retry: 60
              Master_Log_File: mysql_log.000016
          Read_Master_Log_Pos: 154
               Relay_Log_File: mysql_relay_log.000002
                Relay_Log_Pos: 320
        Relay_Master_Log_File: mysql_log.000016
             Slave_IO_Running: Yes
            Slave_SQL_Running: Yes

5.3 测试GTID主从

//在server端的lizhao数据库student表中添加一些内容

mysql> select * from student;
+----+--------+------+
| id | name   | age  |
+----+--------+------+
|  1 | tom    |   20 |
|  2 | jerry  |   23 |
|  3 | messi  |   22 |
|  4 | leo    |   30 |
|  5 | james  |   34 |
|  6 | lebron |   35 |
+----+--------+------+
6 rows in set (0.09 sec)


//删除student表中的lebron用户;
mysql> delete from student where id = 6;	
Query OK, 1 row affected (0.01 sec)

mysql> select * from student;
+----+-------+------+
| id | name  | age  |
+----+-------+------+
|  1 | tom   |   20 |
|  2 | jerry |   23 |
|  3 | messi |   22 |
|  4 | leo   |   30 |
|  5 | james |   34 |
+----+-------+------+
5 rows in set (0.00 sec)

//在slave端查看

mysql> show tables;
+------------------+
| Tables_in_lizhao |
+------------------+
| DrinkWater       |
| soccer           |
| student          |
+------------------+
3 rows in set (0.00 sec)


mysql> select * from student;
+----+-------+------+
| id | name  | age  |
+----+-------+------+
|  1 | tom   |   20 |
|  2 | jerry |   23 |
|  3 | messi |   22 |
|  4 | leo   |   30 |
|  5 | james |   34 |
+----+-------+------+
5 rows in set (0.00 sec)

6. GTID主从与传统主从的区别

传统主从的缺点:

  • 传统一主多从的模型中当master down掉后,我们需要将所有的slave同步目的地从以前的master改成现在master,而且bin-log的序号和偏移量也要去查看,这是十分不方便和耗时的。

//GTID主从的优点:

  • 全局事务标识符(GTID)是创建的唯一标识符,并与在源(主)服务器上提交的每个事务相关联。此标识符不但是唯一的,而且在给定复制设置中的所有服务器上都是唯一的。

  • GTID的特性使得mysql的主从复制变得更加简单,以及数据库一致性更可靠。一个GTID在一个服务器上只执行一次,避免重复执行导致数据混乱或者主从不一致。

  • GTID的生成受gtid_next控制。 在Master上,gtid_next是默认的AUTOMATIC,即在每次事务提交时自动生成新的GTID。它从当前已执行的GTID集合(即gtid_executed)中,找一个大于0的未使用的最小值作为下个事务GTID。同时在binlog的实际的更新事务事件前面插入一条set gtid_next事件。