安装php
yum install -y memcached libmemcached libevent systemctl start memcached
/usr/bin/memcached -u memcached -p 11211 -m 64-c 1024 或者是 vi /etc/sysconfig/memcached
memcached-tool 127.0.0.1:11211 stats
或者mysql
echo stats |nc 127.0.0.1 11211
须要安装nc工具 linux
yum install -y nc rpm -qf `which nc`
若安装libmemcached后,可使用命令git
memstat --servers=127.0.0.1:11211
查看memcached服务状态
在memcached中,运行state命令能够查看memcached服务的状态信息,其中cmd_get表示总的get次数,get_hits表示get的总命中次数,命中率 = get_hits/cmd_ge
总结,查看状态三种方法web
memcached-tool 127.0.0.1:11211 stats echo stats |nc 127.0.0.1 11211 memstat --servers=127.0.0.1:11211
Memcached语法规则
<command name> <key> <flags> <exptime> <bytes>\r\n <data block>\r\n
注:\r\n在windows下是Enter键 redis
telnet 127.0.0.1 11211 #进入memcached
set key2 0 30 2 #30(过时时间30s,若是是0就表示永不过时,2两个字节)
ab
STORED
get key2
VALUE key2 0 2
ab
END算法
ctrl + } 退出
quit 退出 telnetsql
Memcached数据导出和导入
导出:mongodb
memcached-tool 127.0.0.1:11211 dump > data.txt
cat data.txt
导入:数据库
nc 127.0.0.1 11211 < data.txt
若nc命令不存在,yum install nc
注意:导出的数据是带有一个时间戳的,这个时间戳就是该条数据过时的时间点,若是当前时间已经超过该时间戳,那么是导入不进去的
cd /usr/local/src/ wget http://www.apelearn.com/bbs/data/attachment/forum/memcache-2.2.3.tgz tar zxf memcache-2.2.3.tgz cd memcache-2.2.3 /usr/local/php-fpm/bin/phpize
遇到问题
Cannot find autoconf. Please check your autoconf installation and the
$PHP_AUTOCONF environment variable. Then, rerun this script.
解决方法:
yum install -y m4 autoconf
./configure --with-php-config=/usr/local/php-fpm/bin/php-config make && make install
安装完后会有相似这样的提示:Installing shared extensions: /usr/local/php-fpm/lib/php/extensions/no-debug-non-zts-20131226/
而后修改php.ini添加一行
vi /usr/local/php-fpm/etc/php.ini extension=memcache.so
检查
/usr/local/php-fpm/bin/php -m /etc/init.d/php-fpm restart
curl www.apelearn.com/study_v2/.memcache.txt > 1.php 2>/dev/null
1.php内容也能够参考https://coding.net/u/aminglinux/p/yuanke_centos7/git/blob/master/21NOSQL/1.php
执行脚本
/usr/local/php-fpm/bin/php 1.php
或者将1.php放到某个虚拟主机根目录下面,在浏览器访问,便可看到效果
vim /usr/local/php-fpm/etc/php.ini
搜索session
如图,路径为file,即保存在本地,默认的session存放在/tmp
cd /usr/local/src wget http://study.lishiming.net/.mem_se.txt mv .mem_se.txt /data/wwwroot/default/session.php 其中session.php内容能够参考https://coding.net/u/aminglinux/p/yuanke_centos7/git/blob/master/21NOSQL/session.php curl localhost/session.php
php-fpm.conf对应的pool中添加
vim /usr/local/php-fpm/etc/php-fpm.conf php_value[session.save_handler] = memcache php_value[session.save_path] = " tcp://192.168.127.133:11211 "
/etc/init.d/php-fpm restart rm -rf /tmp/sess* curl localhost/session.php
memcached-tool 127.0.0.1:11211 dump > data.txt cat data.txt