mysql主从
mysql主从
1. 主从简介
在现代企业中,数据显得尤为重要,而存储数据的数据库选择又五花八门,但无论是何种数据库,均存在着一种隐患。
想几个问题:
- 用一台数据库存放数据,若此数据库服务器宕机了导致数据丢失怎么办?
- 业务量大了,数据多了,访问的人多了,一台数据库无法保证服务质量了怎么办?
1.1 主从作用
- 实时灾备,用于故障切换
- 读写分离,提供查询服务
- 备份,避免影响业务
1.2 主从形式
- 一主一从
- 主主复制
- 一主多从—扩展系统读取的性能,因为读是在从库读取的
- 多主一从—5.7开始支持
- 联级复制
2. 主从复制原理
主从复制步骤:
- 主库将所有的写操作记录到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.10 | centos7/redhat7 mysql-5.7 | 有数据 |
从数据库 | 192.168.110.20 | centos7/redhat7 mysql-5.7 | 无数据 |
3.1 mysql安装
安装步骤略
3.2 mysql主从配置
3.2.1 确保从数据库与主数据库里的数据一样
为确保从数据库与主数据库里的数据一样,先全备主数据库并还原到从数据库中
//先查看主库有哪些库
[[email protected] ~]# mysql -uroot -pliwenfeng123! -e 'show databases;'
mysql: [Warning] Using a password on the command line interface can be insecure.
+--------------------+
| Database |
+--------------------+
| information_schema |
| liwenfeng |
| mysql |
| performance_schema |
| sys |
| zabbix |
+--------------------+
//再查看从库有哪些库
[[email protected] ~]# mysql -uroot -pliwenfeng -e 'show databases;'
mysql: [Warning] Using a password on the command line interface can be insecure.
+--------------------+
| Database |
+--------------------+
| information_schema |
| mysql |
| performance_schema |
| sys |
+--------------------+
全备主库
[[email protected] ~]# mysqldump -uroot -pliwenfeng123! --all-databases > all.sql
mysqldump: [Warning] Using a password on the command line interface can be insecure.
[[email protected] ~]# ls
all.sql
全备主库时需要另开一个终端,给数据库加上写锁,避免在备份期间有其他人在写入导致数据不一致
mysql> FLUSH TABLES WITH READ LOCK;
Query OK, 0 rows affected (0.00 sec)
//此锁表的终端必须在备份完成以后才能退出
将主库备份文件传送到从库
[[email protected] ~]# scp all.sql [email protected]:/root/
[email protected]'s password:
all.sql 100% 4850KB 36.4MB/s 00:00
解除主库的锁表状态,直接退出交互式界面即可
mysql> quit
Bye
在从库上恢复主库的备份并查看从库有哪些库,确保与主库一致
[[email protected] ~]# mysql -uroot -pliwenfeng < /root/all.sql
mysql: [Warning] Using a password on the command line interface can be insecure.
[[email protected] ~]# mysql -uroot -pliwenfeng -e 'show databases;'
mysql: [Warning] Using a password on the command line interface can be insecure.
+--------------------+
| Database |
+--------------------+
| information_schema |
| liwenfeng |
| mysql |
| performance_schema |
| sys |
| zabbix |
+--------------------+
3.2.2 在主数据库里创建一个同步账号授权给从数据库使用
mysql> CREATE USER 'repl'@'192.168.110.20' IDENTIFIED BY 'repl123';
Query OK, 0 rows affected (0.00 sec)
mysql> GRANT REPLICATION SLAVE ON *.* TO 'repl'@'192.168.110.20';
Query OK, 0 rows affected (0.00 sec)
mysql> flush privileges;
Query OK, 0 rows affected (0.00 sec)
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=1 //数据库服务器唯一标识符,主库的server-id值必须比从库的小
symbolic-links=0
log-error=/var/log/mysqld.log
pid-file=/var/run/mysqld/mysqld.pid
//重启mysql服务
[[email protected] ~]# service mysqld restart
[[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.000002 | 154 | | | |
+------------------+----------+--------------+------------------+-------------------+
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=2 //设置从库的唯一标识符,从库的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服务
[[email protected] ~]# service mysqld restart
[[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> CHANGE MASTER TO
-> MASTER_HOST='192.168.110.10',
-> MASTER_USER='repl',
-> MASTER_PASSWORD='repl123',
-> MASTER_LOG_FILE='mysql-bin.000002',
-> MASTER_LOG_POS=154;
Query OK, 0 rows affected, 2 warnings (0.33 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.10
Master_User: repl
Master_Port: 3306
Connect_Retry: 60
Master_Log_File: mysql-bin.000002
Read_Master_Log_Pos: 9635
Relay_Log_File: mysql-relay-bin.000002
Relay_Log_Pos: 9801
Relay_Master_Log_File: mysql-bin.000002
Slave_IO_Running: Yes
Slave_SQL_Running: Yes
Replicate_Do_DB:
Replicate_Ignore_DB:
3.2.5 测试验证
在主服务器的liwenfeng库的student表中插入数据:
mysql> insert into liwenfeng.student value(5,'lwf',21);
Query OK, 1 row affected (0.03 sec)
Records: 3 Duplicates: 0 Warnings: 0
mysql> select * from liwenfeng.student;
+----+----------+------+
| id | name | age |
+----+----------+------+
| 1 | tom | 20 |
| 2 | jerry | 23 |
| 3 | lisi | 24 |
| 4 | zhangsan | 26 |
| 5 | lwf | 21 |
+----+----------+------+
5 rows in set (0.00 sec)
在从数据库中查看数据是否同步:
mysql> use liwenfeng
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 | 20 |
| 2 | jerry | 23 |
| 3 | lisi | 24 |
| 4 | zhangsan | 26 |
| 5 | lwf | 21 |
+----+----------+------+
5 rows in set (0.00 sec)
4.监控mysql主从I/O线程和SQL线程
编辑配置文件
[[email protected] ~]# vim /usr/local/etc/zabbix_agentd.conf
UnsafeUserParameters=1
UserParameter=check_mysql,/scripts/check_mysql.sh
编写脚本判断I/O线程和SQL线程状态都为yes
[[email protected] ~]# vim /scripts/check_mysql.sh
a=$(mysql -uroot -pliwenfeng123! -e 'show slave status \G' 2>/dev/null|grep Slave_IO_Running:|awk -F: '{print $NF}')
b=$(mysql -uroot -pliwenfeng123! -e 'show slave status \G' 2>/dev/null|grep Slave_SQL_Running:|awk -F: '{print $NF}')
if [ ${a} == "Yes" -a ${b} == "Yes" ];then
echo 1
else
echo 0
fi
服务端手动验证
[[email protected] ~]# zabbix_get -s 192.168.110.20 -k check_mysql
0
添加监控项
添加触发器
关闭slave
[[email protected] ~]# mysql
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 685
Server version: 5.7.23 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> stop slave
验证
5.监控pos值
编写配置文件
[[email protected] ~]# vim /usr/local/etc/zabbix_agentd.conf
UserParameter=check_mysql_pos,/scripts/check_mysql_pos.sh
编写脚本过滤
[[email protected] ~]# vim /scripts/check_mysql_pos.sh
a=$(mysql -uroot -pliwenfeng123! -e 'show slave status \G' 2>/dev/null |grep Read_Master_Log_Pos:|awk -F: '{print $NF}')
b=$(mysql -uroot -pliwenfeng123! -e 'show slave status \G' 2>/dev/null |grep Exec_Master_Log_Pos:|awk -F: '{print $NF}')
c=$[$a-$b]
if [ $c -eq 0 ];then
echo 0
else
echo $c
fi
添加监控项
添加触发器
6.GTID主从
GTID的工作原理:
- master更新数据时,会在事务前产生GTID,一同记录到binlog日志中。
- slave端的i/o 线程将变更的binlog,写入到本地的relay log中。
- sql线程从relay log中获取GTID,然后对比slave端的binlog是否有记录。
- 如果有记录,说明该GTID的事务已经执行,slave会忽略。
- 如果没有记录,slave就会从relay log中执行该GTID的事务,并记录到binlog。
- 在解析过程中会判断是否有主键,如果没有就用二级索引,如果没有就用全部扫描
GTID的优点:
- 一个事务对应一个唯一ID,一个GTID在一个服务器上只会执行一次
- GTID是用来代替传统复制的方法,GTID复制与普通复制模式的最大不同就是不需要指定二进制文件名和位置
- 减少手工干预和降低服务故障时间,当主机挂了之后通过软件从众多的备机中提升一台备机为主机
那么GTID复制是怎么实现自动同步,自动对应位置的呢?
例如:ServerC <-----ServerA ----> ServerB
主机ServerA
备机:ServerB,ServerC
当主机ServerA 挂了之后 ,此时ServerB执行完了所有从ServerA 传过来的事务,
ServerC 延时一点。这个时候需要把 ServerB 提升为主机 ,Server C 继续为备机。
当ServerC 链接ServerC 之后,首先在自己的二进制文件中找到从ServerA 传过来的最新的GTID,
然后将这个GTID 发送到ServerB ,ServerB 获得这个GTID之后,就开始从这个GTID的下一个GTID
开始发送事务给ServerC。这种自我寻找复制位置的模式减少事务丢失的可能性以及故障恢复的时间。
GTID的限制:
- 不支持非事务引擎
- 不支持create table … select 语句复制(主库直接报错)
原理:( 会生成两个sql,一个是DDL创建表SQL,一个是insert into 插入数据的sql。
由于DDL会导致自动提交,所以这个sql至少需要两个GTID,但是GTID模式下,只能给这个sql生成一个GTID ) - 不允许一个SQL同时更新一个事务引擎表和非事务引擎表
- 在一个复制组中,必须要求统一开启GTID或者是关闭GTID
- 开启GTID需要重启(5.7除外)
- 开启GTID后,就不再使用原来的传统复制方式
- 对于create temporary table 和 drop temporary table语句不支持
- 不支持sql_slave_skip_counter
配置
在传统同步的基础上
/在主和从库配置文件分别加入以下两行代码,重新服务
从库
[[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.110.10
Master_User: repl
Master_Port: 3306
Connect_Retry: 60
Master_Log_File: mysql_log.000019
Read_Master_Log_Pos: 154
Relay_Log_File: mysql-relay-bin.000002
Relay_Log_Pos: 367
Relay_Master_Log_File: mysql_log.000019
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: 574
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: 39911099-34dc-11e9-a356-000c29fe51c1
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:458-403
Executed_Gtid_Set: 2b8e13f0-34ef-11e9-8ec3-000c29282f4e:1-403
Auto_Position: 1
Replicate_Rewrite_DB:
Channel_Name:
Master_TLS_Version:
1 row in set (0.00 sec)
验证:
主库写入数据
mysql> use liwenfeng
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 into student value (5,'li',22);
Query OK, 1 row affected (0.00 sec)
从库
mysql> select * from student;
+----+----------+------+
| id | name | age |
+----+----------+------+
| 1 | tom | 20 |
| 2 | jerry | 23 |
| 3 | lisi | 24 |
| 4 | zhangsan | 26 |
| 1 | lwf | 21 |
| 5 | li | 22 |
+----+----------+------+
6 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.110.10
Master_User: repl
Master_Port: 3306
Connect_Retry: 60
Master_Log_File: mysql_log.000019
Read_Master_Log_Pos: 154
Relay_Log_File: mysql-relay-bin.000002
Relay_Log_Pos: 367
Relay_Master_Log_File: mysql_log.000019
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: 574
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: 39911099-34dc-11e9-a356-000c29fe51c1
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:547-403
Executed_Gtid_Set: 2b8e13f0-34ef-11e9-8ec3-000c29282f4e:1-403
Auto_Position: 1
Replicate_Rewrite_DB:
Channel_Name:
Master_TLS_Version:
1 row in set (0.00 sec)