1. 程式人生 > >配置Mysql主從+zabbix監控主從狀態

配置Mysql主從+zabbix監控主從狀態

文章目錄

配置Mysql主從

需求說明

配置Mysql主從,並配置zabbix監控主從的狀態

環境說明

ip 伺服器型別
172.16.11.22 mysql-master端
172.16.11.21 mysql-slave端
172.16.11.20 zabbix伺服器

需搭建好Mysql環境,如何搭建看這裡–> 搭建Mysql

操作步驟

  • 172.16.11.22

①.修改配置檔案,新增如下兩行

[[email protected] ~]# vim /etc/my.cnf
log-bin=mysql-log                  //開啟log-bin日誌
server-id=1                       //資料庫伺服器的唯一識別符號,主伺服器的id要比從伺服器小
[[email protected]
~]# service mysqld restart

②.建立同步賬號給從資料庫同步用

mysql> create user 'relap'@'172.16.11.21' identified by 'relap123!';               //建立賬號
Query OK, 0 rows affected (0.00 sec)

mysql> grant replication slave on *.* to 'relay'@'172.16.11.21' identified by 'relay123!';              //同步授權
Query OK, 0 rows affected, 1 warning (0.00 sec)

mysql> flush privileges;                  //重新整理許可權
Query OK, 0 rows affected (0.00 sec)

mysql> show master status;                  //檢視master狀態
+------------------+----------+--------------+------------------+-------------------+
| File             | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set |
+------------------+----------+--------------+------------------+-------------------+
| mysql-log.000001 |     1102 |              |                  |                   |
+------------------+----------+--------------+------------------+-------------------+
1 row in set (0.00 sec)

  • 172.16.11.21

①.修改配置檔案,新增如下兩行

[[email protected] ~]# vim /etc/my.cnf
relay-log=mysql-relay.log
server-id=8
[[email protected] ~]# service mysqld restart

②.配置並啟動主從複製

[[email protected] ~]# mysql -uroot -plzh123!
mysql> change master to                 //這裡的master_log_pos為之前master狀態裡面的position值
    -> master_host='172.16.11.22',
    -> master_user='relay',
    -> master_password='relay123!',
    -> master_log_file='mysql-log.000001',
    -> master_log_pos=1102;
Query OK, 0 rows affected, 2 warnings (0.01 sec)       
mysql> start slave;             //啟動主從複製

mysql> show slave status \G
*************************** 1. row ***************************
               Slave_IO_State: 
                  Master_Host: 172.16.11.22
                  Master_User: relay
                  Master_Port: 3306
                Connect_Retry: 60
              Master_Log_File: mysql-log.000001
          Read_Master_Log_Pos: 1102
               Relay_Log_File: mysql-relay.000001
                Relay_Log_Pos: 4
        Relay_Master_Log_File: mysql-log.000001
             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: 1102
              Relay_Log_Space: 154
              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: NULL
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: 0
                  Master_UUID: 
             Master_Info_File: /opt/data/master.info
                    SQL_Delay: 0
          SQL_Remaining_Delay: NULL
      Slave_SQL_Running_State: 
           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: 
1 row in set (0.00 sec)


結果測試

  • 172.16.11.22
    在主伺服器上面寫入資料
mysql> create database zhangshan;
Query OK, 1 row affected (0.01 sec)

mysql> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| mysql              |
| performance_schema |
| sys                |
| zhangshan          |
+--------------------+
5 rows in set (0.01 sec)

  • 172.16.11.21
    在從伺服器上面檢視
mysql> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| mysql              |
| performance_schema |
| sys                |
| zhangshan          |
+--------------------+
5 rows in set (0.00 sec)


報錯解決

1.當在從伺服器上配置主從複製的時候配置寫錯
解決方法:
①.stop slave //停止slave主從複製
②.reset slave all //刪除主從配置
重新配置即可

2.出現Slave_IO_Running: No
可能是因為pos值或者log_file檔名不對
①.先鎖表,使主資料庫無法寫入資料,檢查錯誤,然後刪除配置重新配好再解鎖

 flush tables with read lock;            //鎖表
 unlock tables;                 //解鎖表

配置zabbix監控主從狀態

通過zabbix監控其I/O執行緒和SQL執行緒的狀態來檢視主從是否同步出現異常
這裡就不詳細搭建lamp和zabbix了,如何搭建請看下面的文件
如何搭建lamp–> 搭建lamp
如何搭建zabbix --> 搭建zabbix
如何新增監控–> 新增監控

操作步驟

  • 172.16.11.21

①.在mysql的slave端安裝zabbix-agent並配置

[[email protected] ~]# wget http://repo.zabbix.com/zabbix/4.0/rhel/7/x86_64/zabbix-agent-4.0.0-2.el7.x86_64.rpm
[[email protected] ~]# rpm -ivh zabbix-agent-4.0.0-2.el7.x86_64.rpm 
[[email protected] ~]# vim /etc/zabbix/zabbix_agentd.conf 
Server=172.16.11.20      //修改地址為服務端ip
ServerActive=172.16.11.20      //修改為服務端ip
Hostname=172.16.11.21        //後面接的名稱要與web介面配置的名字相同

②.寫監控I/O執行緒和SQL執行緒的指令碼

為了防止把密碼寫到命令裡面會出現警告,影響過濾,需要將使用者和密碼新增到配置檔案裡面

[[email protected] ~]# vim /etc/my.cnf
[client]            //把使用者和密碼新增到client裡面,可以不輸密碼登陸Mysql
user=root    
password=lzh123!


[[email protected] ~]# mkdir /scripts/
[[email protected] ~]# vim /scripts/IO.sh             //監控IO執行緒
#!/bin/bash

stat=`mysql -e 'show slave status \G' | grep -i 'running:' | sed -n '1p' | awk -F '[: ]+' '{print $3}'`

if [ $stat == Yes ];then
    echo '0'
else

    echo '1'
fi
[[email protected] ~]# vim /scripts/SQL.sh            //監控SQL執行緒
#!/bin/bash

stat=`mysql -e 'show slave status \G' | grep -i 'running:' | sed -n '2p' | awk -F '[: ]+' '{print $3}'`

if [ $stat == Yes ];then
    echo '0'
else
    echo '1'
fi

③.修改配置檔案新增指令碼路徑

[[email protected] ~]# vim /etc/zabbix/zabbix_agentd.conf 
UnsafeUserParameters=0                  //把值修改為1
UserParameter=check_io,/bin/bash /scripts/io.sh           //指令碼的路徑

zabbix的web頁面配置

新增主機
在這裡插入圖片描述

新增監控項
在這裡插入圖片描述

在這裡插入圖片描述

在這裡插入圖片描述

新增觸發器
在這裡插入圖片描述

在這裡插入圖片描述

結果測試
當停掉mysql_slave的時候,可以看到已經觸發報警

mysql> stop slave;
Query OK, 0 rows affected (0.01 sec)

在這裡插入圖片描述