MySQL监控系统MySQL MTOP的搭建 - GoogSQL - 博客园


本站和网页 https://www.cnblogs.com/xuanzhi201111/p/4172604.html 的作者无关,不对其内容负责。快照谨为网络故障时之索引,不代表被搜索网站的即时页面。

MySQL监控系统MySQL MTOP的搭建 - GoogSQL - 博客园
首页
新闻
博问
专区
闪存
班级
我的博客
我的园子
账号设置
简洁模式 ...
退出登录
注册
登录
陆炫志
博客园
首页
新随笔
联系
订阅
管理
MySQL监控系统MySQL MTOP的搭建
MySQLMTOP是一个由Python+PHP开发的MySQL企业级监控系统。系统由Python实现多进程数据采集和告警,PHP实现WEB展示和管理。最重要是MySQL服务器无需安装任何Agent,只需在监控WEB界面配置相关数据库信息
功能非常强大:
可对上百台MySQL数据库的状态、连接数、QTS、TPS、数据库流量、复制、性能等进行时时监控
能在数据库偏离设定的正常运行阀值(如连接异常,复制异常,复制延迟) 时发送告警邮件通知到DBA进行处理
对历史数据归档,通过图表展示出数据库近期状态,以便DBA和开发人员能对遇到的问题进行分析和诊断
系统主要由仪表盘、状态监控、进程监控、复制监控、慢查询分析、性能图表、工具组件、告警事件、系统资源等子系统组成,每个子系统负责完成特定信息的采集和展示
一、系统安装需求
MySQL MTOP由PHP和Python开发,所以监控机需要安装PHP运行环境和Python环境。需要的核心包如下:
1.MySQL 5.0及以上(用来存储监控系统采集的数据)
2.Apache 2.2及以上 (WEB服务器运行服务器)
3.PHP 5.3以上 (WEB界面)
4.Python2 (推荐2.7版本,其他版本未做测试,执行数据采集和报警任务)
5.MySQLdb for python (Python连接MySQl的接口)
二、 安装必备的开发包
[root src]$ yum -y install ntp vim-enhanced gcc gcc-c++ flex bison autoconf automake bzip2-devel \
> ncurses-devel zlib-devel libjpeg-devel libpng-devel libtiff-devel freetype-devel libXpm-devel \
> gettext-devel pam-devel libtool libtool-ltdl openssl openssl-devel fontconfig-devel \
> libxml2-devel curl-devel libicu libicu-devel libmcrypt libmcrypt-devel libmhash libmhash-devel
三、同步系统时间:
vim /etc/ntp.conf //添加下面三行内容
然后同步时间,重启ntp服务,操作如下:
[root data]$ service ntpd stop
关闭 ntpd: [失败]
[root data]$ ntpdate cn.pool.ntp.org
18 Dec 15:09:59 ntpdate[11495]: step time server 119.57.127.125 offset -28806.680250 sec
[root data]$ date
2014年 12月 18日 星期四 15:10:03 CST
[root data]$ service ntpd start
正在启动 ntpd: [确定]
[root data]$ chkconfig ntpd on
四、进制apache安装,版本要2.2及以上
[root data]$cd /usr/local/src
[root src]$wget http://archive.apache.org/dist/httpd/httpd-2.2.19.tar.gz
[root src]$tar xvf httpd-2.2.19.tar.gz
[root src]$ cd httpd-2.2.19
./configure --prefix=/usr/local/apache \
--enable-so --enable-rewrite \
--enable-mods-shared=most --sysconfdir=/etc/httpd
[root httpd-2.2.19]$ make && make install
安装完后启动,启动报Could not ....,修改下ServerName就不报这样的错了
[root httpd-2.2.19]$ /usr/local/apache/bin/apachectl start
httpd: Could not reliably determine the server's fully qualified domain name, using localhost.localdomain for ServerName
[root httpd-2.2.19]$ netstat -tnlp |grep httpd
tcp 0 0 :::80 :::* LISTEN 24810/httpd
[root httpd-2.2.19]$ cp /usr/local/webserver/apache/bin/apachectl /etc/init.d/httpd
[root httpd-2.2.19]$ cp /usr/local/apache/bin/apachectl /etc/init.d/httpd
[root httpd-2.2.19]$ grep "ServerName" /etc/httpd/httpd.conf
# ServerName gives the name and port that the server uses to identify itself.
ServerName 127.0.0.1:80
[root httpd-2.2.19]$ service httpd restart
添加开机启动:
[root httpd-2.2.19]$chkconfig --add httpd
[root httpd-2.2.19]$chkconfig httpd on
MySQL我已经安装,在这就不演示这个过程了,可以到官网下载5.5版本的MySQL,然后用我写的脚本安装http://www.cnblogs.com/xuanzhi201111/p/4119065.html
五、进行PHP安装
安装php前安装,否则可能出现以下错误:
configure: error: mcrypt.h not found. Please reinstall libmcrypt
[root src]$wget http://softlayer.dl.sourceforge.net/sourceforge/mcrypt/libmcrypt-2.5.8.tar.gz
[root src]$ tar zxf libmcrypt-2.5.8.tar.gz
[root src]$ cd libmcrypt-2.5.8
[root libmcrypt-2.5.8]$ ./configure --prefix=/usr/local && make && make install
下载PHP并安装,版本要5.3以上,下面要注意的是,php安装涉及了你安装apache和mysql的路径,这个要特别注意的:
[root src]$ wget http://cn2.php.net/distributions/php-5.5.19.tar.gz
[root src]$ tar zxf php-5.5.19.tar.gz
[root src]$ cd php-5.5.19
[root php-5.5.19]$./configure --prefix=/usr/local/php \
--mandir=/usr/local/share/man \
--infodir=/usr/local/share/info \
--with-apxs2=/usr/local/apache/bin/apxs \
--enable-cgi --with-mysql=/usr/local/mysql-5.6.10/ \
--with-config-file-path=/usr/local/php/etc \
--with-pdo-mysql=/usr/local/mysql-5.6.10 \
--with-mysqli=/usr/local/mysql-5.6.10/bin/mysql_config \
--enable-zip --enable-sqlite-utf8 -enable-sockets \
--enable-soap --enable-pcntl --enable-mbstring \
--enable-intl --enable-calendar --enable-bcmath \
--enable-exif --with-mcrypt --with-mhash --with-gd \
--with-png-dir --with-jpeg-dir --with-freetype-dir \
--with-libxml-dir --with-curl --with-curlwrappers \
--with-zlib --with-openssl --with-kerberos=shared \
--with-gettext=shared --with-xmlrpc=shared[root php-5.5.19]$make && make install
拷贝配置文件:
[root php-5.5.19]$ cp php.ini-production /usr/local/php/etc/php.ini
然后修改apache的配置文件:
[root php-5.5.19]$ vim /usr/local/apache/conf/httpd.conf
添加以下内容:
查找AddType application/x-gzip .gz .tgz,在该行下面添加
AddType application/x-httpd-php .php
查找DirectoryIndex index.html 把该行修改成
DirectoryIndex index.html index.htm index.php
重启apache,在/usr/local/apache/htdocs/下vim index.php,操作如下:
[root htdocs]$ service httpd restart
[root htdocs]$ pwd
/usr/local/apache/htdocs
[root htdocs]$ cat index.php
<?php
phpinfo();
?>
在防火墙里添加80端口,并重启,然后请问index.php:http://ip/index.php,如果得到下图,意味着apache整合php成功了
六、进行python安装,推荐2.7版本,其他版本未做测试,执行数据采集和报警任务
[root src]$ wget http://www.python.org/ftp/python/2.7.2/Python-2.7.2.tar.bz2
[root src]$ tar jxf Python-2.7.2.tar.bz2
[root src]$ cd Python-2.7.2
[root Python-2.7.2]$ ./configure --prefix=/usr/local/Python2.7 --enable-shared make && make install
[root Python-2.7.2]$ mv /usr/bin/python /usr/bin/pythonbak
[root Python-2.7.2]$ ln -sf /usr/local/Python2.7/bin/python /usr/bin/python
查看Python版本,如果报少了库文件,vim /etc/ld.so.conf添加/usr/local/Python2.7/lib,执行ldconfig刷新配置文件
[root Python-2.7.2]$ python -V
python: error while loading shared libraries: libpython2.7.so.1.0: cannot open shared object file: No such file or directory
[root Python-2.7.2]$ cat /etc/ld.so.conf
include ld.so.conf.d/*.conf
/usr/local/Python2.7/lib
[root Python-2.7.2]$ ldconfig
[root Python-2.7.2]$ python -V
Python 2.7.2
如果python升级到2.7以后出现yum无法使用的情况,错误为No module named yum,请按照如下步骤处理,将yum使用的python版本改为旧版本,第一行修改为如下即可:
[root htdocs]$ vim /usr/bin/yum
#!/usr/bin/python2.6
七、安装MySQLdb for python
[root src]$ wget http://www.mtop.cc/software/MySQLdb-python.zip
[root src]$ unzip MySQLdb-python.zip
[root src]$ cd MySQLdb1-master
[root MySQLdb1-master]$ vim site.cfg
vim site.cfg添加你安装的mysql路径下的mysql_config:
添加完再执行python setup.py build可能报以下错:
[root MySQLdb1-master]$ python setup.py build
error: command 'gcc' failed with exit status 1
解决方法:
[root MySQLdb1-master]$ yum install gcc python-devel
再执行安装:
[root MySQLdb1-master]$ python setup.py build
[root MySQLdb1-master]$ python setup.py install
基本环境已经安装OVER了,下面去官网下载开源的管理软件包:http://www.lepus.cc/soft/index,要注册账号的
八、安装MySQL MTOP系统
监控机创建监控数据库,并授予权限,导入SQL文件
mysql> create database mysqlmtop default character set utf8;
Query OK, 1 row affected (0.07 sec)
mysql> grant select,insert,update,delete,create,drop on mysqlmtop.* to 'mtop_user'@'localhost' identified by 'password';
Query OK, 0 rows affected (0.04 sec)
mysql> flush privileges;
Query OK, 0 rows affected (0.02 sec)
下载后解包:
[root src]$ unzip MySQLMTOP.zip
[root src]$ cd mysqlmtop2.2
[root sql]$ pwd
/usr/local/src/mysqlmtop2.2/sql
[root sql]$ ls
mysqlmtop_data.sql mysqlmtop.sql
监控机(即MySQL MTOP server)创建监控数据库,并授予权限,导入SQL文件夹里的SQL文件(表结构和数据文件):
[root sql]$ mysql -uroot -p123456 -S /data/mysql-5.6.10/mysql.sock mysqlmtop < ./mysqlmtop.sql
[root sql]$ mysql -uroot -p123456 -S /data/mysql-5.6.10/mysql.sock mysqlmtop < ./mysqlmtop_data.sql
[root sql]$ mv /usr/local/src/mysqlmtop2.2 /usr/local/mysqlmtop
对被监控的数据库进行授权
在python采集数据的过程中,需要连接到需要监控的数据库服务器采集数据,我们为了安全考虑,在WEB管理里面只要求录入主机和端口,没有录入账号和密码。所有需要监控的数据库请授予相同的用户密码记录在配置文件中。授权如下所示:
grant select,super,process on *.* to 'monitor'@'ip' identified by 'monitor';
九、安装数据采集和报警系统
修改监控系统配置文件,进到/usr/local/mysqlmtop/mysqlmtop/etc,修改config.ini
[root etc]$ pwd
/usr/local/mysqlmtop/mysqlmtop/etc
[root etc]$ vim config.ini
授予可执行文件执行权限
[root mysqlmtop]$ pwd
/usr/local/mysqlmtop/mysqlmtop
[root mysqlmtop]$ chmod +x *.py
[root mysqlmtop]$ chmod +x *.sh
[root mysqlmtop]$ chmod +x mtopctl
[root mysqlmtop]$ ln -s /usr/local/mysqlmtop/mysqlmtop/mtopctl /usr/local/bin/
测试MySQL连接,报错了,别慌
[root mysqlmtop]$ ./test_mysql.py
Traceback (most recent call last):
File "./test_mysql.py", line 6, in <module>
import MySQLdb
File "/usr/lib64/python2.6/site-packages/MySQL_python-1.2.4-py2.6-linux-x86_64.egg/MySQLdb/__init__.py", line 19, in <module>
import _mysql
ImportError: libmysqlclient.so.18: cannot open shared object file: No such file or directory
解决方法:
如果是64系统则:
[root mysqlmtop]$ ln -s /usr/local/mysql-5.6.10/lib/libmysqlclient.so.18 /usr/lib64/libmysqlclient.so.18
32位系统:
[root mysqlmtop]$ ln -s /usr/local/mysql-5.6.10/lib/libmysqlclient.so.18 /usr/lib/libmysqlclient.so.18
如果是通过socket文件连接MySQL的话 可能会报以下错:
mysql_exceptions.OperationalError: (2002, "Can't connect to local MySQL server through socket '/tmp/mysql.sock' (111)")
添加一下链接:
[root mysqlmtop]$ ln -s /data/mysql-5.6.10/mysql.sock /tmp/mysql.sock
再执行测试脚本,看到OK就正常连接成功了:
[root mysqlmtop]$ ./test_mysql.py
MySQLDB OK!
修改下启动脚本mtopctl(注意路径),再启动监控系统(注意:只有监控进程运行时系统才会进行监控和报警)
启动:
[root mysqlmtop]$ mtopctl start
nohup: 把输出追加到"nohup.out"
mysql mtop start success!
使用--help查看监控系统进程管理参数,分别有start、stop、status
[root mysqlmtop]$ mtopctl --help
mysql mtop help:
support-site:www.mtop.cc www.ruzuojun.com
====================================================================
start Start mysql mtop monitor server; Command: #mtopctl start
stop Stop mysql mtop monitor server; Command: #mtopctl stop
status Check mysql mtop monitor run status; Command: #mtopctl status
十、安装WEB管理界面
把/usr/local/mysqlmtop/frontweb下的所有文件传到apache的/usr/local/apache/htdocs/
[root frontweb]$ pwd
/usr/local/mysqlmtop/frontweb
[root frontweb]$ cp -a * /usr/local/apache/htdocs/
打开application/config/database.php文件,修改PHP连接监控服务器的数据库信息
[root@localhost config]# pwd
/usr/local/apache/htdocs/application/config
root@localhost config]# vim database.php
修改username填刚刚在MySQL授权用户,password也就是对应的密码,mysqlmtop是对应的库,修改这三个即可
可以通过浏览器去访问了:http://ip
最好把默认的index.html删除了,这样才走index.php,默认管理员账号密码admin/admin 登录后请修改密码,增加普通账号
看一下功能:
哈哈,是不是很吸引呢,功能非常强大,大家可以研究下
总结:
一、该系统是开源的系统,如果搭建在公网,一定要做好各种访问控制,做好各方面的安全工作
二、该系统功能比较多,可以帮助MySQL DBA或运维DBA对管理MySQL及查看其性能
三、可能该系统存在一些小bug,例如发不出邮件啊,这些就要根据自己能力去完善了,如果会python,可以去掉没用的,加上自己想要的功能
详细参考资料 MySQLMTOP官网:http://www.mtop.cc/
作者:陆炫志
出处:xuanzhi的博客 http://www.cnblogs.com/xuanzhi201111
您的支持是对博主最大的鼓励,感谢您的认真阅读。本文版权归作者所有,欢迎转载,但请保留该声明。
posted @
2014-12-18 20:12
GoogSQL
阅读(9669)
评论(1)
编辑
收藏
举报
刷新评论刷新页面返回顶部
Copyright 2022 GoogSQL
Powered by .NET 7.0 on Kubernetes

Copyright ©uecom 京ICP备18064371号-3 IPV6
2024-03-29 18:16:59
tech.zxsbr.com
10.0.12.16