[root@localhost ~]# vim /etc/my.cnf [mysqld] server_id=1 innodb_file_per_table datadir=/data/mysql socket=/var/lib/mysql/mysql.sock log-bin=/data/log-bin/bin binlog_format=row ……
[root@localhost ~]# systemctl start mariadb [root@localhost ~]# systemctl enable mariadb
MariaDB [(none)]> show master logs; +------------+-----------+ | Log_name | File_size | +------------+-----------+ | bin.000001 | 307 |
MariaDB [(none)]> grant REPLICATION CLIENT on *.* to slave@'192.168.1.16' identified by 'slave';
MariaDB [(none)]> flush privileges;
[root@192 ~]# vim /etc/my.cnf [mysqld] server_id=2 read_only innodb_file_per_table datadir=/data/mysql socket=/var/lib/mysql/mysql.sock
MariaDB [(none)]> change master to master_host='192.168.1.15', master_user='slave', master_password='slave', master_log_file='bin.000001', master_log_pos=307;
MariaDB [(none)]> start slave;
ariaDB [(none)]> show slave status\G; *************************** 1. row *************************** Slave_IO_State: Waiting for master to send event Master_Host: 192.168.1.15 Master_User: slave Master_Port: 3306 Connect_Retry: 60 Master_Log_File: bin.000003 Read_Master_Log_Pos: 321 Relay_Log_File: mariadb-relay-bin.000018 Relay_Log_Pos: 531 Relay_Master_Log_File: bin.000003 Slave_IO_Running: Yes #兩個io進程啓動,主從鏈接啓動 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: 321 Relay_Log_Space: 1113 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_SSL_Crl: Master_SSL_Crlpath: Using_Gtid: No Gtid_IO_Pos: Replicate_Do_Domain_Ids: Replicate_Ignore_Domain_Ids: Parallel_Mode: conservative
root@localhost percona-zabbix-tools]# yum install zabbix-agent -y
[root@localhost ~]# vim /etc/zabbix/zabbix_agentd.conf EnableRemoteCommands=1 #容許遠程命令 Server=192.168.1.14,192.168.1.20 #必須都指定監控服務端和proxy代理服務端的ip,不然會致使監控端或proxy代理端沒法zabbix_get key值 ServerActive=192.168.1.20 #主動模式下的代理地址 Hostname=192.168.1.16 #爲方便區分主機名改成本機的ip地址 Include=/etc/zabbix/zabbix_agentd.d/*.conf #包含的子目錄 UnsafeUserParameters=1 #容許參數中的全部字符傳遞給用戶定義的參數
https://www.percona.com/downloads/percona-monitoring-plugins/LATEST/ #percona-zabbix官網地址 [root@localhost percona-zabbix-tools]# wget https://www.percona.com/downloads/percona-monitoring-plugins/percona-monitoring-plugins-1.1.8/binary/redhat/7/x86_64/percona-zabbix-templates-1.1.8-1.noarch.rpm
[root@localhost percona-zabbix-tools]# rpm -ql percona-zabbix-templates /var/lib/zabbix/percona /var/lib/zabbix/percona/scripts /var/lib/zabbix/percona/scripts/get_mysql_stats_wrapper.sh /var/lib/zabbix/percona/scripts/ss_get_mysql_stats.php /var/lib/zabbix/percona/templates /var/lib/zabbix/percona/templates/userparameter_percona_mysql.conf /var/lib/zabbix/percona/templates/zabbix_agent_template_percona_mysql_server_ht_2.0.9-sver1.1.8.xml
[root@localhost ~]#cp /var/lib/zabbix/percona/templates/userparameter_percona_mysql.conf /etc/zabbix/zabbix_agentd.d/
[root@localhost ~]# cat /etc/zabbix/zabbix_agentd.d/userparameter_percona_mysql.conf UserParameter=MySQL.Com-select,/var/lib/zabbix/percona/scripts/get_mysql_stats_wrapper.sh jx UserParameter=MySQL.Qcache-total-blocks,/var/lib/zabbix/percona/scripts/get_mysql_stats_wrapper.sh js UserParameter=MySQL.Handler-read-prev,/var/lib/zabbix/percona/scripts/get_mysql_stats_wrapper.sh mt UserParameter=MySQL.Sort-rows,/var/lib/zabbix/percona/scripts/get_mysql_stats_wrapper.sh ks UserParameter=MySQL.Qcache-free-memory,/var/lib/zabbix/percona/scripts/get_mysql_stats_wrapper.sh jm UserParameter=MySQL.pages-read,/var/lib/zabbix/percona/scripts/get_mysql_stats_wrapper.sh gu ….. UserParameter=MySQL.Key-read-requests,/var/lib/zabbix/percona/scripts/get_mysql_stats_wrapper.sh gg
[root@localhost ~]# ls /etc/zabbix/zabbix_agentd.d/ userparameter_mysql.conf userparameter_percona_mysql.conf
[root@localhost ~]# rm /etc/zabbix/zabbix_agentd.d/userparameter_mysql.conf
[root@localhost ~]#yum install php php-mysql -y
[root@localhost ~]# vim /var/lib/zabbix/percona/scripts/ss_get_mysql_stats.php.cnf <?php $mysql_user = 'root'; $mysql_pass = ''; #沒有設置密碼就爲空
[root@localhost ~]# systemctl restart zabbix-agent
[root@localhost ~]# /var/lib/zabbix/percona/scripts/get_mysql_stats_wrapper.sh gg 4
[[root@localhost ~]# cat /tmp/localhost-mysql_cacti_stats.txt gg:4 gh:2 gi:2 gj:2 gk:5 gl:2832 gm:0 gn:3 go:0 gp:0 gq:8191 gr:8028 gs:163 gt:0 gu:157 gv:6 gw:27 gx:24 gy:175 gz:43 hg:18 hh:0 hi:0 hj:0 hk:-1 hl:0 hm:0 hn:-1 ho:0 hp:0 hq:0 hr:0 hs:0 ht:836 hu:1208 hv:11 hw:0 hx:0 hy:0 hz:0 ig:0 ih:35 ii:0 ij:29 ik:11 il:17 im:431 in:1024 io:431 ip:0 iq:0 ir:1 is:0 it:0 iu:1 iv:2 iw:1 ix:151 iy:0 iz:6 jg:0 jh:0 ji:0 jj:0 jk:0 jl:1 jm:1031336 jn:0 jo:0 jp:0 jq:0 jr:0 js:1 jt:1048576 ju:8 jv:0 jw:0 jx:1 jy:0 jz:0 kg:0 kh:0 ki:0 kj:0 kk:0 kl:0 km:0 kn:0 ko:0 kp:2 kq:0 kr:0 ks:0 kt:0 ku:2 kv:0 kw:6 kx:4886 ky:2250 kz:16777216 lg:0 lh:1632681 li:1632681 lj:1269 lk:32768 ll:0 lm:0 ln:-1 lo:0 lp:0 lq:0 lr:0 ls:0 lt:0 lu:1 lv:0 lw:0 lx:0 ly:0 lz:0 mg:0 mh:0 mi:0 mj:0 mk:0 ml:2 mm:3 mn:1 mo:0 mp:0 mq:5 mr:2 ms:0 mt:0 mu:0 mv:42 mw:0 mx:0 my:0 mz:0 ng:4 nh:0 ni:0 nj:-1 nk:-1 nl:-1 nm:-1 nn:0 no:1 np:0 nq:2 nr:2217568 ns:139112 nt:645374 nu:817696 nv:333952 nw:0 nx:-1 ny:-1 nz:-1 og:0 oh:24484864 oi:134217728 oj:0 ok:0 ol:-1 om:-1 on:-1 oo:-1 op:-1 oq:-1 or:-1 os:-1 ot:-1 ou:-1 ov:-1 ow:-1 ox:-1 oy:-1 oz:-1 pg:-1 ph:-1 pi:-1 pj:-1 pk:-1 pl:-1 pm:-1 pn:-1 po:-1 pp:-1 pq:-1 pr:-1 ps:-1 pt:-1 pu:-1 pv:-1 pw:-1 px:-1 py:0 pz:-1 qg:-1 qh:0 qi:-1 qj:-1 qk:-1 ql:-1 qm:-1 qn:-1 qo:158 qp:676
root@localhost ~]# ll /tmp/localhost-mysql_cacti_stats.txt -rw-r--r-- 1 root root 1228 Jul 15 23:52 /tmp/localhost-mysql_cacti_stats.txt
root@localhost ~]#rm -rf /tmp/localhost-mysql_cacti_stats.txt
MariaDB [(none)]> create database zabbix_proxy_active character set utf8 collate utf8_bin;
MariaDB [(none)]> grant all privileges on zabbix_proxy_active.* to zabbix_proxy_active@192.168.1.20 identified by 'zabbix_proxy_active';
MariaDB [(none)]> show databases; +---------------------+ | Database | +---------------------+ | information_schema | | mysql | | performance_schema | | zabbix | | zabbix_proxy_active | +---------------------+ 5 rows in set (0.04 sec)
MariaDB [(none)]> flush privileges; Query OK, 0 rows affected (0.00 sec)
MariaDB [(none)]> show grants for zabbix_proxy_active@'192.168.1.20'; +-------------------------------------------------------------------------------------------------------------------------------+ | Grants for zabbix_proxy_active@192.168.1.20 | +-------------------------------------------------------------------------------------------------------------------------------+ | GRANT USAGE ON *.* TO 'zabbix_proxy_active'@'192.168.1.20' IDENTIFIED BY PASSWORD '*A63355661598C0599A07E41CE45344B7131CDF89' | | GRANT ALL PRIVILEGES ON `zabbix_proxy_active`.* TO 'zabbix_proxy_active'@'192.168.1.20' | +-------------------------------------------------------------------------------------------------------------------------------+
root@ubuntu1804:~# wget https://repo.zabbix.com/zabbix/4.0/debian/pool/main/z/zabbix-release/zabbix-release_4.0-2+stretch_all.deb root@ubuntu1804:~# dpkg -i zabbix-release_4.0-2+stretch_all.deb root@ubuntu1804:~# apt update
root@ubuntu1804:~# apt install zabbix-proxy-mysql -y
root@ubuntu1804:~# apt install zabbix-get -y
root@ubuntu1804:~# dpkg -l | grep zabbix ii zabbix-get 1:4.0.10-1+bionic amd64 Zabbix network monitoring solution - get ii zabbix-proxy-mysql 1:4.0.10-1+bionic amd64 Zabbix network monitoring solution - proxy (MySQL) ii zabbix-release 1:4.0-2+bionic all Zabbix official repository configuration
root@ubuntu1804:~# mysql -uzabbix_proxy_active -pzabbix_proxy_active -h192.168.1.14 Welcome to the MariaDB monitor. Commands end with ; or \g. Your MariaDB connection id is 58 Server version: 10.1.40-MariaDB-0ubuntu0.18.04.1 Ubuntu 18.04 Copyright (c) 2000, 2018, Oracle, MariaDB Corporation Ab and others. Type 'help;' or '\h' for help. Type '\c' to clear the current input statement. MariaDB [(none)]> MariaDB [(none)]> show databases; +---------------------+ | Database | +---------------------+ | information_schema | | zabbix_proxy_active | #能夠看到此數據庫 +---------------------+ 2 rows in set (0.00 sec)
root@ubuntu1804:~# zcat /usr/share/doc/zabbix-proxy-mysql/schema.sql.gz | mysql -uzabbix_proxy_active -pzabbix_proxy_active -h192.168.1.14 zabbix_proxy_active
root@ubuntu1804:~# mysql -uzabbix_proxy_active -pzabbix_proxy_active -h192.168.1.14 MariaDB [(none)]> use zabbix_proxy_active; 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 MariaDB [zabbix_proxy_active]> show tables; …… | task_acknowledge | | task_check_now | | task_close_problem | | task_remote_command | | task_remote_command_result | | timeperiods | | trends | | trends_uint | | trigger_depends | | trigger_discovery | | trigger_tag | | triggers | | users | | users_groups | | usrgrp | | valuemaps | | widget | | widget_field | +-------------------------------+ 144 rows in set (0.00 sec)
root@ubuntu1804:~# grep '^[a-Z]' /etc/zabbix/zabbix_proxy.conf ProxyMode=0 #0表示開啓主動模式 Server=192.168.1.14 #服務端的ip地址 Hostname=zabbix_proxy_active #proxy代理主機名,在web端添加此代理是必須與此處主機名相同 ListenPort=10051 #proxy監聽的端口 LogFileSize=0 #關閉日誌自動輪換 DBHost=192.168.1.14 #數據庫主機 DBName=zabbix_proxy_active #數據庫名 DBUser=zabbix_proxy_active #數據庫用戶 DBPassword=zabbix_proxy_active #數據庫密碼 ProxyLocalBuffer=24 #數據訪問服務端後,本地保存數據時間爲720小時 ProxyOfflineBuffer=24 #未提交數據到服務端,本地保存數據的時間爲720小時 ConfigFrequency=10 #間隔多久到服務端獲取監控項信息,默認3600秒 DataSenderFrequency=5 #數據發送時間間隔,默認爲1秒 CacheSize=2G #保存監控項的最大內存,默認爲8M HistoryCacheSize=2G #保存監控歷史數據的最大內存,默認爲16M Timeout=30 #監控項超時時間,單位秒 LogSlowQueries=3000 #多久的數據庫查詢會被記錄到日誌,單位毫秒
root@ubuntu1804:~# systemctl restart zabbix-proxy root@ubuntu1804:~# systemctl enable zabbix-proxy
● zabbix-proxy.service - Zabbix Proxy Loaded: loaded (/lib/systemd/system/zabbix-proxy.service; disabled; vendor preset: enabled) Active: active (running) since Tue 2019-07-16 18:06:43 CST; 2h 36min ago Process: 4514 ExecStop=/bin/kill -SIGTERM $MAINPID (code=exited, status=0/SUCCESS) Process: 4515 ExecStart=/usr/sbin/zabbix_proxy -c $CONFFILE (code=exited, status=0/SUCCESS) Main PID: 4527 (zabbix_proxy) Tasks: 25 (limit: 2293) CGroup: /system.slice/zabbix-proxy.service ├─4527 /usr/sbin/zabbix_proxy -c /etc/zabbix/zabbix_proxy.conf ├─4531 /usr/sbin/zabbix_proxy: configuration syncer [synced config 4069 bytes in 0.056334 sec, idle 3600 sec] ├─4532 /usr/sbin/zabbix_proxy: heartbeat sender [sending heartbeat message success in 0.001186 sec, idle 60 sec] ├─4533 /usr/sbin/zabbix_proxy: data sender [sent 0 values in 0.014112 sec, idle 1 sec] ├─4534 /usr/sbin/zabbix_proxy: housekeeper [deleted 1454 records in 0.135417 sec, idle for 1 hour(s)] ├─4535 /usr/sbin/zabbix_proxy: http poller #1 [got 0 values in 0.003211 sec, idle 5 sec] ……
root@ubuntu1804:~# zabbix_get -s 192.168.1.16 -p 10050 -k MySQL.Key-read-requests
4
root@ubuntu1804:~# zabbix_get -s 192.168.1.16 -p 10050 -k MySQL.Qcache-free-memory
1031336php
[root@localhost ~]# ll /tmp/localhost-mysql_cacti_stats.txt -rw-rw-r-- 1 zabbix zabbix 1235 Jul 16 01:00 /tmp/localhost-mysql_cacti_stats.txt
[root@localhost ~]# vim /etc/zabbix/zabbix_agentd.d/mysql_monitor.sh #!/bin/bash master_slave_check(){ num1=`mysql -uroot -e "show slave status\G" | grep "Slave_IO_Running" | awk -F":" '{print $2}'` num2=`mysql -uroot -e "show slave status\G" | grep "Slave_SQL_Running" | awk -F":" '{print $2}'` if test $num1 == "Yes" && test $num2 == "Yes";then echo 60 else echo 120 fi } mysql_sync_delay(){ num=`mysql -uroot -e "show slave status\G" | grep "Seconds_Behind_Master" | awk -F":" '{print $2}'` echo $num } menu(){ case $1 in master_slave_check) master_slave_check ;; mysql_sync_delay) mysql_sync_delay ;; esac } menu $1
[root@localhost ~]# vim /etc/zabbix/zabbix_agentd.d/mysql-master-slave.conf UserParameter=mysql_test[*],/etc/zabbix/zabbix_agentd.d/mysql_monitor.sh $1
[root@localhost ~]# chmod a+x mysql_monitor.sh
[root@localhost ~]# systemctl restart zabbix-agent
root@ubuntu1804:~# zabbix_get -s 192.168.1.16 -p 10050 -k "mysql_test[mysql_sync_delay]" 0 root@ubuntu1804:~# zabbix_get -s 192.168.1.16 -p 10050 -k "mysql_test[master_slave_check]" 60
root@ubuntu1804:~# zabbix_get -s 192.168.1.16 -p 10050 -k "mysql_test[mysql_sync_delay]" 0 root@ubuntu1804:~# zabbix_get -s 192.168.1.16 -p 10050 -k "mysql_test[master_slave_check]" 60
root@ubuntu1804:~# zabbix_get -s 192.168.1.16 -p 10050 -k "mysql_test[master_slave_check]" 120