本文主要是介绍mysql主从配置和从库zabbix监控,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
Mysql的主从架构模式,是很多企业广泛使用,并且大家所广为熟知的一种架构模式
mysql主从复制主要用途
- 实时灾备,用于故障切换
- 读写分离,提供查询服务
- 备份,避免影响业务
mysql主从复制存在的问题
- 主库宕机后, 数据可能丢失
- 主库写压力大, 复制可能会延时
主从复制原理
主从复制步骤:
-
主库将所有的写操作记录到binlog日志中并生成一个log dump线程,将binlog日志传给从库的I/O线程
-
从库生成两个线程,一个I/O线程,一个SQL线程
- I/O线程去请求主库的binlog,并将得到的binlog日志写到relay log(中继日志) 文件中
- SQL线程会读取relay log文件中的日志,并解析成具体操作,来实现主从的操作一致,达到最终数据一致的目的
主从复制配置
主从复制配置步骤:
确保从数据库与主数据库里的数据一样
在主数据库里创建一个同步账号授权给从数据库使用
配置主数据库(修改配置文件)
配置从数据库(修改配置文件)
需求:
搭建两台MySQL服务器,一台作为主服务器,一台作为从服务器,主服务器进行写操作,从服务器进行读操作
环境说明:
数据库角色 | IP | 应用与系统版本 | 有无数据 |
---|---|---|---|
主数据库 | 192.168.230.16 | centos7/redhat7 mysql-5.7 | 有数据 |
从数据库 | 192.168.230.15 | centos7/redhat7 mysql-5.7 | 无数据 |
这里mysql的安装都是源码编译的,详情见前面几篇博客
主库
[root@master ~]# mysql -uroot -p
mysql> create database cs;
mysql> use cs;
mysql> create table aa (id int,name varchar(30));
mysql> insert into aa values (1,'apple'),(2,'banana');
mysql> select * from aa;
+------+--------+
| id | name |
+------+--------+
| 1 | apple |
| 2 | banana |
+------+--------+
做全备
[root@master ~]# mysqldump -uroot -p'cs123.com' --all-databases > $(date +%F-%H-%M)-all.sql
拷贝到从主机
[root@master ~]# scp 2019-02-27-14-36-all.sql 192.168.230.15:/root/
从库
[root@slave ~]# mysql -uroot -p'cs123.com' < 2019-02-27-14-36-all.sql
mysql> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| cs |
| mysql |
| performance_schema |
| sys |
| zabbix |
+--------------------+
mysql> use cs;
mysql> show tables;
+--------------+
| Tables_in_cs |
+--------------+
| aa |
+--------------+
mysql> select * from aa;
+------+--------+
| id | name |
+------+--------+
| 1 | apple |
| 2 | banana |
+------+--------+
主库
mysql> grant replication slave on *.* to 'rep'@'192.168.230.15' identified by 'rep123.com';
mysql> flush privileges;
从库
[root@slave ~]# mysql -urep -p'rep123.com' -h 192.168.230.16
mysql> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
+--------------------+
主库
[root@master ~]# vim /etc/my.cnf
log-bin=mysql-bin
server-id=1
[root@master ~]# service mysqld restartmysql> show master status;
+------------------+----------+--------------+------------------+-------------------+
| File | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set |
+------------------+----------+--------------+------------------+-------------------+
| mysql-bin.000001 | 804259 | | | |
+------------------+----------+--------------+------------------+-------------------+
从库
[root@slave ~]# vim /etc/my.cnf
log-bin=mysql-bin
server-id=2
[root@slave ~]# service mysqld restart
mysql> change master to master_host='192.168.230.16',master_user='rep',master_password='rep123.com',master_log_file='mysql-bin.000001',master_log_pos=804259;
mysql> start slave;
mysql> show slave status \G
*************************** 1. row ***************************Slave_IO_State: Waiting for master to send eventMaster_Host: 192.168.230.16Master_User: repMaster_Port: 3306Connect_Retry: 60Master_Log_File: mysql-bin.000001Read_Master_Log_Pos: 841636Relay_Log_File: slave-relay-bin.000002Relay_Log_Pos: 37697Relay_Master_Log_File: mysql-bin.000001Slave_IO_Running: YesSlave_SQL_Running: YesReplicate_Do_DB: Replicate_Ignore_DB: Replicate_Do_Table: Replicate_Ignore_Table: Replicate_Wild_Do_Table: Replicate_Wild_Ignore_Table: Last_Errno: 0Last_Error: Skip_Counter: 0Exec_Master_Log_Pos: 841636Relay_Log_Space: 37904Until_Condition: NoneUntil_Log_File: Until_Log_Pos: 0Master_SSL_Allowed: NoMaster_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: NoLast_IO_Errno: 0Last_IO_Error: Last_SQL_Errno: 0Last_SQL_Error: Replicate_Ignore_Server_Ids: Master_Server_Id: 1Master_UUID: b27045f6-36bb-11e9-b404-000c29c87115Master_Info_File: /opt/data/master.infoSQL_Delay: 0SQL_Remaining_Delay: NULLSlave_SQL_Running_State: Slave has read all relay log; waiting for more updatesMaster_Retry_Count: 86400Master_Bind: Last_IO_Error_Timestamp: Last_SQL_Error_Timestamp: Master_SSL_Crl: Master_SSL_Crlpath: Retrieved_Gtid_Set: Executed_Gtid_Set: Auto_Position: 0Replicate_Rewrite_DB: Channel_Name: Master_TLS_Version:
看到这个就说明成功了
Slave_IO_Running: Yes
Slave_SQL_Running: Yes
测试
主库插入数据
mysql> use cs;
mysql> show tables;
+--------------+
| Tables_in_cs |
+--------------+
| aa |
+--------------+
mysql> insert into aa values (3,'cici'),(4,'dog');
mysql> select * from aa;
+------+--------+
| id | name |
+------+--------+
| 1 | apple |
| 2 | banana |
| 3 | cici |
| 4 | dog |
+------+--------+从库查看
mysql> use cs;
mysql> select * from aa;
+------+--------+
| id | name |
+------+--------+
| 1 | apple |
| 2 | banana |
| 3 | cici |
| 4 | dog |
+------+--------+
从库zabbix监控
先在zabbix客户端添加监控脚本和修改配置
[root@slave scripts]# vim Slave_IO_Running.sh
#!/bin/bash
Slave_IO_Running=$(mysql -uroot -pcs123.com -e "show slave status \G" 2> /dev/null |grep "Slave_IO_Running:" |awk -F: '{pri nt $2}')
if [ $Slave_IO_Running == Yes ];thenecho 0
elseecho 1
fi[root@slave scripts]# vim Slave_SQL_Running.sh
#!/bin/bash
Slave_SQL_Running=$(mysql -uroot -pcs123.com -e "show slave status \G" 2> /dev/null |grep "Slave_SQL_Running:" |awk -F: '{p rint $2}')
if [ $Slave_SQL_Running == Yes ];thenecho 0
elseecho 1
fi[root@slave scripts]# vim Read_Exec.sh
#!/bin/bash
Read_Master_Log_Pos=$(mysql -uroot -pcs123.com -e "show slave status \G" 2> /dev/null | grep "Read_Master_Log_Pos:" |awk -F: '{print $2}')
Exec_Master_Log_Pos=$(mysql -uroot -pcs123.com -e "show slave status \G" 2> /dev/null | grep "Exec_Master_Log_Pos:" |awk -F: '{print $2}')
a=$[$Read_Master_Log_Pos - $Exec_Master_Log_Pos]
if [ $a -eq 0 ];thenecho "0"
elseecho "$a"
fi[root@slave ~]# chmod -R 755 /scripts/
[root@slave ~]# chown -R zabbix.zabbix /scripts/[root@slave ~]# vim /usr/local/etc/zabbix_agentd.conf
UserParameter=check_mysql_io,/scripts/Slave_IO_Running.sh
UserParameter=check_mysql_sql,/scripts/Slave_SQL_Running.sh
UserParameter=check_mysql_wait,/scripts/Read_Exec.sh[root@slave scripts]# pkill zabbix
[root@slave scripts]# zabbix_agentd
然后就是配置zabbix的web界面,添加监控项和触发器
添加完了触发器后再添加告警媒介,因为之前已经添加过了,这里就不添加直接用了,怎么添加见上个博客
然后去客户端停掉slave角色,等待报警结果
mysql> stop slave;
过一会就告警了,邮件也发过来了
另外两个照着上面做就行了~~
这篇关于mysql主从配置和从库zabbix监控的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!