mysql主从

1.主从作用

  • 实时灾备,用于故障切换
  • 读写分离,提供查询服务
  • 备份,避免影响业务一主一从
1.1主从形式
  • 主主复制
  • 一主多从—扩展系统读取的性能,因为读是在从库读取的
  • 多主一从—5.7开始支持
  • 联级复制

2.主从复制原理

mysql主从

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

3.主从复制配置

3.1主从复制配置步骤:
  • 1.确保从数据库与主数据库里的数据一样
  • 2.在主数据库里创建一个同步账号授权给从数据库使用
  • 3.配置主数据库(修改配置文件)
  • 4.配置从数据库(修改配置文件)
3.2传统主从复制
3.3配置

服务端(主库)

//为了避免多次输入用户密码,我们把用户密码写入配置文件
[[email protected] ~]# cat .my.cnf 
[client]
user = root
password = 123456

[mysqldump]
user = root
password = 123456

//先查看主库有哪些库
[[email protected] ~]# mysql  -e 'show databases;'
+--------------------+
| Database           |
+--------------------+
| information_schema |
| mysql              |
| performance_schema |
| sys                |
| zabbix             |
| zhangzhengguang    |
+--------------------+
[[email protected] ~]# mysql  -e 'select * from zhangzhengguang.student;'
+------+-----------+------+
| id   | name      | age  |
+------+-----------+------+
|    1 | tom       |   20 |
|    2 | jerry     |   23 |
|    3 | liwenfeng |   19 |
|    4 | zzg       |   22 |
|    5 | fupian    |   12 |
|    6 | gsh       |   33 |
+------+-----------+------+

//全备主库
//全备主库时需要另开一个终端,给数据库加上写免在备份期间有其他人在写入导致数据不一致
[[email protected] ~]# mysql 
mysql> flush tables with read lock;   //新开的终端输入
Query OK, 0 rows affected (0.01 sec)  
[[email protected] ~]# mysqldump --all-databases >all-20190227.sql
[[email protected] ~]# ls
all-20190227.sql  anaconda-ks.cfg

//备份主库并将备份文件传送到从库
[[email protected] ~]# scp all-20190227.sql [email protected]:/root/

//在从库(客户端)查看,验证
[[email protected] ~]# ls
all-20190227.sql  anaconda-ks.cfg 
传送完成

//解除主库的锁表状态,直接退出交互式界面即可
mysql> quit    //新开的终端

// 在主数据库里创建一个同步账号授权给从数据库使用
[[email protected] ~]# mysql
mysql> CREATE USER 'zhang'@'192.168.220.50' IDENTIFIED BY 'zhang123';
Query OK, 0 rows affected (0.00 sec)

mysql> grant replication slave on *.* to 'zhang'@'192.168.220.50';
Query OK, 0 rows affected (0.00 sec)

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

mysql> quit
Bye

// 配置主数据库
[[email protected] ~]# vim /etc/my.cnf
[[email protected] ~]# cat /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   //启用binlog日志
server-id=1    //数据库服务器唯一标识符,主库的server-id值必须比从库的小
加上最后两行

//重启服务
[[email protected] ~]# systemctl restart mysqld

//查看主库的状态

mysql> show master status;
+------------------+----------+--------------+------------------+-------------------+
| File             | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set |
+------------------+----------+--------------+------------------+-------------------+
| mysql-bin.000001 |     5029 |              |                  |                   |
+------------------+----------+--------------+------------------+-------------------+
1 row in set (0.00 sec)

客户端(从库)

//为了避免多次输入用户密码,我们把用户密码写入配置文件
[[email protected] ~]# cat .my.cnf 
[client]
user = root
password = 123456

[mysqldump]
user = root
password = 123456

//在从库上恢复主库的备份并查看从库有哪些库,确保与主库一致
[[email protected] ~]# mysql <all-20190227.sql 
[[email protected] ~]# mysql
mysql> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| mysql              |
| performance_schema |
| sys                |
| zabbix             |
| zhangzhengguang    |
+--------------------+
6 rows in set (0.05 sec)

mysql> select * from zhangzhengguang.student;
+------+-----------+------+
| id   | name      | age  |
+------+-----------+------+
|    1 | tom       |   20 |
|    2 | jerry     |   23 |
|    3 | liwenfeng |   19 |
|    4 | zzg       |   22 |
|    5 | fupian    |   12 |
|    6 | gsh       |   33 |
+------+-----------+------+
6 rows in set (0.00 sec)

//配置从数据库
[[email protected] ~]# vim /etc/my.cnf
[[email protected] ~]# cat /etc/my.cnf
[mysqld]
datadir=/opt/data
basedir = /usr/local/mysql
socket = /tmp/mysql.sock
port = 3306
pid-file = /opt/data/mysql.pid
user = mysql 
skip-name-resolve

relay-log=mysql-relay-bin    //启用中继日志relay-log
server-id=2    //设置从库的唯一标识符,从库的server-id值必须大于主库的该值
加上最后两行

//重新启动服务
[[email protected] ~]# systemctl restart  mysqld

//配置并启动主从复制
mysql> CHANGE MASTER TO
    ->      MASTER_HOST='192.168.220.40',
    ->      MASTER_USER='zhang',
    ->      MASTER_PASSWORD='zhang123',
    ->      MASTER_LOG_FILE='mysql-bin.000001',
    ->      MASTER_LOG_POS=5029;
Query OK, 0 rows affected, 2 warnings (0.15 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.220.40
                  Master_User: zhang
                  Master_Port: 3306
                Connect_Retry: 60
              Master_Log_File: mysql-bin.000002
          Read_Master_Log_Pos: 318460
               Relay_Log_File: mysql-relay-bin.000003
                Relay_Log_Pos: 318673
        Relay_Master_Log_File: mysql-bin.000002
             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: 318460
              Relay_Log_Space: 571718
              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: 2b8e13f0-34ef-11e9-8ec3-000c29282f4e
             Master_Info_File: /opt/data/master.info
                    SQL_Delay: 0
          SQL_Remaining_Delay: NULL
      Slave_SQL_Running_State: Slave has read all relay log; waiting for more u

 测试验证
 //主库
mysql> insert student value(10,'zz',50);
Query OK, 1 row affected (0.01 sec)

mysql> select * from student;
+------+-----------+------+
| id   | name      | age  |
+------+-----------+------+
|    1 | tom       |   20 |
|    2 | jerry     |   23 |
|    3 | liwenfeng |   19 |
|    4 | zzg       |   22 |
|    5 | fupian    |   12 |
|    6 | gsh       |   33 |
|   10 | zz        |   50 |
+------+-----------+------+
7 rows in set (0.00 sec)

//从库
mysql> select * from student;
+------+-----------+------+
| id   | name      | age  |
+------+-----------+------+
|    1 | tom       |   20 |
|    2 | jerry     |   23 |
|    3 | liwenfeng |   19 |
|    4 | zzg       |   22 |
|    5 | fupian    |   12 |
|    6 | gsh       |   33 |
|   10 | zz        |   50 |
+------+-----------+------+
7 rows in set (0.00 sec)

同步成功


4.zabbix监控

4.1状态监控

在客户端

//写控制脚本
[[email protected] scripts]# cat mysql1.sh 
#/bin/bash

a=$(mysql  -e 'show slave status\G' |grep Slave_IO_Running |awk  '{print $NF}')
2> /dev/null
b=$(mysql  -e 'show slave status\G' |grep -E 'Slave_SQL_Running'|grep -v Slave_SQL_Running_State |awk  '{print $NF}') 2> /dev/null
if [ $a == Yes -a $b == Yes ];then
	echo 0
else
	echo 1
fi
[[email protected] scripts]# chmod +x mysql1.sh 

//配置agentd配置文件
[[email protected] scripts]# tail -1 /usr/local/etc/zabbix_agentd.conf
UserParameter=check_ms_mysql,/scripts/mysql1.sh

//重新启动服务
[[email protected] scripts]# pkill zabbix
[[email protected] scripts]# zabbix_agentd

在服务端手动检验

[[email protected] ~]# zabbix_get -s 192.168.220.50 -k check_ms_mysql
0

添加监控项,触发器
mysql主从
在客户端stop slave
mysql主从

4.2延迟监控

在客户端

//写控制脚本
[[email protected] scripts]# cat mysql2.sh 
#/bin/bash

c=$(mysql -e 'show slave status\G' |grep  Read_Master_Log_Pos |awk  '{print $NF}') 2>/dev/null
d=$(mysql -e 'show slave status\G' |grep Exec_Master_Log_Pos |awk  '{print $NF}') 2>/dev/null


if [ $c -eq $d ];then
	echo '0'
else
	echo '$c-$d'
fi

[[email protected] scripts]# chmod +x mysql2.sh 

//配置agentd配置文件
[[email protected] scripts]# tail -1 /usr/local/etc/zabbix_agentd.conf
UserParameter=check_status_mysql,/scripts/mysql2.sh

//重新启动服务
[[email protected] scripts]# pkill zabbix
[[email protected] scripts]# zabbix_agentd

在服务端手动检验

[[email protected] ~]# zabbix_get -s 192.168.220.50 -k check_status_mysql
0

添加监控项,触发器
mysql主从

5.GTID主从同步

5.1.GTID介绍
  • 基于GTID的复制是从Mysql5.6开始支持的一种新的复制方式,此方式与传统基于日志的方式存在很大的差异,在原来的基于日志的复制中,从服务器连接到主服务器并告诉主服务器要从哪个二进制日志的偏移量开始执行增量同步,这时我们如果指定的日志偏移量不对,这与可能造成主从数据的不一致,而基于GTID的复制会避免。
  • 在基于GTID的复制中,首先从服务器会告诉主服务器已经在从服务器执行完了哪些事务的GTID值,然后主库会有把所有没有在从库上执行的事务,发送到从库上进行执行,并且使用GTID的复制可以保证同一个事务只在指定的从库上执行一次,这样可以避免由于偏移量的问题造成数据不一致。
  • 什么是GTID,也就是全局事务ID,其保证为每一个在主上提交的事务在复制集群中可以生成一个唯一的ID。
  • 一个GITD由两部分组成的,分别是source_id 和transaction_id,GTID=source_id:transaction_id,其中source_id就是执行事务的主库的server-uuid值,server-uuid值是在mysql服务首次启动生成的,保存在数据库的数据目录中,在数据目录中有一个auto.conf文件,这个文件保存了server-uuid值(唯一的)。而事务ID则是从1开始自增的序列,表示这个事务是在主库上执行的第几个事务,Mysql会保证这个事务和GTID是一比一的关系。
5.2.优缺点

优点:

  • A:很方便的进行故障转移,因为GTID是全局唯一的标识符,所以就很简单知道哪些事务在从服务器没有执行,在多个从服务器也没必要进行多个日志偏移量配置了.

  • B:从库和主库的数据一致性。

缺点

  • A:故障处理比日志处理复杂。
  • B:执行语句的一些限制。
5.3配置

在传统同步的基础上


//在主和从库配置文件分别加入以下两行代码,重新服务
从库
[[email protected] ~]# cat /etc/my.cnf
[mysqld]
gtid-mode=on
enforce-gtid-consistency=true
[[email protected] ~]# service mysqld restart
主库
[[email protected] ~]# cat /etc/my.cnf
gtid-mode=on
enforce-gtid-consistency=true
[[email protected] ~]# service mysqld restart
Shutting down MySQL.... SUCCESS! 
Starting MySQL. SUCCESS! 


//在主库上
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.00 sec)

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


mysql> set global gtid_mode=on;
Query OK, 0 rows affected (0.01 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 '%enforce_gtid_consistency%';
+--------------------------+-------+
| Variable_name            | Value |
+--------------------------+-------+
| enforce_gtid_consistency | ON    |
+--------------------------+-------+
1 row in set (0.00 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.00 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.00 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                      | 2b8e13f0-34ef-11e9-8ec3-000c29282f4e:1-558 |
| session_track_gtids              | OFF                                        |
+----------------------------------+--------------------------------------------+
8 rows in set (0.00 sec)

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

mysql> change master to  master_auto_position=1;
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.220.40
                Master_User: zhang
                Master_Port: 3306
              Connect_Retry: 60
            Master_Log_File: mysql-bin.000006
        Read_Master_Log_Pos: 84719
             Relay_Log_File: mysql-relay-bin.000004
              Relay_Log_Pos: 84932
      Relay_Master_Log_File: mysql-bin.000006
           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: 84719
            Relay_Log_Space: 85226
            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: 2b8e13f0-34ef-11e9-8ec3-000c29282f4e
           Master_Info_File: /opt/data/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: 2b8e13f0-34ef-11e9-8ec3-000c29282f4e:559-827
          Executed_Gtid_Set: 2b8e13f0-34ef-11e9-8ec3-000c29282f4e:1-827
              Auto_Position: 1
       Replicate_Rewrite_DB: 
               Channel_Name: 
         Master_TLS_Version: 
1 row in set (0.00 sec)

验证:
在主库插入(10,‘zhangxian’,22)到zhangzhengguang.student表中

mysql> insert zhangzhengguang.student value (10,'zhangxian',22);
Query OK, 1 row affected (0.00 sec)

从库查看

mysql> select * from zhangzhengguang.student;
+------+-----------+------+
| id   | name      | age  |
+------+-----------+------+
|    1 | tom       |   20 |
|    2 | jerry     |   23 |
|    3 | liwenfeng |   19 |
|    4 | zzg       |   22 |
|    5 | fupian    |   12 |
|    6 | gsh       |   33 |
|  100 | jjyy      |   10 |
|   10 | zhangxian |   22 |
+------+-----------+------+
8 rows in set (0.01 sec)
mysql> show slave status\G
*************************** 1. row ***************************
             Slave_IO_State: Waiting for master to send event
                Master_Host: 192.168.220.40
                Master_User: zhang
                Master_Port: 3306
              Connect_Retry: 60
            Master_Log_File: mysql-bin.000006
        Read_Master_Log_Pos: 234781
             Relay_Log_File: mysql-relay-bin.000004
              Relay_Log_Pos: 234994
      Relay_Master_Log_File: mysql-bin.000006
           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: 234781
            Relay_Log_Space: 235288
            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: 2b8e13f0-34ef-11e9-8ec3-000c29282f4e
           Master_Info_File: /opt/data/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: 2b8e13f0-34ef-11e9-8ec3-000c29282f4e:559-1280
          Executed_Gtid_Set: 2b8e13f0-34ef-11e9-8ec3-000c29282f4e:1-1280
              Auto_Position: 1
       Replicate_Rewrite_DB: 
               Channel_Name: 
         Master_TLS_Version: 
1 row in set (0.01 sec)




验证成功