mysql主从
1. 主从简介
在现代企业中,数据显得尤为重要,而存储数据的数据库选择又五花八门,但无论是何种数据库,均存在着一种隐患。
想几个问题:
- 用一台数据库存放数据,若此数据库服务器宕机了导致数据丢失怎么办?
- 业务量大了,数据多了,访问的人多了,一台数据库无法保证服务质量了怎么办?
1.1 主从的作用
- 实时灾备,用于故障切换
- 读写分离,提供查询服务
- 备份,避免影响业务
1.2 主从形式
- 一主一从
- 主主复制
- 一主多从—扩展系统读取的性能,因为是在从库读取的
- 多主一从—5.7开始支持
- 联级复制
主从复制原理
主从复制步骤
- 主库将所有的写操作记录到binlog日志中并生成一个log dump线程,将binlog日志传给从库的I/O线程
- 从库生成两个线程,一个I/O线程,一个SQL线程
- I/O线程去请求主库的binlog,并将得到的binlog日志写到relay log(中继日志) 文件中
- SQL线程,会读取relay log文件中的日志,并解析成具体操作,来实现主从的操作一致,达到最终数据一致的目的
3.主从复制配置
主从复制配置步骤:
- 确保从数据库与主数据库里的数据一样
- 在主数据库里创建一个同步账号授权给从数据库使用
- 配置主数据库(修改配置文件)
- 配置从数据库(修改配置文件)
需求:
搭建两台MySQL服务器,一台作为主服务器,一台作为从服务器,主服务器进行写操作,从服务器进行读操作
环境说明
数据库角色 | ip | 应用与系统版本 | 有无数据 |
---|---|---|---|
主数据库 | 192.168.120.11 | centos7/redhat7 mysql-5.7 | 有数据 |
从数据库 | 192.168.120.22 | centos7/redhat7 mysql-5.7 | 无数据 |
3.1 mysql安装
分别在主从两台服务器上安装mysql-5.7版本,此处略过安装步骤,若有疑问请参考《mysql基础》与《mysql进阶》两篇文章。
3.2 mysql主从配置
3.2.1 确保从数据库与主数据库里的数据一样
为确保从数据库与主数据库里的数据一样,先全备主数据库并还原到从数据库中
//先查看主库有哪些库
[root@jun ~]# mysql -uroot -pming123 -e 'show databases;'
mysql: [Warning] Using a password on the command line interface can be insecure.
+--------------------+
| Database |
+--------------------+
| information_schema |
| ming |
| mysql |
| performance_schema |
| sys |
| zabbix |
+--------------------+
[root@jun ~]# scp /etc/my.cnf root@192.168.120.22:/root
root@192.168.120.22's password:
my.cnf 100% 191 203.3KB/s 00:00
[root@jun ~]#
//查看从库有哪些库
mysql> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| mysql |
| performance_schema |
| sys |
+--------------------+
4 rows in set (0.00 sec)
mysql>
//全备主库
全备主库时需要另开一个终端,给数据库加上读锁,避免在备份期间有其他人在写入导致数据不一致
mysql>
mysql> FLUSH TABLES WITH READ LOCK;
Query OK, 0 rows affected (2.39 sec)
mysql>
//备份主库并将备份文件传到从库
[root@jun ~]# mysqldump -uroot -pming123 --all-databases > /opt/all-201902271653.sql;
mysqldump: [Warning] Using a password on the command line interface can be insecure.
[root@jun ~]# ls /opt/
all-201902271653.sql data defines.inc.php
[root@jun ~]# scp /opt/all-201902271653.sql root@192.168.120.22:/opt/
root@192.168.120.22's password:
all-201902271653.sql 100% 9206KB 31.6MB/s 00:00
[root@jun ~]#
//解除主库的锁表状态,直接退出交互式界面即可
mysql> quit
Bye
[root@jun ~]#
//在从库上恢复主库的备份,并查看从库有哪些库,确保与主库一致
[root@localhost etc]# mysql -uroot -pming123 < /opt/all-201902271653.sql
mysql: [Warning] Using a password on the command line interface can be insecure.
[root@localhost etc]# mysql -uroot -pming123 -e 'show databases;'
mysql: [Warning] Using a password on the command line interface can be insecure.
+--------------------+
| Database |
+--------------------+
| information_schema |
| ming |
| mysql |
| performance_schema |
| sys |
| zabbix |
+--------------------+
[root@localhost etc]#
3.2.2 在主数据库里创建一个同步账号授权给从数据库使用
mysql> CREATE USER 'repl'@'192.168.120.22' IDENTIFIED BY 'repl123';
Query OK, 0 rows affected (0.20 sec)
mysql> GRANT REPLICATION SLAVE ON *.* TO 'repl'@'192.168.120.22';
Query OK, 0 rows affected (0.00 sec)
mysql> flush privileges;
Query OK, 0 rows affected (0.05 sec)
删除授权
MariaDB [(none)]> drop user 'jml'@'192.168.161.6';
查看授权
MariaDB [(none)]> show grants for 'lmj'@'192.168.161.7';
查看所有授权
MariaDB [(none)]> select * from information_schema.user_privileges;
3.2.3 配置主数据库
[root@jun ~]# 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
server-id = 1 // 数据库服务器唯一标识符,比从库小
log-bin = mysql-bin // 启动binlog日志
symbolic-links=0
[root@jun ~]#
//重启服务
[root@jun ~]# service mysqld stop
Shutting down MySQL....... SUCCESS!
[root@jun ~]# service mysqld start
Starting MySQL............ SUCCESS!
//查看主库状态
mysql> show master status;
+------------------+----------+--------------+------------------+-------------------+
| File | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set |
+------------------+----------+--------------+------------------+-------------------+
| mysql-bin.000001 | 83699 | | | |
+------------------+----------+--------------+------------------+-------------------+
1 row in set (0.00 sec)
3.2.4 配置从数据库
[root@localhost ~]# 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
server-id = 2 // 设置从库唯一标识符,从库必须必主库大
log-bin = mysql-bin //启动中继日志relay-log
skip-grant-tables
relay-log=mysql-relay-bin
[root@localhost ~]#
//重启服务
[root@localhost ~]# service mysqld stop
Shutting down MySQL.. SUCCESS!
[root@localhost ~]# service mysqld start
Starting MySQL. SUCCESS!
//配置并启动主从复制
mysql> CHANGE MASTER TO
-> MASTER_HOST='192.168.120.11',
-> MASTER_USER='repl',
-> MASTER_PASSWORD='repl123',
-> MASTER_LOG_FILE='mysql-bin.000001',
-> MASTER_LOG_POS=83699;
Query OK, 0 rows affected, 2 warnings (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.120.11
Master_User: repl
Master_Port: 3306
Connect_Retry: 60
Master_Log_File: mysql-bin.000001
Read_Master_Log_Pos: 114192
Relay_Log_File: mysql-relay-bin.000004
Relay_Log_Pos: 30813
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 测试验证
在主服务器上插入数据
mysql> select * from jun;
+----+------+------+
| id | name | AGE |
+----+------+------+
| 1 | lmj | 10 |
+----+------+------+
1 row in set (0.00 sec)
mysql> insert into jun values (2,'liu',9);
Query OK, 1 row affected (0.00 sec)
mysql> select * from jun;
+----+------+------+
| id | name | AGE |
+----+------+------+
| 1 | lmj | 10 |
| 2 | liu | 9 |
+----+------+------+
2 rows in set (0.00 sec)
mysql>
在从数据库上查看是否同步
mysql> select * from jun;
+----+------+------+
| id | name | AGE |
+----+------+------+
| 2 | liu | 9 |
+----+------+------+
1 row in set (0.00 sec)
mysql>
4.1 zabbix监测mysql主从
4.1.1 mysql主从同步故障
- 创建监测主从故障脚本
[root@localhost scripts]# cat 1.sh
#!/bin/bash
a=$(mysql -uroot -pming123 -e 'show slave status \G' 2>/dev/null |grep Slave_IO_Running|awk -F ': ' '{print $2}')
b=$(mysql -uroot -pming123 -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
[root@localhost scripts]#
- 给脚本权限
[root@localhost scripts]# chmod +x 1.sh
[root@localhost scripts]# chown -R zabbix.zabbix 1.sh
[root@localhost scripts]# ll
total 8
-rwxr-xr-x. 1 zabbix zabbix 258 Feb 28 01:28 1.sh
- 修改zabbix配置文件
[root@localhost scripts]# vim /usr/local/etc/zabbix_agentd.co
UserParameter=check_mysql,/scripts/1.sh
UserParameter=check_mysql1,/scripts/2.sh
- 重启服务
- 添加监控项
- 添加触发器
- 验证
//关闭从库
mysql> stop slave;
Query OK, 0 rows affected (0.00 sec)
zabbix 告警
4.1.2 mysql主从同步延迟
- 创建延迟脚本
[root@localhost ~]# cat /scripts/2.sh
#!/bin/bash
#!/bin/bash
#!/bin/bash
a=$( mysql -e 'show slave status \G'|grep Exec_Master_Log_Pos|awk -F ': ' '{print $2}')
b=$(mysql -e 'show slave status \G'|grep Read_Master_Log_Pos:|awk -F ': ' '{print $2}')
c=$[ $a - $b ]
if [ $c -ne 0 ];then
echo 1
else
echo 0
fi
[root@localhost ~]#
- 更改脚本权限
[root@localhost scripts]# chmod +x 2.sh
[root@localhost scripts]# chown -R zabbix.zabbix 2.sh
[root@localhost scripts]# ll
total 8
-rwxr-xr-x. 1 zabbix zabbix 258 Feb 28 01:28 1.sh
-rwxr-xr-x. 1 zabbix zabbix 279 Feb 27 07:19 2.sh
- 修改zabbix配置文件
[root@localhost ~]# vim /usr/local/etc/zabbix_agentd.conf
UserParameter=check_mysql1,/scripts/2.sh
- 重启服务
- 添加监控项
- 添加触发器
- 验证