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 restart

mysql> 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 event
                  Master_Host: 192.168.230.16
                  Master_User: rep
                  Master_Port: 3306
                Connect_Retry: 60
              Master_Log_File: mysql-bin.000001
          Read_Master_Log_Pos: 841636
               Relay_Log_File: slave-relay-bin.000002
                Relay_Log_Pos: 37697
        Relay_Master_Log_File: mysql-bin.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: 841636
              Relay_Log_Space: 37904
              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: b27045f6-36bb-11e9-b404-000c29c87115
             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: 

看到這個就說明成功了

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 ];then
        echo 0
else
        echo 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 ];then
        echo 0
else
        echo 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 ];then
        echo "0"
else
        echo "$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;

過一會就告警了,郵件也發過來了
在這裏插入圖片描述

另外兩個照着上面做就行了~~

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章