【介绍】
memcached是danga.com的一个开源项目,它是一个高性能的分布式的内存对象缓存系统,通过在内存里维护一个统一的巨大的Hash表,能够用来存储各种格式的数据。可以类比于MySQL这样的服务。
而PHP的memcached扩展是连接memcached的方式。
【安装】
安装libevent:
安装libmemcached:
memcached扩展基于libmemcahced 库。
官网:https://launchpad.net/libmemcached/+download
可以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.6.6
wget http://www.memcached.org/files/memcached-1.6.6.tar.gz && tar zxvf memcached-1.6.6.tar.gz && cd memcached-1.6.6 ./configure --enable-dtrace --enable-64bit --disable-docs --with-libevent=/usr/lib make && make install
验证版本:1.5.13
wget http://www.memcached.org/files/memcached-1.5.13.tar.gz && tar zxvf memcached-1.5.13.tar.gz && cd memcached-1.5.13 ./configure --enable-dtrace --enable-64bit --disable-docs --with-libevent=/usr/lib make && make install
验证安装:
memcached -h
输出:
memcached 1.6.6 ...
报错:去掉 –enable-dtrace 后重试。
将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/ Installing shared extensions: /usr/lib64/php/modules/
修改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
git clone https://github.com/php-memcached-dev/php-memcached.git && cd php-memcached phpize ./configure --with-php-config=/usr/local/php/bin/php-config --with-libmemcached-dir=/usr/local/libmemcached/ ./configure --with-php-config=/usr/bin/php-config --with-libmemcached-dir=/usr/local/libmemcached/ --disable-memcached-saslmake 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
启动命令:
memcached -d -m 1024 -u root -l 127.0.0.1 -p 11211
【排错】
报错:error: ISO C++ forbids comparison between pointer and integer
make 时报错:
make[1]: Entering directory '/root/libmemcached-1.0.18' CXX clients/memflush.o clients/memflush.cc: In function ‘int main(int, char**)’: clients/memflush.cc:42:22: error: ISO C++ forbids comparison between pointer and integer [-fpermissive] if (opt_servers == false) ^~~~~ clients/memflush.cc:51:24: error: ISO C++ forbids comparison between pointer and integer [-fpermissive] if (opt_servers == false) ^~~~~ make[1]: *** [Makefile:5832: clients/memflush.o] Error 1 make[1]: Leaving directory '/root/libmemcached-1.0.18' make: *** [Makefile:3700: all] Error 2
参考:https://github.com/oneinstack/oneinstack/issues/154
参考:https://blog.csdn.net/doubleface999/article/details/108836126
直接修改 clients/memflush.cc 源代码,将42行、51行:
if (opt_servers == false)
替换为:
if (!opt_servers)
【参考】
参考: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