最近配置zabbix,想用percona的插件模板监控agent端的mysql。但是在运行php命令的时候系统发出警告,server端得不到数据库。情况如下:
# php -q ./ss_get_mysql_stats.php --host localhost --items gg
PHP Warning: PHP Startup: Unable to load dynamic library '/usr/lib64/php/modules/mysql.so' - /usr/lib64/php/modules/mysql.so: symbol mysql_fetch_row, version libmysqlclient_18 not defined in file libmysqlclient.so.18 with link time reference in Unknown on line 0
PHP Warning: PHP Startup: Unable to load dynamic library '/usr/lib64/php/modules/mysqli.so' - /usr/lib64/php/modules/mysqli.so: symbol mysql_change_user, version libmysqlclient_18 not defined in file libmysqlclient.so.18 with link time reference in Unknown on line 0
PHP Warning: PHP Startup: Unable to load dynamic library '/usr/lib64/php/modules/pdo_mysql.so' - /usr/lib64/php/modules/pdo_mysql.so: symbol mysql_options, version libmysqlclient_18 not defined in file libmysqlclient.so.18 with link time reference in Unknown on line 0
条件:
# cat /etc/redhat-release
CentOS Linux release 7.5.1804 (Core)
zabbix版本:3.2.4,编译安装。(使用zabbix自带模板监控数据库是没有问题的)
# yum -y install php php-mysql
# rpm -qa | grep php
php-common-5.4.16-45.el7.x86_64
php-pdo-5.4.16-45.el7.x86_64
php-mysql-5.4.16-45.el7.x86_64
php-cli-5.4.16-45.el7.x86_64
php-5.4.16-45.el7.x86_64
但是,在agent端使用php命令进行测试的时候,系统就发出上面的警告还监测不到数据。参考网上的一些方法,再者因为自己能力有限也没有解决掉这个问题。希望有相关经验的大牛给指点一下。
万般无奈,只好进行编译安装PHP搏一搏了,删除已经安装的PHP,再进行编译安装PHP。
1、安装依赖应用:
# yum install -y gcc gcc-c++ autoconf libjpeg libjpeg-devel libpng libpng-devel freetype freetype-devel libpng libpng-devel libxml2 libxml2-devel zlib zlib-devel glibc glibc-devel glib2 glib2-devel bzip2 bzip2-devel ncurses curl openssl-devel db4-devel libXpm-devel libX11-devel gmp-devel readline-devel libxslt-devel expat-devel xmlrpc-c libcurl libcurl-devel
2、安装加密扩展库,先安装Libmcrypt
# tar xf libmcrypt-2.5.8.tar.gz //版本不局限于此
# cd libmcrypt-2.5.8
# ./configure
# make ;make install
3、编译安装PHP
# tar xf php-5.6.30.tar.gz //版本不局限于此
# cd php-5.6.30
# ./configure --prefix=/usr/local/php --with-config-file-path=/etc --with-apxs2=/usr/sbin/apxs --with-mysql=/software/mysql --with-mysqli=/software/mysql/bin/mysql_config --enable-inline-optimization --enable-fpm --enable-soap --with-libxml-dir --with-xmlrpc --with-openssl --with-mcrypt --with-mhash --with-pcre-regex --with-sqlite3 --with-zlib --enable-bcmath --with-iconv --with-bz2 --enable-calendar --with-curl --with-cdb --enable-dom --enable-exif --enable-fileinfo --enable-filter --with-pcre-dir --enable-ftp --with-gd --with-openssl-dir --with-jpeg-dir --with-png-dir --with-zlib-dir --with-freetype-dir --enable-gd-native-ttf --with-gettext --with-gmp --with-mhash --enable-json --enable-mbstring --disable-mbregex --disable-mbregex-backtrack --with-libmbfl --with-onig --enable-pdo --with-pdo-mysql --with-zlib-dir --with-pdo-sqlite --with-readline --enable-session --enable-shmop --enable-simplexml --enable-sockets --enable-sysvmsg --enable-sysvsem --enable-sysvshm --enable-wddx --with-libxml-dir --with-xsl --enable-zip --enable-mysqlnd-compression-support --with-pear
此时,会先出现两个报错(我的服务器遇到的),第一个:
Sorry, I cannot run apxs. Possible reasons follow:
1. Perl is not installed
2. apxs was not found. Try to pass the path using --with-apxs2=/path/to/apxs
3. Apache was not built using --enable-so (the apxs usage page is displayed)
The output of /usr/sbin/apxs follows:
./configure: line 6461: /usr/sbin/apxs: No such file or directory
configure: error: Aborting
措施:
#yum install -y perl*
#yum install -y httpd-devel
#find / -name apxs 得到的路径是:/usr/bin/apxs //我的服务器中得到的路径,可能因服务器不同,路径不同(猜测,请见谅)
于是修改--with-apsx2=/usr/bin/apxs指定到正确路径
第二个错误如下,我参考:http://james23dier.iteye.com/blog/798898 (感谢此博主),使用方法1将问题解决了:
checking for specified location of the MySQL UNIX socket... no
checking for MySQL UNIX socket location... no
configure: error: Cannot find libmysqlclient under /usr.
Note that the MySQL client library is not bundled anymore!
通过find命令找到libmysqlclient,发现是在/usr/lib64目录内的libmysqlclient.so.18.0.0做的软连接,PHP默认是去的 /usr/lib/搜索,所以没有找到.找到问题了就好解决了.
解决办法就是:(libmysqlclient.so.18.0.0文件版本根据自己情况,目录还可能在/usr/lib64/中)
1、cp -rp /usr/lib64/libmysqlclient.so.18.0.0 /usr/lib/libmysqlclient.so
2、centos : --with-mysql=/usr/lib64/mysql (香巴拉) 或者cp /usr/lib64/mysql/* /usr/lib/mysql/
问题解决后在进行安装
# make ;make install
安装结束后,找到php命令所在的路径(当时认为php已安装结束,直接用php命令,依旧报那个错。这个php命令是看到php安装结束后,末尾有个相关路径的信息,我就cd到这个目录下发现有php命令,然后在执行php命令,结果就取到数据了)
# ll /usr/local/php/bin/
total 78576
-rwxr-xr-x. 1 root root 837 Jul 20 16:10 pear
-rwxr-xr-x. 1 root root 858 Jul 20 16:10 peardev
-rwxr-xr-x. 1 root root 774 Jul 20 16:10 pecl
lrwxrwxrwx. 1 root root 9 Jul 20 16:10 phar -> phar.phar
-rwxr-xr-x. 1 root root 14833 Jul 20 16:10 phar.phar
-rwxr-xr-x. 1 root root 40265432 Jul 20 16:10 php
-rwxr-xr-x. 1 root root 40152376 Jul 20 16:10 php-cgi
-rwxr-xr-x. 1 root root 3710 Jul 20 16:10 php-config
-rwxr-xr-x. 1 root root 4538 Jul 20 16:10 phpize
于是修改percona插件的脚本文件ss_get_mysql_stats.php
# pwd
/var/lib/zabbix/percona/scripts
# ll
total 64
-rwxr-xr-x. 1 root root 1262 Jul 20 16:21 get_mysql_stats_wrapper.sh
-rwxr-xr-x. 1 root root 59993 Jul 20 16:26 ss_get_mysql_stats.php
# vim get_mysql_stats_wrapper.sh
...
DIR=`dirname $0`
CMD="/usr/local/php/bin/php -q $DIR/ss_get_mysql_stats.php --host $HOST --items gg"
...
5、添加PHP环境变量
# vim /etc/profile
...
PATH=$PATH:/usr/local/php/bin/
export PATH //在文件末尾添加这两行,路径根据自己的情况进行编写。
6、测试
# php -q /var/lib/zabbix/percona/scripts/ss_get_mysql_stats.php --host localhost --items gg
gg:307206
到此针对php的操作已经完成。其他设置可参考网上的教程。