Memcached部署(上)
memcached
Memcached多用于作为数据库的前端cache使用,从而减少数据库的负载。Memcached是一种内存缓存,用于存储键值对。
工作流程:
(1) 检查客户端请求的数据是否在Memcached中,如果在,把请求的数据返回给客户端。
(2) 客户端请求的数据不在Memcached中,就去查询后端数据库,把从后端数据库取得的数据返回给客户端,同时在 Memcached中备份一份。
(3) Memcached中的数据要和后端数据库中的数据保持一致。
(4) Memcached根据过期策略和LRU策略来清理内存空间。
1. memcached安装
- 安装libevent
[root@chunlin Memcached]# tar -zxf libevent-2.1.8-stable.tar.gz
[root@chunlin Memcached]# cd libevent-2.1.8-stable
[root@chunlin libevent-2.1.8-stable]# mkdir /usr/local/libevent
[root@chunlin libevent-2.1.8-stable]# ./configure
--prefix=/usr/local/libevent
[root@chunlin libevent-2.1.8-stable]# make
[root@chunlin libevent-2.1.8-stable]# make install
- 安装memcached
[root@chunlin Memcached]# tar -zxf memcached-1.4.36.tar.gz
[root@chunlin Memcached]# cd memcached-1.4.36
[root@chunlin memcached-1.4.36]# mkdir /usr/local/memcached
[root@chunlin memcached-1.4.36]# ./configure
--prefix=/usr/local/memcached
--with-libevent=/usr/local/libevent
[root@chunlin memcached-1.4.36]# make
[root@chunlin memcached-1.4.36]# make install
2. Mmecached的PHP 扩展
- php的json扩展
php7中json的扩展包为jsond
[root@chunlin Memcached]# tar -zxf jsond-1.4.0.tgz
[root@chunlin Memcached]# cd jsond-1.4.0
[root@chunlin jsond-1.4.0]# /usr/local/php/bin/phpize
--with-php-config=/usr/local/php/bin/php-configphpize命令用来生成configure脚本文件,这个命令一般在php安装目录的bin目录下
[root@chunlin jsond-1.4.0]# ./configure
--with-php-config=/usr/local/php/bin/php-config
[root@chunlin jsond-1.4.0]# make
[root@chunlin jsond-1.4.0]# make test
[root@chunlin jsond-1.4.0]# make install
遇到的问题:
解决:
[root@chunlin jsond-1.4.0]# yum install -y autoconf.noarch
- php支持igbinary
下载igbinary
[root@chunlin Memcached]# tar -zxf igbinary-2.0.1.tgz
[root@chunlin Memcached]# cd igbinary-2.0.1
[root@chunlin igbinary-2.0.1]# /usr/local/php/bin/phpize
--with-php-config=/usr/local/php/bin/php-config
[root@chunlin igbinary-2.0.1]# ./configure
--with-php-config=/usr/local/php/bin/php-config
--enable-igbinary
[root@chunlin igbinary-2.0.1]# make
[root@chunlin igbinary-2.0.1]# make test
[root@chunlin igbinary-2.0.1]# make install
- php支持msgpack
下载msgpack
[root@chunlin Memcached]# tar -zxf msgpack-2.0.2.tgz
[root@chunlin Memcached]# cd msgpack-2.0.2
[root@chunlin msgpack-2.0.2]# /usr/local/php/bin/phpize
[root@chunlin msgpack-2.0.2]# ./configure
--with-php-config=/usr/local/php/bin/php-config
[root@chunlin msgpack-2.0.2]# make
[root@chunlin msgpack-2.0.2]# make test
[root@chunlin msgpack-2.0.2]# make install
- memcached的php扩展
下载memcached的php扩展包memcached
[root@chunlin Memcached]# tar -zxf memcached-3.0.2.tgz
[root@chunlin Memcached]# cd memcached-3.0.2
[root@chunlin memcached-3.0.2]# /usr/local/php/bin/phpize
--with-php-config=/usr/local/php/bin/php-config
[root@chunlin memcached-3.0.2]# ./configure
--with-php-config=/usr/local/php/bin/php-config
--enable-memcached
--with-libmemcached-dir=/usr/local/libmemcached
--enable-memcached-igbinary
--enable-memcached-json
--enable-memcached-msgpack \
--disable-memcached-sasl
[root@chunlin memcached-3.0.2]# make
[root@chunlin memcached-3.0.2]# make test
[root@chunlin memcached-3.0.2]# make install
安装完每个PHP拓展包,都会在类似/usr/local/php/lib/php/extensions/no-debug-non-zts-20160303这样的目录下生成.so后缀的文件
- 测试
Memcahced的测试需要用到nginx和php,nginx和php的安装和配置详见http://www.cnblogs.com/NewStudy/p/7271539.html
[root@chunlin ~]# vim /etc/php.ini
在文件最后添加对模块的扩展
extension=igbinary.so
extension=msgpack.so
extension=jsond.so
extension=memcached.so
[root@chunlin ~]# /etc/init.d/php-fpm restart
重启FastCGI进程管理器
然后重启web服务器
浏览器中输入http://IP/index.php
index.php中的内容如下:
<?php
phpinfo();
?>;
浏览器显示结果如下所示:
php页面中使用memcached
[root@chunlin html]# vim memcached.php
<?php
$mem = new Memcached();
$mem-> addServer("127.0.0.1",11211);
$mem-> set("test","Memcached test");
echo $mem-> get('test');
?>
浏览器中输入http://IP/memcached.php出现Memcached test字样表示PHP与memcached之间的接口能正常工作了
3. Nagios监控Memcached
1) 安装check_memcached
软件:perl-Config-Tiny、Nagios-Plugins-Memcached、epel源
环境:能执行perl程序的环境
[guest@host nagios]$ yum install -y perl-CPAN.noarch
[guest@host nagios]$ tar -zxf Nagios-Plugins-Memcached-0.02.tar.gz
[guest@host nagios]$ cd Nagios-Plugins-Memcached-0.02/
[guest@host Nagios-Plugins-Memcached-0.02]$ perl Makefile.PL
[guest@host Nagios-Plugins-Memcached-0.02]$ make
[guest@host Nagios-Plugins-Memcached-0.02]$ make install
安装完之后,我在我的/usr/local/bin目录下发现了check_memcached脚本,以为这样安装就算大功告成了,然而结果并不是这样,执行脚本时报错了
[root@host bin]# ./check_memcached --help
解决办法:
[root@host perl]# rpm -ivh perl-Config-Tiny-2.14-7.el7.noarch.rpm
[guest@host bin]$ yum install -y perl-Nagios-Plugin.noarch
2) 配置memcached监控
[guest@host bin]$ cp check_memcached /usr/local/nagios/libexec/
[guest@host libexec]$ chmod 755 check_memcached
[guest@host libexec]$ cd /usr/local/nagios/etc/objects/
[guest@host objects]$ vim commands.cfg
define command{
command_name check_memcached_time
command_line $USER1$/check_memcached -H $HOSTADDRESS$ -w $ARG1$ -c $ARG2$
}
define command{
command_name check_memcached_size
command_line $USER1$/check_memcached -H $HOSTADDRESS$ --size-warning $ARG1$ --size-critical $ARG2$
}
define command{
command_name check_memcached_hit
command_line $USER1$/check_memcached -H $HOSTADDRESS$ --hit-warning $ARG1$ --hit-critical $ARG2$
}
以上三个命令的定义依次为:
check_memcached_time:对时间的定义,单位为毫秒
check_memcached_size:对内存使用情况的定义,以百分比为单位
check_memcached_hit:对hit值的定义,以百分比为单位
[root@host objects]# vim services.cfg
define service{
use local-service,services-pnp ; Name of service template to use
host_name chunlin
service_description Memcached_time
check_command check_memcached_time!200!500
notifications_enabled 0
}
define service{
use local-service,services-pnp ; Name of service template to use
host_name chunlin
service_description Memcached_size
check_command check_memcached_size!80!90
notifications_enabled 0
}
define service{
use local-service,services-pnp ; Name of service template to use
host_name chunlin
service_description Memcached_hit
check_command check_memcached_hit!90!80
notifications_enabled 0
}
[root@host objects]# nagios -v /usr/local/nagios/etc/nagios.cfg
[root@host objects]# systemctl restart nagios.service