在使用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_ZERO_IN_DATE,NO_ZERO_DATE,ERROR_FOR_DIVISION_BY_ZERO,NO_AUTO_CREATE_USER,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 : coe2coe@qq.com
# 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,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 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,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中的數據
在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使用問題以及解決辦法,希望對大家有所幫助,如果大家有任何疑問請給我留言,小編會及時回復大家的。在此也非常感謝大家對腳本之家網站的支持!
您可能感興趣的文章:- 詳解MySQL監控工具 mysql-monitor
- 詳解MySQL 表中非主鍵列溢出情況監控
- 使用Grafana+Prometheus監控mysql服務性能
- zabbix監控Nginx/Tomcat/MySQL的詳細教程
- mysql索引使用率監控技巧(值得收藏!)
- 關于對mysql語句進行監控的方法詳解
- zabbix監控MySQL主從狀態的方法詳解
- 利用Prometheus與Grafana對Mysql服務器的性能監控詳解
- MySQL實時監控工具orztop的使用介紹
- 實戰模擬監控MySQL服務shell腳本小結
- 安裝配置Zabbix來監控MySQL的基本教程
- 簡述mysql監控組復制