使用yum安装Memcached
memcached依赖于libevent,在安装memcached之前需要检查libevent是否安装
#rpm -qa libevent
[root@harry-01 ~]# yum list | grep memcache
Repodata is over 2 weeks old. Install yum-cron? Or run: yum makecache fast
libmemcached.i686 1.0.16-5.el7 base
libmemcached.x86_64 1.0.16-5.el7 base
libmemcached-devel.i686 1.0.16-5.el7 base
libmemcached-devel.x86_64 1.0.16-5.el7 base
memcached.x86_64 1.4.15-10.el7_3.1 base
memcached-devel.i686 1.4.15-10.el7_3.1 base
memcached-devel.x86_64 1.4.15-10.el7_3.1 base
pcp-pmda-memcache.x86_64 4.1.0-5.el7_6 updates
php-pecl-memcache.x86_64 3.0.8-4.el7 base
python-memcached.noarch 1.48-4.el7 base
[root@harry-01 ~]# yum -y install memcached
fastestmirror
Loading mirror speeds from cached hostfile
* base: mirrors.aliyun.com
* extras: ap.stykers.moe
* updates: ap.stykers.moe
启动memcached服务
[root@harry-01 ~]# systemctl start memcached
#查看memcache进程
[root@harry-01 ~]# ps aux | grep memcache
memcach+ 6973 0.0 0.1 350616 2184 ? Ssl 21:41 0:00 /usr/bin/memcached -u memcached -p 11211 -m 64 -c 1024
root 6987 0.0 0.0 112724 984 pts/0 R+ 21:41 0:00 grep --color=auto memcache
查看memcached状态
Memcached命令行
ab
STORED
get key2
VALUE key2 0 2
ab
END
Memcached语法规则
<key> 客户端需要保存数据的key
该标志将和需要存储的数据一起存储,并在客户端get数据时返回。
客户端可以将此标志用做特殊用途,此标志对服务器来说是不透明的。
若为0表示存储的数据永远不过期(但可被服务器算法:LRU 等替换)。
如果非0(unix时间或者距离此时的秒数),当过期后,服务器可以保证用户得不到该数据(以服务器时间为标准)。
set key3 1 100 4 #1是<flags> 100是<exptime>为过期时间 4是<bytes>字节数
abcd #data block
STORED
get key3
VALUE key3 1 4
abcd
END
replace key3 1 200 5
abcdx
STORED
get key3
VALUE key3 1 5
abcdx
END
delete key3
DELETED
get key3
END
memcached数据导出和导入
每当需要重启服务器之前,需要将memcached中的数据导出(memcached是存储在内存中,一旦重启数据就会消失)
PHP连接Memcached
先安装php的memcache扩展
[root@harry-01 ~]# cd /usr/local/src/
[root@harry-01 src]# wget http://www.apelearn.com/bbs/data/attachment/forum/memcache-2.2.3.tgz
2019-03-02 23:23:11 (1.16 MB/s) - 已保存 “memcache-2.2.3.tgz” [27366/27366])
[root@harry-01 src]# tar zxf memcache-2.2.3.tgz
[root@harry-01 src]# cd memcache-2.2.3
[root@harry-01 memcache-2.2.3]# /usr/local/php-fpm/bin/phpize
Configuring for:
PHP Api Version: 20131106
Zend Module Api No: 20131226
Zend Extension Api No: 220131226
Cannot find autoconf. Please check your autoconf installation and the
$PHP_AUTOCONF environment variable. Then, rerun this script.
[root@harry-01 memcache-2.2.3]# echo $?
1
//出现错误 解决办法:yum -y install autoconf
[root@harry-01 memcache-2.2.3]# ./configure --with-php-config=/usr/local/php-fpm/bin/php-config
checking for grep that handles long lines and -e... /usr/bin/grep
[root@harry-01 memcache-2.2.3]# make && make install
安装完后会有类似这样的提示:
修改php.ini添加一行
[root@harry-01 ~]# vim /etc/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
Memcached中存储session
#编辑php.ini添加两行
session.save_handler = memcache #指定存储类型
session.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<br><br>1443702394<br><br>i44nunao0g3o7vf2su0hnc5440
telnet 127.0.0.1 11211
get i44nunao0g3o7vf2su0hnc5440