1. 程式人生 > 資料庫 >MySQL資料庫監控軟體lepus使用問題以及解決辦法

MySQL資料庫監控軟體lepus使用問題以及解決辦法

在使用lepus3.7監控MySQL資料庫的時候,碰到了以下幾個問題,本部落格給出了這些問題產生的原因,以及相應的解決辦法。

1.問題1:php頁面無法連線資料庫

直接使用php程式執行php檔案,可以連線mysql,但是在httpd中同樣的php頁面無法連線mysql。

lepus的web程式(PHP程式碼)無法連線資料庫時,web介面上什麼操作也無法繼續。

為此編寫了最簡單的PDO連線測試程式碼:

php程式碼如下:

[x@coe2coe lepus]$ cat mysql.php
<?php
 try{
 #$dsn="mysql:host=127.0.0.1;dbname=lepus;";
 $dsn="mysql:host=11.1.1.11;dbname=lepus;";
 $user="coe2coe";
 $pwd="XXXXXXXXXX";
 $sql="select now() as a";
 $dbh=new PDO($dsn,$user,$pwd);
 $stmt=$dbh->prepare($sql);
 $stmt->execute();
 $row=$stmt->fetch(PDO::FETCH_ASSOC);
 echo "result:".$row['a'];
 }
 catch(PDOException $e) {
 echo "FAILED:".$e->getMessage();
 }
?>

php程式直接執行php檔案:

[x@coe2coe lepus]$ php mysql.php
result:2018-09-27 00:03:44

通過瀏覽器訪問這個頁面:

FAILED:SQLSTATE[HY000] [2003] Can't connect to MySQL server on '11.1.1.11' (13)

lepus的web程式給出的錯誤提示資訊更加模糊。

原因:

通過一番baidu之後,終於看到了一個比較靠譜的分析。

Linux(CentOS7)的selinux安全機制禁止了httpd中的模組訪問網路。

[x@coe2coe lepus]$ sudo getsebool -a |grep httpd
httpd_anon_write --> off
httpd_builtin_scripting --> on
httpd_can_check_spam --> off
httpd_can_connect_ftp --> off
httpd_can_connect_ldap --> off
httpd_can_connect_mythtv --> off
httpd_can_connect_zabbix --> off
httpd_can_network_connect --> off
httpd_can_network_connect_cobbler --> off
httpd_can_network_connect_db --> off
httpd_can_network_memcache --> off
httpd_can_network_relay --> off
httpd_can_sendmail --> off
httpd_dbus_avahi --> off
httpd_dbus_sssd --> off
httpd_dontaudit_search_dirs --> off
httpd_enable_cgi --> on
httpd_enable_ftp_server --> off
httpd_enable_homedirs --> off
httpd_execmem --> off
httpd_graceful_shutdown --> on
httpd_manage_ipa --> off
httpd_mod_auth_ntlm_winbind --> off
httpd_mod_auth_pam --> off
httpd_read_user_content --> off
httpd_run_ipa --> off
httpd_run_preupgrade --> off
httpd_run_stickshift --> off
httpd_serve_cobbler_files --> off
httpd_setrlimit --> off
httpd_ssi_exec --> off
httpd_sys_script_anon_write --> off
httpd_tmp_exec --> off
httpd_tty_comm --> off
httpd_unified --> off
httpd_use_cifs --> off
httpd_use_fusefs --> off
httpd_use_gpg --> off
httpd_use_nfs --> off
httpd_use_openstack --> off
httpd_use_sasl --> off
httpd_verify_dns --> off

解決辦法:

臨時辦法:臨時禁用SELINUX。

[x@coe2coe lepus]$ sudo setenforce 0

永久辦法:修改selinux配置檔案,禁用SELINUX。

[x@coe2coe lepus]$ cat /etc/selinux/config
# This file controls the state of SELinux on the system.
# SELINUX= can take one of these three values:
#  enforcing - SELinux security policy is enforced.
#  permissive - SELinux prints warnings instead of enforcing.
#  disabled - No SELinux policy is loaded.
#SELINUX=enforcing
SELINUX=disabled
# SELINUXTYPE= can take one of three two values:
#  targeted - Targeted processes are protected,#  minimum - Modification of targeted policy. Only selected processes are protected. 
#  mls - Multi Level Security protection.
SELINUXTYPE=targeted

驗證:

再次在瀏覽器中訪問這個php頁面:

result:2018-09-27 00:09:26

2.問題2:lepus日誌中出現group by警告。

2018-09-27 01:12:41 [WARNING] check mysql 11.1.1.11:3408 failure: 1055 Expression #2 of SELECT list is not in GROUP BY clause and contains nonaggregated column 'information_schema.processlist.USER' which is not functionally dependent on columns in GROUP BY clause; this is incompatible with sql_mode=only_full_group_by

原因:

這是lepus後端監控程式寫的log。

預設情況下sql_mode包含ONLY_FULL_GROUP_BY。

mysql> select @@sql_mode;
+-------------------------------------------------------------------------------------------------------------------------------------------+
| @@sql_mode                                |
+-------------------------------------------------------------------------------------------------------------------------------------------+
| ONLY_FULL_GROUP_BY,STRICT_TRANS_TABLES,NO_ZERO_IN_DATE,NO_ZERO_DATE,ERROR_FOR_DIVISION_BY_ZERO,NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION |
+-------------------------------------------------------------------------------------------------------------------------------------------+
1 row in set (0.01 sec)

解決辦法:

去掉ONLY_FULL_GROUP_BY。

sql_mode=STRICT_TRANS_TABLES,NO_ENGINE_SUBSTITUTION

3.問題3:複製監控查詢不到資料。

沒有查詢到資料.

解決辦法:

show_compatibility_56=1

4.問題4:表空間分析沒有資料。

5.問題5:慢查詢沒有資料。

前提:

MySQL的my.cnf配置檔案中已經配置了慢查詢日誌。

slow_query_log=1

long_query_time=10

log_slow_admin_statements=1

log_slow_slave_statements=1

原因:

1.lepus慢查詢分析基於pecona-toolkit工具包中的pt-query-digest程式。需要先安裝這個工具包。

2.pt-query-digest程式與lepus3.7建的表有點衝突。

Pipeline process 5 (iteration) caused an error: DBD::mysql::st execute failed: Data truncated for column 'checksum' at row 1 [for Statement "REPLACE INTO `lepus`.`mysql_slow_query_review_history`(`checksum`,`sample`,`serverid_max`,`db_max`,`user_max`,`ts_min`,
.....
Terminating pipeline because process 4 (iteration) caused too many errors.

修改mysql_slow_query_review:

mysql> alter table mysql_slow_query_review modify checksum varchar(100) not null ;
Query OK,0 rows affected (0.03 sec)
Records: 0 Duplicates: 0 Warnings: 0
修改mysql_slow_query_review_history:
mysql> alter table mysql_slow_query_review_history modify checksum varchar(100) not null;
Query OK,0 rows affected (0.02 sec)
Records: 0 Duplicates: 0 Warnings: 0
mysql> alter table mysql_slow_query_review_history modify serverid_max smallint(4) null;
Query OK,0 rows affected (0.02 sec)
Records: 0 Duplicates: 0 Warnings: 0

修改指令碼:

原始的lepus_slowquery.sh檔案存在一些問題。

(1)需要人工指定lepus_server_id。這個指令碼需要在每個MySQL伺服器上部署,因此如果要監控的MySQL很多,會比較容易出錯。

lepus_server_id這個引數很重要。下面的程式碼可以自動取得這個id。

id=$( $mysql_client -h$lepus_db_host -P$lepus_db_port -u$lepus_db_user -p$lepus_db_password -e "select id,host,port from $lepus_db_database.db_servers_mysql where host='$mysql_host' and port=$mysql_port\G" 2>/dev/null |grep "id:" |awk -F": " '{print $2}')

(2)同一臺機器上如果部署有多個MySQL服務例項時,應該只需要一個定時任務即可,在另一指令碼中同時對本機的多個MySQL服務例項進行檢查。

這個總的定時指令碼如下,測試時開啟了6個MySQL例項,埠依次為:3306 3307 3308 3406 3407 3408.其中3306和3406為MASTER,其它為SLAVE。在這個總的指令碼中對每個例項呼叫lepus_slowquery.sh。

[x@coe2coe mysql]$ cat slowquery.sh
##################################################################
# FileName :slowquery.sh
# Author  : [email protected]
# Created  :2018-09-27
# Description :http://www.cnblogs.com/coe2coe/
#################################################################
#!/bin/bash
ports=(3306 3307 3308 3406 3407 3408)
i=0
while [ $i -lt ${#ports[*]} ]
do
 port=${ports[$i]}
 echo -e "/lepus_slowquery.sh $port"
 ./lepus_slowquery.sh $port 
 let i=i+1
done

(3)原始的lepus_slowquery.sh會去修改MySQL的全域性配置引數,個人認為不需要修改,這兩個配置還是應該按照MySQL伺服器的my.cnf檔案中配置的為準,不應該因為部署了一個lepus監控系統就隨意的修改這個引數。因此直接註釋掉了最後面的幾行程式碼。

long_query_time
slow_query_log_file

修改後的完整的lepus_slowquery.sh檔案如下:

[x@coe2coe mysql]$ cat lepus_slowquery.sh
#!/bin/bash
#****************************************************************#
# ScriptName: /usr/local/sbin/lepus_slowquery.sh
# Create Date: 2014-03-25 10:01
# Modify Date: 2014-03-25 10:01
#***************************************************************#
port=$1
id=$2
if [ "$port" == "" ] || [ $port -lt 1 ] 
then
 echo -e "invalid argument port"
 exit 1
fi
echo -e "mysql port is :{$port} "
#config lepus database server
lepus_db_host="11.1.1.11"
lepus_db_port=3306
lepus_db_user="lepus_monitor"
lepus_db_password="XXXXXXXXXX"
lepus_db_database="lepus"
#config mysql server
mysql_client="/usr/bin/mysql"
mysql_host="11.1.1.11"
mysql_port=$port
mysql_user="lepus_monitor"
mysql_password="XXXXXXXXXX"
id=$( $mysql_client -h$lepus_db_host -P$lepus_db_port -u$lepus_db_user -p$lepus_db_password -e "select id,port from $lepus_db_database.db_servers_mysql where host='$mysql_host' and port=$mysql_port\G" 2>/dev/null |grep "id:" |awk -F": " '{print $2}')
if [ "$id" == "" ] || [ $id -lt 1 ]
then
 echo -e "invalid argument id"
 exit 2
fi
echo -e "mysql lepus id is :{$id}"
#config slowqury
slowquery_dir="/tmp/"
slowquery_long_time=1
slowquery_file=`$mysql_client -h$mysql_host -P$mysql_port -u$mysql_user -p$mysql_password -e "show variables like 'slow_query_log_file'" 2>/dev/null |grep log|awk '{print $2}'`
pt_query_digest="/usr/bin/pt-query-digest"
#config server_id
lepus_server_id=$id
#collect mysql slowquery log into lepus database
$pt_query_digest --user=$lepus_db_user --password=$lepus_db_password --port=$lepus_db_port --review h=$lepus_db_host,D=$lepus_db_database,t=mysql_slow_query_review --history h=$lepus_db_host,t=mysql_slow_query_review_history --no-report --limit=100% --filter=" \$event->{add_column} = length(\$event->{arg}) and \$event->{serverid}=$lepus_server_id " $slowquery_file > /tmp/lepus_slowquery.log
##### set a new slow query log ###########
#tmp_log=`$mysql_client -h$mysql_host -P$mysql_port -u$mysql_user -p$mysql_password -e "select concat('$slowquery_dir','slowquery_','$port','_',date_format(now(),'%Y%m%d%H'),'.log');" 2>/dev/null |grep log|sed -n -e '2p'`
#config mysql slowquery
#$mysql_client -h$mysql_host -P$mysql_port -u$mysql_user -p$mysql_password -e "set global slow_query_log=1;set global long_query_time=$slowquery_long_time;" 2>/dev/null
#$mysql_client -h$mysql_host -P$mysql_port -u$mysql_user -p$mysql_password -e "set global slow_query_log_file = '$tmp_log'; "
#delete log before 7 days
#cd $slowquery_dir
#/usr/bin/find ./ -name 'slowquery_*' -mtime +7|xargs rm -rf ;
####END####

6.問題6:web慢查詢查詢不到lepus中的資料

在mysql_slow_query_review表中記錄了慢查詢,但是在lepus web介面上沒有資料。

執行:select sleep(14)有時候無法在web介面查詢到。

原因:有時候pt-query-digest產生的結果中db_max為NULL,導致查詢不出來。

這個欄位安裝的原始資料庫是NOT NULL,但是在NOT NULL的情況下pt-query-digest有時會插入NULL資料,導致報錯。所以修改為了NULL。

修改為NULL後,web介面中查詢時使用的PHP程式的SQL語句有問題,沒有考慮NULL的情況,導致查詢不出來這部分資料。

解決辦法:

臨時開啟general_log這個全域性引數,再做web查詢慢日誌,就可以很快找到這個SQL語句,再根據這個SQL語句就可以找到有問題的PHP程式碼。

將 application/controllers/lp_mysql.php中的以下語句註釋掉即可。

修改前:

$this->db->where( "b.db_max !=",'information_schema'");

修改後:

//$this->db->where( "b.db_max !=",'information_schema'");

7.問題7:主機監控中的三項都沒有資料。

原因:監控主機以及被監控主機上沒有安裝snmpd,snmptrapd。

解決辦法:

在所有主機上安裝snmpd和snmptrapd。

軟體包:

x@coe2coe snmp]$ ls net-snmp*

net-snmp-5.7.2-32.el7.x86_64.rpm

net-snmp-agent-libs-5.7.2-32.el7.x86_64.rpm

net-snmp-devel-5.7.2-32.el7.x86_64.rpm

net-snmp-libs-5.7.2-32.el7.x86_64.rpm

net-snmp-perl-5.7.2-32.el7.x86_64.rpm

net-snmp-python-5.7.2-32.el7.x86_64.rpm

net-snmp-sysvinit-5.7.2-32.el7.x86_64.rpm

net-snmp-utils-5.7.2-32.el7.x86_64.rpm

CentOS7-everything-xxx.iso上有這些軟體包。

安裝完畢後啟動snmpd和snmptrapd服務。

總結

以上所述是小編給大家介紹的MySQL資料庫監控軟體lepus使用問題以及解決辦法,希望對大家有所幫助,如果大家有任何疑問請給我留言,小編會及時回覆大家的。在此也非常感謝大家對我們網站的支援!