在使用lepus3.7監(jiān)控MySQL數(shù)據(jù)庫的時(shí)候,碰到了以下幾個(gè)問題,本博客給出了這些問題產(chǎn)生的原因,以及相應(yīng)的解決辦法。
1. 問題1:php頁面無法連接數(shù)據(jù)庫
直接使用php程序執(zhí)行php文件,可以連接mysql,但是在httpd中同樣的php頁面無法連接mysql。
lepus的web程序(PHP代碼)無法連接數(shù)據(jù)庫時(shí),web界面上什么操作也無法繼續(xù)。
為此編寫了最簡單的PDO連接測(cè)試代碼:
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程序直接執(zhí)行php文件:
[x@coe2coe lepus]$ php mysql.phpresult:2018-09-27 00:03:44
通過瀏覽器訪問這個(gè)頁面:
FAILED:SQLSTATE[HY000] [2003] Can't connect to MySQL server on '11.1.1.11' (13)
lepus的web程序給出的錯(cuò)誤提示信息更加模糊。
原因:
通過一番baidu之后,終于看到了一個(gè)比較靠譜的分析。
Linux(CentOS7)的selinux安全機(jī)制禁止了httpd中的模塊訪問網(wǎng)絡(luò)。
[x@coe2coe lepus]$ sudo getsebool -a |grep httpdhttpd_anon_write --> offhttpd_builtin_scripting --> onhttpd_can_check_spam --> offhttpd_can_connect_ftp --> offhttpd_can_connect_ldap --> offhttpd_can_connect_mythtv --> offhttpd_can_connect_zabbix --> offhttpd_can_network_connect --> offhttpd_can_network_connect_cobbler --> offhttpd_can_network_connect_db --> offhttpd_can_network_memcache --> offhttpd_can_network_relay --> offhttpd_can_sendmail --> offhttpd_dbus_avahi --> offhttpd_dbus_sssd --> offhttpd_dontaudit_search_dirs --> offhttpd_enable_cgi --> onhttpd_enable_ftp_server --> offhttpd_enable_homedirs --> offhttpd_execmem --> offhttpd_graceful_shutdown --> onhttpd_manage_ipa --> offhttpd_mod_auth_ntlm_winbind --> offhttpd_mod_auth_pam --> offhttpd_read_user_content --> offhttpd_run_ipa --> offhttpd_run_preupgrade --> offhttpd_run_stickshift --> offhttpd_serve_cobbler_files --> offhttpd_setrlimit --> offhttpd_ssi_exec --> offhttpd_sys_script_anon_write --> offhttpd_tmp_exec --> offhttpd_tty_comm --> offhttpd_unified --> offhttpd_use_cifs --> offhttpd_use_fusefs --> offhttpd_use_gpg --> offhttpd_use_nfs --> offhttpd_use_openstack --> offhttpd_use_sasl --> offhttpd_verify_dns --> off
解決辦法:
臨時(shí)辦法:臨時(shí)禁用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=enforcingSELINUX=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
驗(yàn)證:
再次在瀏覽器中訪問這個(gè)php頁面:
result:2018-09-27 00:09:26
2. 問題2:lepus日志中出現(xiàn)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后端監(jiān)控程序?qū)懙膌og。
默認(rèn)情況下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_ZERO_IN_DATE,NO_ZERO_DATE,ERROR_FOR_DIVISION_BY_ZERO,NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION
3. 問題3:復(fù)制監(jiān)控查詢不到數(shù)據(jù)。
沒有查詢到數(shù)據(jù).
解決辦法:
show_compatibility_56=1
4. 問題4:表空間分析沒有數(shù)據(jù)。
5. 問題5:慢查詢沒有數(shù)據(jù)。
前提:
MySQL的my.cnf配置文件中已經(jīng)配置了慢查詢?nèi)罩尽?/p>
slow_query_log=1long_query_time=10log_slow_admin_statements=1log_slow_slave_statements=1
原因:
1.lepus慢查詢分析基于pecona-toolkit工具包中的pt-query-digest程序。需要先安裝這個(gè)工具包。
2. pt-query-digest程序與lepus3.7建的表有點(diǎn)沖突。
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: 0mysql> 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。這個(gè)腳本需要在每個(gè)MySQL服務(wù)器上部署,因此如果要監(jiān)控的MySQL很多,會(huì)比較容易出錯(cuò)。
lepus_server_id這個(gè)參數(shù)很重要。下面的代碼可以自動(dòng)取得這個(gè)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)同一臺(tái)機(jī)器上如果部署有多個(gè)MySQL服務(wù)實(shí)例時(shí),應(yīng)該只需要一個(gè)定時(shí)任務(wù)即可,在另一腳本中同時(shí)對(duì)本機(jī)的多個(gè)MySQL服務(wù)實(shí)例進(jìn)行檢查。
這個(gè)總的定時(shí)腳本如下,測(cè)試時(shí)開啟了6個(gè)MySQL實(shí)例,端口依次為:3306 3307 3308 3406 3407 3408.其中3306和3406為MASTER,其它為SLAVE。在這個(gè)總的腳本中對(duì)每個(gè)實(shí)例調(diào)用lepus_slowquery.sh。
[x@coe2coe mysql]$ cat slowquery.sh################################################################### FileName :slowquery.sh# Author : coe2coe@qq.com# Created :2018-09-27# Description :http://www.cnblogs.com/coe2coe/##################################################################!/bin/bashports=(3306 3307 3308 3406 3407 3408)i=0while [ $i -lt ${#ports[*]} ]do port=${ports[$i]} echo -e "/lepus_slowquery.sh $port" ./lepus_slowquery.sh $port let i=i+1done(3)原始的lepus_slowquery.sh會(huì)去修改MySQL的全局配置參數(shù),個(gè)人認(rèn)為不需要修改,這兩個(gè)配置還是應(yīng)該按照MySQL服務(wù)器的my.cnf文件中配置的為準(zhǔn),不應(yīng)該因?yàn)椴渴鹆艘粋€(gè)lepus監(jiān)控系統(tǒng)就隨意的修改這個(gè)參數(shù)。因此直接注釋掉了最后面的幾行代碼。
long_query_timeslow_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=$1id=$2if [ "$port" == "" ] || [ $port -lt 1 ] then echo -e "invalid argument port" exit 1fiecho -e "mysql port is :{$port} "#config lepus database serverlepus_db_host="11.1.1.11"lepus_db_port=3306lepus_db_user="lepus_monitor"lepus_db_password="XXXXXXXXXX"lepus_db_database="lepus"#config mysql servermysql_client="/usr/bin/mysql"mysql_host="11.1.1.11"mysql_port=$portmysql_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,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}')if [ "$id" == "" ] || [ $id -lt 1 ]then echo -e "invalid argument id" exit 2fiecho -e "mysql lepus id is :{$id}"#config slowquryslowquery_dir="/tmp/"slowquery_long_time=1slowquery_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_idlepus_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,D=$lepus_db_database,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中的數(shù)據(jù)
在mysql_slow_query_review表中記錄了慢查詢,但是在lepus web界面上沒有數(shù)據(jù)。
執(zhí)行:select sleep(14)有時(shí)候無法在web界面查詢到。
原因:有時(shí)候pt-query-digest產(chǎn)生的結(jié)果中db_max為NULL,導(dǎo)致查詢不出來。
這個(gè)字段安裝的原始數(shù)據(jù)庫是NOT NULL,但是在NOT NULL的情況下pt-query-digest有時(shí)會(huì)插入NULL數(shù)據(jù),導(dǎo)致報(bào)錯(cuò)。所以修改為了NULL。
修改為NULL后,web界面中查詢時(shí)使用的PHP程序的SQL語句有問題,沒有考慮NULL的情況,導(dǎo)致查詢不出來這部分?jǐn)?shù)據(jù)。
解決辦法:
臨時(shí)打開general_log這個(gè)全局參數(shù),再做web查詢慢日志,就可以很快找到這個(gè)SQL語句,再根據(jù)這個(gè)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:主機(jī)監(jiān)控中的三項(xiàng)都沒有數(shù)據(jù)。
原因:監(jiān)控主機(jī)以及被監(jiān)控主機(jī)上沒有安裝snmpd,snmptrapd。
解決辦法:
在所有主機(jī)上安裝snmpd和snmptrapd。
軟件包:
x@coe2coe snmp]$ ls net-snmp*net-snmp-5.7.2-32.el7.x86_64.rpmnet-snmp-agent-libs-5.7.2-32.el7.x86_64.rpmnet-snmp-devel-5.7.2-32.el7.x86_64.rpmnet-snmp-libs-5.7.2-32.el7.x86_64.rpmnet-snmp-perl-5.7.2-32.el7.x86_64.rpmnet-snmp-python-5.7.2-32.el7.x86_64.rpmnet-snmp-sysvinit-5.7.2-32.el7.x86_64.rpmnet-snmp-utils-5.7.2-32.el7.x86_64.rpm
CentOS7-everything-xxx.iso上有這些軟件包。
安裝完畢后啟動(dòng)snmpd和snmptrapd服務(wù)。
總結(jié)
以上所述是小編給大家介紹的MySQL數(shù)據(jù)庫監(jiān)控軟件lepus使用問題以及解決辦法,希望對(duì)大家有所幫助,如果大家有任何疑問請(qǐng)給我留言,小編會(huì)及時(shí)回復(fù)大家的。在此也非常感謝大家對(duì)VeVb武林網(wǎng)網(wǎng)站的支持!
新聞熱點(diǎn)
疑難解答
圖片精選