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.47.138 | centos7/redhat7 mysql-5.7 |
有数据 |
从数据库 | 192.168.47.137 | centos7/redhat7 mysql-5.7 |
无数据 |
3.1 mysql安装
分别在主从两台服务器上安装mysql-5.7版本,此处略过安装步骤,若有疑问请参考《mysql基础》与《mysql进阶》两篇文章。
3.2 mysql主从配置
3.2.1 确保从数据库与主数据库里的数据一样
为确保从数据库与主数据库里的数据一样,先全备主数据库并还原到从数据库中
//先查看主库有哪些库
[[email protected] ~]# mysql -uroot -p123456 -e 'show databases;'
mysql: [Warning] Using a password on the command line interface can be insecure.
+--------------------+
| Database |
+--------------------+
| information_schema |
| aaa |
| mysql |
| performance_schema |
| sys |
| zabbix |
+--------------------+
//再查看从库有哪些库
[[email protected] ~]# mysql -uroot -p123456 -e 'show databases;'
mysql: [Warning] Using a password on the command line interface can be insecure.
+--------------------+
| Database |
+--------------------+
| information_schema |
| mysql |
| pengye |
| performance_schema |
| sys |
+--------------------+
//全备主库
//全备主库时需要另开一个终端,给数据库加上读锁,避免在备份期间有其他人在写入导致数据不一致
[[email protected] ~]# mysql -uroot -p123456
mysql> FLUSH TABLES WITH READ LOCK;
Query OK, 0 rows affected (0.07 sec)
//此锁表的终端必须在备份完成以后才能退出
//备份主库并将备份文件传送到从库
[[email protected] ~]# mysqldump -uroot -p123456 --all-databases > all.sql
mysqldump: [Warning] Using a password on the command line interface can be insecure.
[[email protected] ~]# ls
all.sql anaconda-ks.cfg
[[email protected] ~]# scp all.sql [email protected]:/root
[email protected]'s password:
all.sql 100% 5179KB 44.9MB/s 00:00
//解除主库的锁表状态,直接退出交互式界面即可
mysql> quit
Bye
//在从库上恢复主库的备份并查看从库有哪些库,确保与主库一致
[[email protected] ~]# ls
all.sql anaconda-ks.cfg
[[email protected] ~]# mysql -uroot -p123456 < all.sql
mysql: [Warning] Using a password on the command line interface can be insecure.
[[email protected] ~]# mysql -uroot -p123456 -e 'show databases;'
mysql: [Warning] Using a password on the command line interface can be insecure.
+--------------------+
| Database |
+--------------------+
| information_schema |
| aaa |
| mysql |
| pengye |
| performance_schema |
| sys |
| zabbix |
+--------------------+
3.2.2 在主数据库里创建一个同步账号授权给从数据库使用
在主数据库上一步创建同步账号
[[email protected] ~]# mysql -uroot -p123456
mysql> grant replication slave on *.* to 'repl'@'192.168.47.137' identified by '123';
Query OK, 0 rows affected, 1 warning (0.00 sec)
mysql> flush privileges;
Query OK, 0 rows affected (0.00 sec)
去从数据库上登陆账号检测是否创建成功
[[email protected] ~]# mysql -urepl -p123 -h 192.168.47.138
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 3397
Server version: 5.7.23 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;
3.2.3 配置主数据库
[[email protected] ~]# vim /etc/my.cnf
//在[mysqld]这段的后面加上如下内容
log-bin=mysql-bin //启用binlog日志
server-id=3 //数据库服务器唯一标识符,主库的server-id值必须比从库的小
//重启mysql服务
[[email protected] ~]# service mysqld restart
Shutting down MySQL.... SUCCESS!
Starting MySQL.. SUCCESS!
//查看主库的状态
mysql> show master status;
+------------------+----------+--------------+------------------+-------------------+
| File | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set |
+------------------+----------+--------------+------------------+-------------------+
| mysql-bin.000001 | 46044 | | | |
+------------------+----------+--------------+------------------+-------------------+
1 row in set (0.00 sec)
3.2.4 配置从数据库
[[email protected] ~]# vim /etc/my.cnf //添加如下内容
server-id=5 //设置从库的唯一标识符,从库的server-id值必须小于主库的该值
relay-log=mysql-relay-bin //启用中继日志relay-log
//重启从库的mysql服务
[[email protected] ~]# service mysqld restart
Shutting down MySQL.... SUCCESS!
Starting MySQL.. SUCCESS!
//配置并启动主从复制
mysql> change master to
-> master_host='192.168.47.138',
-> master_user='repl',
-> master_password='123',
-> master_log_file='mysql-bin.000001',
-> master_log_pos=46044;
Query OK, 0 rows affected, 2 warnings (0.02 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.47.138
Master_User: repl
Master_Port: 3306
Connect_Retry: 60
Master_Log_File: mysql-bin.000001
Read_Master_Log_Pos: 326921
Relay_Log_File: mysql-relay-bin.000002
Relay_Log_Pos: 281197
Relay_Master_Log_File: mysql-bin.000001
Slave_IO_Running: Yes //此处必须为Yes
Slave_SQL_Running: Yes //此处必须为Yes
Replicate_Do_DB:
Replicate_Ignore_DB:
Replicate_Do_Table:
......
3.2.5 测试验证
在主服务器的aaa库的bbb表中插入数据:
[[email protected] ~]# mysql -uroot -p123456
mysql> use aaa;
mysql> insert bbb values(3,'jia',20),(4,'meng',21);
Query OK, 2 rows affected (0.00 sec)
Records: 2 Duplicates: 0 Warnings: 0
mysql> select * from bbb;
+----+-------+------+
| id | name | age |
+----+-------+------+
| 1 | jerry | 23 |
| 2 | qing | 25 |
| 3 | jia | 20 |
| 4 | meng | 21 |
+----+-------+------+
4 rows in set (0.00 sec)
在从数据库中查看数据是否同步:
[[email protected] ~]# mysql -uroot -p123456
mysql> select * from aaa.bbb;
ERROR 1046 (3D000): No database selected
mysql> select * from aaa.bbb;
+----+-------+------+
| id | name | age |
+----+-------+------+
| 1 | jerry | 23 |
| 2 | qing | 25 |
| 3 | jia | 20 |
| 4 | meng | 21 |
+----+-------+------+
4 rows in set (0.00 sec)
4.监控mysql主从
4.1编写监控脚本
[[email protected] ~]# cd /scripts/
[[email protected] scripts]# vim slave.sh
[[email protected] scripts]# cat slave.sh
#!/bin/bash
a=$(mysql -uroot -p123456 -e 'show slave status\G' 2>/dev/null |grep 'Slave_IO_Running'|awk -F: '{print $NF}')
b=$(mysql -uroot -p123456 -e 'show slave status\G' 2>/dev/null |grep 'Slave_SQL_Running:'|awk -F: '{print $NF}')
if [ $a == Yes -a $b == Yes ];then
echo 0
else
echo 1
fi
[[email protected] scripts]# bash slave.sh //服务正常输出0,发生故障输出1
0
[[email protected] scripts]# vim log.sh
[[email protected] scripts]# cat log.sh
#!/bin/bash
a=$(mysql -uroot -p123456 -e 'show slave status\G' 2>/dev/null |grep 'Exec_Master_Log_Pos'|awk -F: '{print $NF}')
b=$(mysql -uroot -p123456 -e 'show slave status\G' 2>/dev/null |grep 'Read_Master_Log_Pos'|awk -F: '{print $NF}')
c=$[ $a-$b ]
echo $c
[[email protected] scripts]# bash log.sh //服务正常输出0,发生故障输出1
0
//给两个脚本执行权限,并修改属组为zabbix
[[email protected] scripts]# chmod +x log.sh
[[email protected] scripts]# chmod +x slave.sh
[[email protected] scripts]# chown zabbix.zabbix slave.sh
[[email protected] scripts]# chown zabbix.zabbix log.sh
[[email protected] scripts]# ll
total 12
-rwxr-xr-x. 1 zabbix zabbix 115 Feb 25 04:17 check_process.sh
-rwxr-xr-x. 1 zabbix zabbix 280 Feb 28 02:05 log.sh
-rwxr-xr-x. 1 zabbix zabbix 304 Feb 28 02:02 slave.sh
4.2添加监控项与触发项
添加监控项:
添加触发项:
配置agentd配置文件:
[[email protected] scripts]# vim /usr/local/etc/zabbix_agentd.conf //添加下面内容
UserParameter=ck_mysql,/scripts/slave.sh
//重启服务
[email protected] scripts]# pkill zabbix
[[email protected] scripts]# zabbix_agentd
4.3验证:
关闭客户端上mysql的slave,看是否会报警与发邮件
[[email protected] scripts]# mysql -uroot -p123456
mysql> stop slave;
5.GTID主从
5.1 GTID介绍
(1.)基本概念
MySQL 5.6 的新特性之一,全局事务标识符(GTID)是创建的唯一标识符,并与在源(主)服务器上提交的每个事务相关联。此标识符不但是唯一的,而且在给定复制设置中的所有服务器上都是唯一的。所有交易和所有GTID之间都有一对一的映射关系 。它由服务器ID以及事务ID组合而成。这个全局事务ID不仅仅在原始服务器上唯一,在所有存在主从关系 的mysql服务器上也是唯一的。正是因为这样一个特性使得mysql的主从复制变得更加简单,以及数据库一致性更可靠。一个GTID在一个服务器上只执行一次,避免重复执行导致数据混乱或者主从不一致。
(2)GTID的的优点:
- 更简单的实现failover,不用以前那样在需要找log_file和log_pos;
- 更简单的搭建主从复制;
- 比传统的复制更加安全;
- GTID是连续的没有空洞的,保证数据的一致性,零丢失。
5.2 GTID主从配置
1.全备主库,将备份文件传送到从库;在主数据库里创建一个同步账号授权给从数据库使用,上面已经配置过
2.配置主数据库
[[email protected] ~]# vim /etc/my.cnf //添加下面内容
gtid_mode=ON //开启gtid模式
enforce-gtid-consistency=true //强制gtid一致性,开启后对于特定create table不被支持
//重启服务
[[email protected] ~]# service mysqld restart
Shutting down MySQL.... SUCCESS!
Starting MySQL. SUCCESS!
//查看主库的状态
[[email protected] ~]# mysql -uroot -p123456
mysql> show master status;
+------------------+----------+--------------+------------------+---------------------------------------------+
| File | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set |
+------------------+----------+--------------+------------------+---------------------------------------------+
| mysql-bin.000005 | 2490 | | | 9352971c-34e8-11e9-9119-000c29ec8de1:1-1127 |
+------------------+----------+--------------+------------------+---------------------------------------------+
1 row in set (0.00 sec)
3.配置从数据库
[[email protected] ~]# vim /etc/my.cnf //添加下面内容
gtid-mode=ON //开启gtid模式
enforce_gtid_consistency=true //强制gtid一致性,开启后对于特定create table不被支持
//重启服务
[[email protected] ~]# service mysqld restart
Shutting down MySQL.... SUCCESS!
Starting MySQL. SUCCESS!
//配置并启动主从复制
[[email protected] ~]# mysql -uroot -p123456
mysql> change master to
-> master_host='192.168.47.138',
-> master_user='repl',
-> master_password='123',
-> master_log_file='mysql-bin.000005',
-> master_log_pos=2490;
Query OK, 0 rows affected, 2 warnings (0.00 sec)
//启动slave
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.47.138
Master_User: repl
Master_Port: 3306
Connect_Retry: 60
Master_Log_File: mysql-bin.000005
Read_Master_Log_Pos: 41920
Relay_Log_File: mysql-relay-bin.000026
Relay_Log_Pos: 39750
Relay_Master_Log_File: mysql-bin.000005
Slave_IO_Running: Yes //此处必须为Yes
Slave_SQL_Running: Yes //此处必须为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: 41920
Relay_Log_Space: 40123
......
4.测试验证
在主服务器的aaa库的bbb表中插入数据:
[[email protected] ~]# mysql -uroot -p123456
mysql> use aaa;
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 bbb;
+----+-------+------+
| id | name | age |
+----+-------+------+
| 1 | jerry | 23 |
| 2 | qing | 25 |
| 3 | jia | 20 |
| 4 | meng | 21 |
+----+-------+------+
4 rows in set (0.00 sec)
mysql> insert bbb values(5,'peng',21),(6,'ye',22);
Query OK, 2 rows affected (0.00 sec)
Records: 2 Duplicates: 0 Warnings: 0
mysql> select * from bbb;
+----+-------+------+
| id | name | age |
+----+-------+------+
| 1 | jerry | 23 |
| 2 | qing | 25 |
| 3 | jia | 20 |
| 4 | meng | 21 |
| 5 | peng | 21 |
| 6 | ye | 22 |
+----+-------+------+
6 rows in set (0.00 sec)
在从数据库中查看数据是否同步:
[[email protected] ~]# mysql -uroot -p123456
mysql> use aaa;
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 bbb;
+----+-------+------+
| id | name | age |
+----+-------+------+
| 1 | jerry | 23 |
| 2 | qing | 25 |
| 3 | jia | 20 |
| 4 | meng | 21 |
| 5 | peng | 21 |
| 6 | ye | 22 |
+----+-------+------+
6 rows in set (0.00 sec)