怎样安装PHP的memcached扩展

2016年9月29日 | 分类: 【技术】

【介绍】

memcached是danga.com的一个开源项目,它是一个高性能的分布式的内存对象缓存系统,通过在内存里维护一个统一的巨大的Hash表,能够用来存储各种格式的数据。可以类比于MySQL这样的服务。

而PHP的memcached扩展是连接memcached的方式。

官网:http://memcached.org/

【安装】

安装libevent:

参考:http://amon.org/libevent

参考:https://blog.uvtodo.com/node/l153nqw.htm

安装libmemcached:

memcached扩展基于libmemcahced 库。

官网:https://launchpad.net/libmemcached/+download

参考:https://www.jianshu.com/p/b89dc1e78068
参考:https://www.w3cschool.cn/memcached/oqvf12l4.html

可以yum安装:

yum install libmemcached libmemcached-devel

可以编译安装:

wget https://launchpad.net/libmemcached/1.0/1.0.18/+download/libmemcached-1.0.18.tar.gz && tar zxf libmemcached-1.0.18.tar.gz && cd libmemcached-1.0.18
./configure --prefix=/usr/local/libmemcached --with-memcached
make && make install

安装路径:
1. 库文件:/usr/local/libmemcached/lib/
2. 头文件:/usr/local/libmemcached/include/

如果不指定安装目录 –prefix=/usr/local/libmemcached :
1. 库文件:/usr/local/lib/
2. 头文件:/usr/local/include/libmemcachde/

编译安装memcached:

当前版本:1.5.12

wget http://www.memcached.org/files/memcached-1.5.12.tar.gz && tar zxvf memcached-1.5.12.tar.gz && cd memcached-1.5.12
./configure --enable-dtrace --enable-64bit --disable-docs --with-libevent=/usr/lib
make && make install

验证安装:

memcached -h

输出:

memcached 1.5.12
...

将memecached设成系统服务

编辑文件 /etc/init.d/memcached :

#! /bin/sh
#
#chkconfig: - 90 25
#description:Memcached Daemon
#
. /etc/rc.d/init.d/functions
. /etc/sysconfig/network

MEMCACHED="/usr/local/bin/memcached"
SERVER_ID="127.0.0.1"
SERVER_PORT="11211"

[ -f $MEMCACHED ] || exit 1

start()
{
      echo -n $"Starting memcached:"
      daemon $MEMCACHED -u daemon -d -m 1024 -l $SERVER_IP -p $SERVER_PORT 
      echo
}
stop()
{
     echo -n $"Shutting down memcached:"
     killproc memcached
     echo
}

#Print how to called.
case "$1" in
start)
start
;;
stop)
stop
;;
restart)
stop
sleep 3
start
;;
*)
echo $"Usage:$0 {start|stop|restart}"
exit 1
esac
exit 0

清除句尾^M:

dos2unix /etc/init.d/memcached

设置启动服务:

chmod 755 /etc/init.d/memcached
chkconfig --add memcached
chkconfig --level 2345 memcached on
chkconfig --list memcached

输出:

memcached       0:off   1:off   2:on    3:on    4:on    5:on    6:off

服务管理命令:

service memcached start   # 启动memcached
service memcached stop   # 关闭memcached
service memcached restart   # 重启memcached

输出:

Starting memcached (via systemctl):                        [  OK  ]

编辑 /etc/sysconfig/memcached 进行配置:

PORT=”11211″ 端口
USER=”root” 使用的用户名
MAXCONN=”1024″ 同时最大连接数
CACHESIZE=”64″ 使用的内存大小
OPTIONS=”" 附加参数

安装完成。

编译安装php-memcached:

由于memcached目前版本不支持php7.1,解决方法见后。

git clone https://github.com/rlerdorf/php-memcached.git && cd php-memcached
/usr/local/php/bin/phpize
./configure --with-php-config=/usr/local/php/bin/php-config --with-libmemcached-dir=/usr/local/libmemcached/
make
make install

输出:

Installing shared extensions:     /usr/local/php/lib/php/extensions/no-debug-zts-20180731/

修改php.ini:

/usr/local/php/etc/php.ini

在文件最后面加入memcached的扩展路径:

; [memcached]
extension = "memcached.so"

重启php服务

service php-fpm restart

报错:

PHP Fatal error:  Cannot redefine class constant Memcached::RES_BAD_KEY_PROVIDED in Unknown on line 0

参考:https://stackoverflow.com/questions/41758945/php-fatal-error-cannot-redefine-class-constant-memcachedres-bad-key-provided

git clone https://github.com/php-memcached-dev/php-memcached.git && cd php-memcached
/usr/local/php/bin/phpize
./configure --with-php-config=/usr/local/php/bin/php-config --with-libmemcached-dir=/usr/local/libmemcached/
make
make install

这次成功安装。

检查是否成功安装到php扩展:

/usr/local/php/bin/php -m | grep memcached

输出:

memcached

查看phpinfo:

memcached
memcached support	enabled
Version 	3.1.0-dev
libmemcached version 	1.0.16
SASL support 	yes
Session support 	yes
igbinary support 	no
json support 	no
msgpack support 	no 

【参考】

参考:https://zhuanlan.zhihu.com/p/28978617
参考:http://www.cnblogs.com/wangtao_20/p/4057591.html
参考:https://blog.linuxeye.cn/345.html
参考:https://www.yiichina.com/topic/6635