首页 > 数据库 > 其他综合 >

MySQL之数据库监控(Lepus)

2019-02-27

MySQL之数据库监控(Lepus)。部署LAMP,python连接mysql数据库的接口。

部署LAMP
https://blog.51cto.com/13598811/2104461 

https://pypi.org/project/MySQL-python

python连接mysql数据库的接口

[root@wallet04 ~]# tar zxvf setuptools-0.6c11.tar.gz
[root@wallet04 ~]# cd setuptools-0.6c11
[root@wallet04 setuptools-0.6c11]# python setup.py install

[root@wallet04 ~]# which mysql_config 
/usr/local/mysql/bin/mysql_config

[root@wallet04 ~]# tar zxvf MySQL-python-1.2.3.tar.gz
[root@wallet04 ~]# cd MySQL-python-1.2.3
[root@wallet04 MySQL-python-1.2.3]# vi site.cfg
#mysql_config = /usr/local/bin/mysql_config
mysql_config = /usr/local/mysql/bin/mysql_config
[root@wallet04 MySQL-python-1.2.3]# python setup.py build 
[root@wallet04 MySQL-python-1.2.3]# python setup.py install

https://pypi.org/project/cx_Oracle

python连接oracle数据库的接口

[root@wallet04 ~]# rpm -ivh oracle-instantclient11.2-*

[root@wallet04 ~]# vim /root/.bash_profile
export ORACLE_HOME=/usr/lib/oracle/11.2/client64
export PATH=$PATH:$ORACLE_HOME/bin
export LD_LIBRARY_PATH=$ORACLE_HOME/lib:/usr/lib:/usr/local/lib
[root@wallet04 ~]# source /root/.bash_profile

[root@wallet04 ~]# vim /etc/ld.so.conf 
/usr/lib/oracle/11.2/client64/lib
[root@wallet04 ~]# ldconfig

[root@wallet04 ~]# tar zxvf cx_Oracle-5.1.3.tar.gz 
[root@wallet04 ~]# cd cx_Oracle-5.1.3
[root@wallet04 cx_Oracle-5.1.3]# python setup.py build 
[root@wallet04 cx_Oracle-5.1.3]# python setup.py install 

https://pypi.org/project/redis

python连接redis的接口

[root@wallet04 ~]# tar zxvf redis-2.10.6.tar.gz
[root@wallet04 ~]# cd redis-2.10.6
[root@wallet04 redis-2.10.6]# python setup.py install

https://pypi.org/project/pymongo

python连接mongodb的接口

[root@wallet04 ~]# tar zxvf pymongo-2.9.tar.gz
[root@wallet04 ~]# cd pymongo-2.9
[root@wallet04 pymongo-2.9]# python setup.py install

[root@wallet04 ~]# unzip lepus.zip
[root@wallet04 ~]# cd lepus/python
[root@wallet04 python]# python test_driver_mysql.py 
MySQL python drivier is ok!
[root@wallet04 python]# python test_driver_oracle.py 
Oracle python drivier is ok!
[root@wallet04 python]# python test_driver_mongodb.py
MongoDB python drivier is ok!
[root@wallet04 python]# python test_driver_redis.py 
Redis python drivier is ok!

[root@wallet04 ~]# mysql -u root -pabcd.1234
mysql> create database lepus default character set utf8;
mysql> grant select,insert,update,delete,create on lepus.* to 'lepus'@'localhost' identified by 'lepus';
mysql> flush privileges;

mysql> use lepus
mysql> source /root/lepus/sql/lepus_table.sql
mysql> source /root/lepus/sql/lepus_data.sql

[root@wallet04 ~]# cd lepus/python
[root@wallet04 python]# chmod +x install.sh
[root@wallet04 python]# ./install.sh
[note] lepus will be install on basedir: /usr/local/lepus
[note] /usr/local/lepus directory does not exist,will be created.
[note] /usr/local/lepus directory created success.
[note] wait copy files.......
[note] change script permission.
[note] create links.
[note] install complete.

[root@wallet04 ~]# cd /usr/local/lepus
[root@wallet04 lepus]# vim etc/config.ini
###监控机MySQL数据库连接地址###
[monitor_server]
host="localhost"
port=3306
user="lepus"
passwd="lepus"
dbname="lepus"

[root@wallet04 ~]# cd lepus
[root@wallet04 lepus]# cp -rf php/* /usr/local/apache/htdocs
[root@wallet04 ~]# cd /usr/local/apache/htdocs/application/config
$db['default']['hostname'] = 'localhost';
$db['default']['port']     = '3306';
$db['default']['username'] = 'lepus';
$db['default']['password'] = 'lepus';
$db['default']['database'] = 'lepus';
$db['default']['dbdriver'] = 'mysql';
$db['default']['dbprefix'] = '';
$db['default']['pconnect'] = TRUE;
$db['default']['db_debug'] = TRUE;
$db['default']['cache_on'] = FALSE;
$db['default']['cachedir'] = '';
$db['default']['char_set'] = 'utf8';
$db['default']['dbcollat'] = 'utf8_general_ci';
$db['default']['swap_pre'] = '';
$db['default']['autoinit'] = TRUE;
$db['default']['stricton'] = FALSE;

[root@wallet04 ~]# cd /usr/local/lepus
[root@wallet04 lepus]# lepus start
[root@wallet04 lepus]# lepus status
lepus server is running...

[root@wallet04 lepus]# tail -f logs/lepus.log
2018-12-20 14:04:49 [INFO] check oracle controller finished.
2018-12-20 14:04:53 [INFO] check mongodb controller started.
2018-12-20 14:04:53 [WARNING] check mongodb: not found any servers
2018-12-20 14:04:53 [INFO] check mongodb controller finished.
2018-12-20 14:04:55 [INFO] check redis controller started.
2018-12-20 14:04:55 [WARNING] check redis: not found any servers
2018-12-20 14:04:55 [INFO] check redis controller finished.
2018-12-20 14:05:02 [INFO] check os controller started.
2018-12-20 14:05:02 [WARNING] check os: not found any servers
2018-12-20 14:05:02 [INFO] check os controller finished.
2018-12-20 14:05:20 [INFO] check mysql controller started.
2018-12-20 14:05:30 [INFO] check mysql controller finished.
2018-12-20 14:05:35 [INFO] alarm controller started.
2018-12-20 14:05:35 [INFO] alarm controller finished.

慢查询日志分析
[root@wallet01 ~]# yum install -y perl-DBI perl-DBD-MySQL \
perl-Time-HiRes perl-IO-Socket-SSL perl-TermReadKey

[root@wallet01 ~]# tar zxvf percona-toolkit-3.0.12_x86_64.tar.gz
[root@wallet01 ~]# cd percona-toolkit-3.0.12
[root@wallet01 percona-toolkit-3.0.12]# perl Makefile.PL
[root@wallet01 percona-toolkit-3.0.12]# make
[root@wallet01 percona-toolkit-3.0.12]# make install

[root@wallet01 ~]# pt-heartbeat --version
pt-heartbeat 3.0.12

[root@wallet01 ~]# cd /usr/local/sbin
[root@wallet01 sbin]# vi 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
#***************************************************************#
 
#config lepus database
lepus_db_host="192.168.1.204"
lepus_db_port=3306
lepus_db_user="lepus"
lepus_db_password="lepus"
lepus_db_database="lepus"
 
#config mysql server
mysql_client=`which mysql`
mysql_host="192.168.1.201"
mysql_port=3306
mysql_user="root"
mysql_password="abcd.1234"
 
#config slowqury
slowquery_dir="/var/lib/mysql/"
slowquery_long_time=0.01
slowquery_file=`$mysql_client -h$mysql_host -P$mysql_port -u$mysql_user -p$mysql_password  -e "show 
variables like'slow_query_log_file'"|grep log|awk '{print $2}'`
pt_query_digest=`which pt-query-digest`

#config server_id
lepus_server_id=204
 
#collect slowquery log into lepus database
$pt_query_digest --user=$lepus_db_user --password=$lepus_db_password --port=$lepus_db_port --charset=utf8 
--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 slowquery log ###########
tmp_log=`$mysql_client -h$mysql_host -P$mysql_port -u$mysql_user -p$mysql_password -e "select concat
('$slowquery_dir','slowquery_',date_format(now(),'%Y%m%d%H'),'.log');"|grep log|sed -n -e '2p'`
 
#config slowquery variables
$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;"

$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 7days
cd $slowquery_dir
/usr/bin/find ./ -name 'slowquery_*' -mtime +7|xargs rm -rf ;
 
####END####
[root@wallet01 sbin]# chmod a+x lepus_slowquery.sh

[root@wallet01 ~]# crontab -l
*/5 * * * * /usr/local/sbin/lepus_slowquery.sh 1>/dev/null 2>&1 &


相关文章
最新文章
热点推荐