mysql主从

mysql主从

1.主从简介

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

想几个问题:

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

1.1 主从作用

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

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

2. 主从复制原理

mysql主从主从复制步骤:

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

主从复制配置步骤:

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

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

环境说明:

数据库角色 IP 应用与系统版本 有无数据}
主数据库 192.168.233.10 centos7/redhat7
mysql-5.7
有数据
从数据库 192.168.233.20 centos7/redhat7
mysql-5.7
无数据

3.1 mysql安装

3.2 mysql主从配置

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

为确保从数据库与主数据库里的数据一样,先全备主数据库并还原到从数据库中
//先查看主库有哪些库

[[email protected] ~]# mysql -uroot -pgaosihan123! -e 'show databases'
mysql: [Warning] Using a password on the command line interface can be insecure.
+--------------------+
| Database           |
+--------------------+
| information_schema |
| gaosihan           |
| mysql              |
| performance_schema |
| sys                |
| zabbix             |
+--------------------+

//再查看从库有哪些库

[[email protected] ~]# mysql -uroot -pgaosihan123! -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.00 sec)
//此锁表的终端必须在备份完成以后才能退出

//备份主库并将备份文件传送到从库

[[email protected] ~]# mysqldump -uroot -pgaosihan123! --all-databases > /opt/all-201902271400.sql
mysqldump: [Warning] Using a password on the command line interface can be insecure.
[[email protected] ~]# ls /opt
all-201902271400.sql  data
[[email protected] ~]# scp /opt/all-201902271400.sql [email protected]:/opt/
The authenticity of host '192.168.233.20 (192.168.233.20)' can't be established.
ECDSA key fingerprint is SHA256:HuPXtXVhKqgn/1twqP26nlDMMtYfiiaR+a5KZuOgTc4.
ECDSA key fingerprint is MD5:39:4a:44:92:87:e8:36:f0:91:1d:90:fa:7b:6b:a6:12.
Are you sure you want to continue connecting (yes/no)? yes
Warning: Permanently added '192.168.233.20' (ECDSA) to the list of known hosts.
[email protected]'s password: 
all-201902271400.sql                                                     100% 5113KB  39.1MB/s   00:00

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

//在从库上恢复主库的备份并查看从库有哪些库,确保与主库一致

[[email protected] opt]# ls
all-201902271400.sql  cc  data

[[email protected] ~]# mysql -uroot -pgaosihan123! < /opt/all-201902271400.sql 
mysql: [Warning] Using a password on the command line interface can be insecure.
[[email protected] ~]# mysql -uroot -pgaosihan123! -e 'show databases;'
mysql: [Warning] Using a password on the command line interface can be insecure.
+--------------------+
| Database           |
+--------------------+
| information_schema |
| gaosihan           |
| mysql              |
| performance_schema |
| sys                |
| zabbix             |
+--------------------+

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

mysql> grant replication slave on *.* to 'repl'@'192.168.233.20' identified by 'repl123';
Query OK, 0 rows affected, 1 warning (0.01 sec)

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

在从数据库里查看
[[email protected] ~]# mysql -urepl -prepl123 -h192.168.233.10
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 9854
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.
可以登录

3.2.3 配置主数据库

[[email protected] ~]# vim /etc/my.cnf
//在[mysqld]这段的后面加上如下内容
[mysqld]
datadir=/var/lib/mysql
socket=/var/lib/mysql/mysql.sock
log-bin=mysql-bin   //启用binlog日志
server-id=3     //数据库服务器唯一标识符,主库的server-id值必须比从库的小

symbolic-links=0

log-error=/var/log/mysqld.log
pid-file=/var/run/mysqld/mysqld.pid

//重启mysql服务

[[email protected] ~]# systemctl restart mysqld
[[email protected] ~]# 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      128                    :::22                                 :::*
LISTEN      0      100                   ::1:25                                 :::*
LISTEN      0      80                     :::3306                               :::*

//查看主库的状态

mysql> show master status;
+------------------+----------+--------------+------------------+-------------------+
| File             | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set |
+------------------+----------+--------------+------------------+-------------------+
| mysql_bin.000009 |   808220 |              |                  |                   |
+------------------+----------+--------------+------------------+-------------------+
1 row in set (0.00 sec)

3.2.4 配置从数据库

[[email protected] ~]# vim /etc/my.cnf
//添加如下内容
[mysqld]
datadir=/var/lib/mysql
socket=/var/lib/mysql/mysql.sock
server-id=5    //设置从库的唯一标识符,从库的server-id值必须大于主库的该值
relay-log=mysql-relay-bin       //启用中继日志relay-log

symbolic-links=0

log-error=/var/log/mysqld.log
pid-file=/var/run/mysqld/mysqld.pid

//配置并启动主从复制



//查看从服务器状态

mysql> CHANGE MASTER TO MASTER_HOST='192.168.233.10',MASTER_USER='repl',MASTER_PASSWORD='repl123',MASTER_LOG_FILE='mysql_bin.000008',MASTER_LOG_POS=154;
Query OK, 0 rows affected, 2 warnings (0.02 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.233.10
                  Master_User: repl
                  Master_Port: 3306
                Connect_Retry: 60
              Master_Log_File: mysql_bin.000008
          Read_Master_Log_Pos: 154
               Relay_Log_File: mysql_relay_log.000002
                Relay_Log_Pos: 320
        Relay_Master_Log_File: mysql_bin.000008
             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: e2955a52-34dd-11e9-a0fd-000c29c6e1a8
             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: 
            Executed_Gtid_Set: 
                Auto_Position: 0
         Replicate_Rewrite_DB: 
                 Channel_Name: 
           Master_TLS_Version: 
1 row in set (0.00 sec)

3.2.5测试验证

在主服务器的gaosihan添加数据

mysql> use gaosihan;
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 gaosihan;
Empty set (0.00 sec)

mysql> INSERT INTO gaosihan (id,name,age) VALUE (1,'tom',20);
Query OK, 1 row affected (0.01 sec)

mysql> select * from gaosihan;
+----+------+------+
| id | name | age  |
+----+------+------+
|  1 | tom  |   20 |
+----+------+------+
1 row in set (0.00 sec)

在从数据库中查看数据是否同步:
mysql> use gaosihan;
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 gaosihan;
+----+------+------+
| id | name | age  |
+----+------+------+
|  1 | tom  |   20 |
+----+------+------+
1 row in set (0.01 sec)

监控mysql主从的指标

[[email protected] ~]# cat .my.cnf 
[client]
user=root
password=gaosihan123!

1.mysql主从的状态
[[email protected] ~]# cat zhuangtai.sh
#!/bin/bash
a=$(mysql -e 'show slave status\G' | grep 'Slave_IO_Running' | awk -F: '{print $2}')
b=$(mysql -e 'show slave status\G' | grep 'Slave_SQL_Running:' | awk -F: '{print $2}')

if [ $a = Yes ] && [ $b = Yes ];then
echo "0"
else
echo "1"
fi
[[email protected] ~]# bash zhaungtai.sh 
0


2.mysql主从的延迟
[[email protected] ~]# cat yanchi.sh 
#!/bin/bash

c=$(mysql -e 'show slave status\G' | grep 'Read_Master_Log_Pos' | awk -F: '{print $2}')
d=$(mysql -e 'show slave status\G' | grep 'Exec_Master_Log_Pos:' | awk -F: '{print $2}')

if [ $c -eq $d ]; then
echo "没有延迟"
elif [ $c -gt $d ]; then
echo "有延迟"
fi
[[email protected] ~]# bash yanchi.sh 
没有延迟

GTID主从同步

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是一比一的关系。

优缺点

优点:

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

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

缺点

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

GTID主从的配置

查看主服务器的配置
[[email protected] ~]# vim /etc/my.cnf
#GTID
server-id = 13
gtid-mode = ON
enforce-gtid-consistency = ON
#BINLOG
log-slave-updates = 1
log-bin=master-binlog
binlog_format=row
#relay log
#relay logskip_slave_start=1

查看从服务器的配置
[[email protected] ~]# vim /etc/my.cnf
#GTID
server-id = 15
gtid-mode = ON
enforce-gtid-consistency = ON
#BINLOG
log-slave-updates = 1
log-bin=master-binlog
binlog_format=row
#relay log
#relay logskip_slave_start=1

在主库中

mysql> create user [email protected]'192.168.233.20' identified by'123456'
    -> ;
Query OK, 0 rows affected (0.01 sec)

mysql> grant replication slave on *.* to 'haha'@'192.168.233.20';
Query OK, 0 rows affected (0.01 sec)

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

mysql> show master status;
+----------------------+----------+--------------+------------------+------------------------------------------+
| File                 | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set                        |
+----------------------+----------+--------------+------------------+------------------------------------------+
| master-binlog.000001 |      773 |              |                  | e2955a52-34dd-11e9-a0fd-000c29c6e1a8:1-3 |
+----------------------+----------+--------------+------------------+------------------------------------------+
1 row in set (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> 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 master status;
+----------------------+----------+--------------+------------------+------------------------------------------+
| File                 | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set                        |
+----------------------+----------+--------------+------------------+------------------------------------------+
| master-binlog.000001 |      773 |              |                  | e2955a52-34dd-11e9-a0fd-000c29c6e1a8:1-3 |
+----------------------+----------+--------------+------------------+------------------------------------------+
1 row in set (0.00 sec)

在从库中

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

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

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

mysql> CHANGE MASTER TO MASTER_HOST='192.168.233.10', MASTER_USER='haha', MASTER_PASSWORD='123456', MASTER_LOG_FILE='mysql-bin.000001', MASTER_LOG_POS=773;
ERROR 1776 (HY000): Parameters MASTER_LOG_FILE, MASTER_LOG_POS, RELAY_LOG_FILE and RELAY_LOG_POS cannot be set when MASTER_AUTO_POSITION is active.
mysql> show slave status \G
*************************** 1. row ***************************
               Slave_IO_State: Waiting for master to send event
                  Master_Host: 192.168.233.10
                  Master_User: haha
                  Master_Port: 3306
                Connect_Retry: 60
              Master_Log_File: master-binlog.000001
          Read_Master_Log_Pos: 773
               Relay_Log_File: localhost-relay-bin.000003
                Relay_Log_Pos: 426
        Relay_Master_Log_File: master-binlog.000001
             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: 773
              Relay_Log_Space: 637
              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: 13
                  Master_UUID: e2955a52-34dd-11e9-a0fd-000c29c6e1a8
             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: ddb90b8b-3a67-11e9-bebf-000c2948ad09:1
            Executed_Gtid_Set: ddb90b8b-3a67-11e9-bebf-000c2948ad09:1,
e2955a52-34dd-11e9-a0fd-000c29c6e1a8:1-3
                Auto_Position: 1
         Replicate_Rewrite_DB: 
                 Channel_Name: 
           Master_TLS_Version: 
1 row in set (0.00 sec)

切记需要关闭防火墙
systemctl stop firewalld

验证
在主库中插入数据

insert gaosihan.student value (15,'fupian',25);
Query OK, 1 row affected (0.10 sec)

在从库中验证

mysql> select * from gaosihan.student;
+----+-------------+------+
| id | name        | age  |
+----+-------------+------+
|  1 | tom         |   20 |
|  2 | jerry       |   23 |
|  3 | wangqing    |   25 |
|  4 | sean        |   28 |
|  5 | zhangshan   |   26 |
|  6 | zhangshan   |   20 |
|  7 | lisi        | NULL |
|  8 | chengshuo   |   10 |
|  9 | wangwu      |    3 |
| 10 | qiuyi       |   15 |
| 11 | qiuxiaotian |   20 |
| 15 | fupian      |   25 |
+----+-------------+------+
12 rows in set (0.01 sec)