21.5 memcached命令行php
21.6 memcached数据导出和导入linux
21.7 php链接memcachednginx
21.8 memcached中存储sessionsgit
21.5 memcached命令行算法
1.telnet 127.0.0.1 11211 #进入memcachedsql
2.set key2 0 30 2shell
set 表示存储一条数据apache
key2 表示k的名字(k-v)vim
0 指的是flags,下面详解(特殊需求时才会用)windows
30 为过时时间(秒)。若是为0表明永不过时
2 表示你要存的数值是两位的(或者说是两个字节),指定了几位就要写几位(好比12)
ab
STORED
get key2
VALUE key2 0 2
ab
END
Memcached语法规则
1.<command name> <key> <flags> <exptime> <bytes>\r\n <data block>\r\n
注:\r\n在windows下是Enter键
<command name> 能够是set, add, replace
set表示按照相应的<key>存储该数据,没有的时候增长,有的时候覆盖
add表示按照相应的<key>添加该数据,可是若是该<key>已经存在则会操做失败
replace表示按照相应的<key>替换数据,可是若是该<key>不存在则操做失败。
2.<key> 客户端须要保存数据的key
3.<flags> 是一个16位的无符号的整数(以十进制的方式表示)。该标志将和须要存储的数据一块儿存储,并在客户端get数据时返回。客户端能够将此标志用作特殊用途,此标志对服务器来讲是不透明的。
4.<exptime> 为过时的时间。若为0表示存储的数据永远不过时(但可被服务器算法:LRU 等替换)。若是非0(unix时间或者距离此时的秒数),当过时后,服务器能够保证用户得不到该数据(以服务器时间为标准)。
5.<bytes> 须要存储的字节数,当用户但愿存储空数据时<bytes>能够为0
6.<data block>须要存储的内容,输入完成后,最后客户端须要加上\r\n(直接点击Enter)做为结束标志。
实例:
[root@axinlinux-01 ~]# telnet 192.168.208.128 11211
Trying 192.168.208.128...
Connected to 192.168.208.128.
Escape character is '^]'.
set key2 0 03 2
12
STORED
set key1 0 30 3 #若是咱们指定了他的字节是3,写入12就不对了
12
CLIENT_ERROR bad data chunk
ERROR
set key1 0 30 3 #指定了字节为3,就要写3位
abc
STORED
get key1 #咱们查看key1就没有的,由于已通过了指定的时间30秒
END
set key1 0 30 3 #咱们再插入一个key1
122
STORED
get key1 #用get查看他,就会获得相应的数据
VALUE key1 0 3
122
END
演习:
set key3 1 1000 4 #先set key3
abcd
STORED
replace key3 1 0 5 #咱们作一个替换(key3),设置他永不过时
abcde
STORED
get key3
VALUE key3 1 5 #咱们再来get key3就是咱们替换之后的值
abcde
END
delete key3 #咱们再把key3删除
DELETED
get key3 #在get就没有了
END
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
21.6 memcached数据导出和导入
先前提到把mencached重启的时候,要把数据存储一下。启动后,再把数据导回去
1.导出:
memcached-tool 127.0.0.1:11211 dump > data.txt
cat data.txt
2.导入:
nc 127.0.0.1 11211 < data.txt
若nc命令不存在,yum install nc
注意:导出的数据是带有一个时间戳的,这个时间戳就是该条数据过时的时间点,若是当前时间已经超过该时间戳,那么是导入不进去的
实例:
set name 1 0 6 #先写几条数据
amings
STORED
set age 1 0 2
22
STORED
set axin 1 0 10
1234567899
STORED
^] #退出的话,按ctrl+],在quit退出
telnet> quit
Connection closed.
[root@axinlinux-01 ~]# memcached-tool 192.168.208.128:11211 stats #看一下他的状态
#192.168.208.128:11211 Field Value
accepting_conns 1
auth_cmds 0
auth_errors 0
bytes 225
bytes_read 282
bytes_written 178
cas_badval 0
cas_hits 0
cas_misses 0
cmd_flush 0
cmd_get 5 #get了5次
cmd_set 9 #set了9个 ,这都是咱们以上作的操做
cmd_touch 0
conn_yields 0
connection_structures 11
curr_connections 10
curr_items 3
decr_hits 0
decr_misses 0
delete_hits 1
delete_misses 0
evicted_unfetched 0
evictions 0
expired_unfetched 1
get_hits 2
get_misses 3
hash_bytes 524288
hash_is_expanding 0
hash_power_level 16
incr_hits 0
incr_misses 0
libevent 2.0.21-stable
limit_maxbytes 67108864
listen_disabled_num 0
pid 1697
pointer_size 64
reclaimed 2
reserved_fds 20
rusage_system 0.111099
rusage_user 0.152030
threads 4
time 1541686324
total_connections 12
total_items 8
touch_hits 0
touch_misses 0
uptime 2180
version 1.4.15
[root@axinlinux-01 ~]# memcached-tool 192.168.208.128:11211 dump > data.txt #这样来导出
Dumping memcache contents
Number of buckets: 1
Number of items : 3
Dumping bucket 1 - 3 total items
[root@axinlinux-01 ~]# ls #存在了当前目录下
22.txt anaconda-ks.cfg data.txt logs rsync2.test shell
[root@axinlinux-01 ~]# cat data.txt
add axin 1 1541684144 10
1234567899
add name 1 1541684144 6 #这里为add
amings
add age 1 1541684144 2
22
[root@axinlinux-01 ~]# systemctl restart memcached #咱们重启一下,数据会清空
[root@axinlinux-01 ~]# telnet 192.168.208.128 11211
Trying 192.168.208.128...
Connected to 192.168.208.128.
Escape character is '^]'.
set name
ERROR
quit
Connection closed by foreign host.
[root@axinlinux-01 ~]# nc 192.168.208.128 11211 < data.txt #这样来导入
STORED
STORED
STORED
导出时系统会记录一个时间戳,咱们在这个时间戳以后导入,就会过时(也就是进去后没有数据)
[root@axinlinux-01 ~]# date -d "+1 hour" +%s #咱们能够日后加一个小时的时间戳
1541691370
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
21.7 php链接memcached(为下面的sessions会话为准备)
php-fpm须要有一个模块(memcache)来链接memcached,这个模块能够理解为他们的中间件。
1.先安装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文件,要用他来编译
./configure --with-php-config=/usr/local/php-fpm/bin/php-config
make && make install
2.安装完后会有相似这样的提示:Installing shared extensions: /usr/local/php-fpm/lib/php/extensions/no-debug-non-zts-20131226/。咱们须要这个路径下的memcache.so的文件
3.而后修改php.ini添加一行extension=memcache.so
4.检查/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
实例
[root@axinlinux-01 ~]# cd /usr/local/src/
[root@axinlinux-01 src]# wget http://www.apelearn.com/bbs/data/attachment/forum/memcache-2.2.3.tgz
[root@axinlinux-01 src]# tar xzf memcache-2.2.3.tgz
[root@axinlinux-01 src]# cd memcache-2.2.3/
[root@axinlinux-01 memcache-2.2.3]# ls #看一下并无用来编译的config文件
config9.m4 config.w32 example.php memcache_consistent_hash.c memcache_queue.c memcache_session.c php_memcache.h
config.m4 CREDITS memcache.c memcache.dsp memcache_queue.h memcache_standard_hash.c README
[root@axinlinux-01 memcache-2.2.3]# /usr/local/php-fpm/bin/phpize #因此要执行一下phpize,来生成一下config文件
Configuring for:
PHP Api Version: 20131106
Zend Module Api No: 20131226
Zend Extension Api No: 220131226
[root@axinlinux-01 memcache-2.2.3]# ls #再次查看就会发现已经有了config文件
acinclude.m4 config9.m4 config.sub CREDITS Makefile.global memcache_queue.c missing run-tests.php
aclocal.m4 config.guess configure example.php memcache.c memcache_queue.h mkinstalldirs
autom4te.cache config.h.in configure.in install-sh memcache_consistent_hash.c memcache_session.c php_memcache.h
build config.m4 config.w32 ltmain.sh memcache.dsp memcache_standard_hash.c README
[root@axinlinux-01 memcache-2.2.3]# ./configure --with-php-config=/usr/local/php-fpm/bin/php-config #只须要指定这一个就能够了
[root@axinlinux-01 memcache-2.2.3]# echo $?
0
[root@axinlinux-01 memcache-2.2.3]# make
Installing shared extensions: /usr/local/php-fpm/lib/php/extensions/no-debug-non-zts-20131226/
[root@axinlinux-01 memcache-2.2.3]# echo $?
0
[root@axinlinux-01 memcache-2.2.3]# make install
Installing shared extensions: /usr/local/php-fpm/lib/php/extensions/no-debug-non-zts-20131226/
#在提示的这个路径下,就生成了一个memcache.so的文件。咱们要的就是他
[root@axinlinux-01 memcache-2.2.3]# echo $?
0
[root@axinlinux-01 memcache-2.2.3]# ls /usr/local/php-fpm/lib/php/extensions/no-debug-non-zts-20131226/
memcache.so opcache.a opcache.so
[root@axinlinux-01 memcache-2.2.3]# vim /usr/local/php-fpm/etc/php.ini
;extension=php_pdo_pgsql.dll
;extension=php_pdo_sqlite.dll
;extension=php_pgsql.dll
;extension=php_shmop.dll
extension=memcache.so #在这个区域加入这一行。以上这些就是扩展模块的区域
; The MIBS data available in the PHP distribution must be installed.
; See http://www.php.net/manual/en/snmp.installation.php
;extension=php_snmp.dll
[root@axinlinux-01 memcache-2.2.3]# /usr/local/php-fpm/sbin/php-fpm -m #在来看一下有没有这个模块
[PHP Modules]
。。。
memcache
测试
[root@axinlinux-01 memcache-2.2.3]# curl www.apelearn.com/study_v2/.memcache.txt > 1.php 2>/dev/null
[root@axinlinux-01 memcache-2.2.3]# cat 1.php
[root@axinlinux-01 memcache-2.2.3]# /usr/local/php-fpm/bin/php 1.php
Get key1 value: This is first value<br>Get key1 value: This is replace value<br>Get key2 value: Array
(
[0] => aaa
[1] => bbb
[2] => ccc
[3] => ddd
)
<br>Get key1 value: <br>Get key2 value: <br>[root@axinlinux-01 memcache-2.2.3]#
#出现以上,即为成功
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
21.8 memcached中存储sessions
应用场景:lnmp的架构中负载均衡,为了使用户的登陆状态在一台服务器上,能够使用upstream,ip_hash来解决。那要是使用lvs负载均衡呢?就能够把sessions不在存储到服务器磁盘上,而是存到memcached里面去。memcached能够做为一个公共的服务器,访问的时候使用其中某一个内网ip,而不是127.0.0.1啦
那怎么在php中指定他的,怎么把sessions存到memcached服务里面去呢?如下是几种方法:
1.
本实例是在lamp/lnmp环境下实现
方法1.(不成功,不建议使用)
编辑php.ini添加两行
session.save_handler = memcache
session.save_path = "tcp://192.168.0.9:11211"
方法2. (httpd使用)
或者httpd.conf中对应的虚拟主机中添加
php_value session.save_handler "memcache" php_value session.save_path "tcp://192.168.0.9:11211"
方法3.
或者php-fpm.conf对应的pool中添加(nginx能够使用)
php_value[session.save_handler] = memcache
php_value[session.save_path] = " tcp://192.168.0.9:11211 "
2.
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
3.
curl localhost/session.php
相似于1443702394<br><br>1443702394<br><br>i44nunao0g3o7vf2su0hnc5440
4.
telnet 127.0.0.1 11211
5.
get i44nunao0g3o7vf2su0hnc5440
实例:(针对第三种方法)
[root@axinlinux-01 vhost]# cd /usr/local/php-fpm/etc/php-fpm.d/
[root@axinlinux-01 php-fpm.d]# ls
axin.conf.bak www.conf
[root@axinlinux-01 php-fpm.d]# vim www.conf
php_value[session.save_handler] = memcache #直接在最下面加入
php_value[session.save_path] = " tcp://192.168.208.128:11211 "
[root@axinlinux-01 php-fpm.d]# /etc/init.d/php-fpm restart
Gracefully shutting down php-fpm . done
Starting php-fpm done
[root@axinlinux-01 ~]# wget http://study.lishiming.net/.mem_se.txt
[root@axinlinux-01 ~]# mv .mem_se.txt /data/wwwroot/default/index.php
[root@axinlinux-01 ~]# cat /data/wwwroot/default/index.php
<?php
session_start();
if (!isset($_SESSION['TEST'])) {
$_SESSION['TEST'] = time();
}
$_SESSION['TEST3'] = time();
print $_SESSION['TEST'];
print "<br><br>";
print $_SESSION['TEST3'];
print "<br><br>";
print session_id();
?>
[root@axinlinux-01 php-fpm.d]# curl localhost/index.php
1541775242<br><br>1541775242<br><br>1v3tr7dji2d65vagi9ft2tpbl3 #正常的话是有后面这个数值的,若是没有就表明不成功。这个数值就是Key
[root@axinlinux-01 php-fpm.d]# telnet 192.168.208.128 11211 #进入memcached
Trying 192.168.208.128...
Connected to 192.168.208.128.
Escape character is '^]'.
get 1v3tr7dji2d65vagi9ft2tpbl3 #get上面的那个KEY 。若是get不出来,退出memcached多试几回curl的操做
VALUE 1v3tr7dji2d65vagi9ft2tpbl3 0 37
TEST|i:1541775242;TEST3|i:1541775242; #这个值是他的VALUE
END
总结:
要想把sessions存储到memcached里面去,
第一步要去下载php的扩展包(php链接memcached那一部分)
第二部就是将sessions增长到memcached里面去