重庆分公司,新征程启航
为企业提供网站建设、域名注册、服务器等服务
[root@Dasoncheng ~]# telnet 127.0.0.1 11211 Trying 127.0.0.1... Connected to 127.0.0.1. Escape character is \'^]\'. set keyname 0 30 2 ab STORED set keyname2 0 30 3 abc STORED Memcached语法规则
[root@Dasoncheng ~]# telnet 127.0.0.1 11211 Trying 127.0.0.1... Connected to 127.0.0.1. Escape character is \'^]\'. set key3 1 100 4 ##set是创建,如果存在key3会覆盖(add则会报错,replace若key3不存在则会报错) 1234 STORED get key3 ##get 查看key3 VALUE key3 1 4 1234 END replace key3 1 1000 2 ab STORED get key3 VALUE key3 1 2 ab END delete key3 ##delete删除 DELETED get key3 END 21.6 memcached数据导出和导入
导出:
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
注意:导出的数据是带有一个时间戳的,这个时间戳就是该条数据过期的时间点,如果当前时间已经超过该时间戳,那么是导入不进去的
[root@Dasoncheng ~]# telnet 127.0.0.1 11211 Trying 127.0.0.1... Connected to 127.0.0.1. Escape character is \'^]\'. set key1 1 0 1 a STORED set key2 1 10 2 ab STORED set key3 1 0 3 abc STORED set key4 1 0 4 abcd STORED ^] telnet> quit Connection closed.
导出:
[root@Dasoncheng ~]# memcached-tool 127.0.0.1:11211 dump Dumping memcache contents Number of buckets: 1 Number of items : 4 Dumping bucket 1 - 4 total items add key1 1 1507602916 1 a add key4 1 1507602916 4 abcd add key3 1 1507602916 3 abc [root@Dasoncheng ~]# memcached-tool 127.0.0.1:11211 dump >data.txt Dumping memcache contents Number of buckets: 1 Number of items : 3 Dumping bucket 1 - 3 total items [root@Dasoncheng ~]# cat data.txt add key1 1 1507602916 1 a add key4 1 1507602916 4 abcd add key3 1 1507602916 3 abc [root@Dasoncheng ~]# memcached-tool 127.0.0.1:11211 stats #127.0.0.1:11211 Field Value curr_connections 11 curr_items 3 decr_hits 0 decr_misses 0 delete_hits 1 delete_misses 0 evicted_unfetched 0 evictions 0 expired_unfetched 2 get_hits 8 ……
导入:
[root@Dasoncheng ~]# nc 127.0.0.1 11211
先安装php的memcache扩展
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
./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添加一行extension="memcache.so“
检查/usr/local/php/bin/php-fpm -m
下载测试脚本 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放到某个虚拟主机根目录下面,在浏览器访问,即可看到效果
最终可以看到数据如下:
[0] => aaa
[1] => bbb
[2] => ccc
[3] => ddd
本实例是在lamp/lnmp环境下实现
编辑php.ini添加两行 session.save_handler = memcachesession.save_path = "tcp://192.168.0.9:11211" 或者httpd.conf中对应的虚拟主机中添加 php_value session.save_handler "memcache"php_value session.save_path "tcp://192.168.0.9:11211" 或者php-fpm.conf对应的pool中添加 php_value[session.save_handler] = memcache php_value[session.save_path] = " tcp://192.168.0.9:11211 " wget http://study.lishiming.net/.mem_se.txt mv .mem_se.txt /usr/local/apache2/htdocs/session.php 其中session.php内容可以参考https://coding.net/u/aminglinux/p/yuanke_centos7/git/blob/master/21NOSQL/session.php curl localhost/session.php 类似于1443702394