memcached命令行、 memcached数据导出和导入、php连接memcached、memcached中存储session

来源:转载

memcached命令行
进入数据库

说明: 使用telnet命令进入数据库


[[email protected] ~]# telnet 127.0.0.1 11211
-bash: telnet: 未找到命令# yum安装telnet命令
[[email protected] ~]# yum install -y telnet#成功进入数据库
[[email protected] ~]# telnet 127.0.0.1 11211
Trying 127.0.0.1...
Connected to 127.0.0.1.
Escape character is '^]'. 存储数据

说明:使用set 创建数据


[[email protected] ~]# telnet 127.0.0.1 11211
Trying 127.0.0.1...
Connected to 127.0.0.1.
Escape character is '^]'.
set key2 0 30 2
wh
STOREDset key1 0 30 3
wt
wrt
CLIENT_ERROR bad data chunk
ERROR
特别说明:定义了三个字节,那么要入3个字节,否则会报错
set key1 0 20 3
qwe
STORED注:key2 --> 库名;30 --> 单位是秒,表示过期时间;2 --> 定义几个字节
查看存储是否成功

说明: 如果get没有数据出来,说明数据已经过期了,这就是过期时间,如果不想数据过期可以设置为0


get key1
END
如果数据没有过期的话,那么get就可以查看存储的数据
[[email protected] ~]# 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
wuwu
STORED
get key3
VALUE key3 1 4
wuwu
END
Memcached语法规则 /r/n /r/n 说明:/r/n在Windows下是Enter键
可以是set、add、replace set表示按照相应的存储该数据,没有的时候增加,有的时候覆盖 add表示按照相应添加该数据,但是如果该已经存在,则会操作失败. replace表示按照相应的替换数据,但是如果该不存在,则会操作失败.
客户端需要保存数据的key
是一个16位的无符号的整数(以十进制的方式表示). 该标志将和需要存储的数据一起存储,并在客户端get数据时返回. 客户端可以将此标志用做特殊用途,此标志对服务器来说是不透明的.
为过期的时间 若为0表示存储的数据永远不过期(但可被服务器算法:LRU等替换). 如果非0(Unix时间或者距离此时的秒数),当过期后,服务器可以保证用户得不到该数据(以服务器时间为标准)
需要存储的内容,输入完成后,最后客户端需要加上/r/n(直接点击Enter)作为结束标志 替换数据--replace
#创建key4,过期时间为1000秒,5个字节
set key4 1 1000 5
Anwut
STORED
#查看是否创建成功
get key4
VALUE key4 1 5
Anwut
END
#把key4过期时间替换成0永不过期,10个字节
replace key4 1 0 10
1234567890
STOREDget key4
VALUE key4 1 10
1234567890
END 删除值--delete
#删除key4的值
delete key4
DELETED
#使用get查看,key4就没任何的数值
get key4
END 退出memcached方式
^] --> Ctrl+]
telnet> quit
Connection closed.memcached数据导出和导入

说明:memcached数据导出的目的呢,是重启服务器或者重启Memcached服务的时候,内在内存的数据会消失, 所以当重启Memcached服务时需要先把数据导出来,启动完之后再把数据导回去,以保证数据不丢失.


[[email protected] ~]# telnet 127.0.0.1 11211
Trying 127.0.0.1...
Connected to 127.0.0.1.
Escape character is '^]'.
set name 1 0 6
annawu
STORED
set key6 1 0 3
wqd
STORED
set name 1 0 4
dong
STORED
^]
telnet> quit
Connection closed.
#写入几条数据,目的是为了学习做实验
查看memcached状态

说明:之前做的过操作使用memstat命令可以看到


[[email protected] ~]# memstat --servers=127.0.0.1:11211
Server: 127.0.0.1 (11211)
pid: 2180
uptime: 39939
time: 1507661325
version: 1.4.15
libevent: 2.0.21-stable
pointer_size: 64
rusage_user: 1.113442
rusage_system: 1.263668
curr_connections: 10
total_connections: 17
connection_structures: 11
reserved_fds: 20
cmd_get: 9//查看了有9个项目
cmd_set: 10//创建了10个项目
cmd_flush: 0
cmd_touch: 0
get_hits: 4
get_misses: 5
delete_misses: 0
delete_hits: 1//删除了一个项目
incr_misses: 0
incr_hits: 0
decr_misses: 0
decr_hits: 0
cas_misses: 0
cas_hits: 0
cas_badval: 0
touch_hits: 0
touch_misses: 0
auth_cmds: 0
auth_errors: 0
bytes_read: 430
bytes_written: 3464
limit_maxbytes: 67108864
accepting_conns: 1
listen_disabled_num: 0
threads: 4
conn_yields: 0
hash_power_level: 16
hash_bytes: 524288
hash_is_expanding: 0
bytes: 145
curr_items: 2
total_items: 8
expired_unfetched: 1
evicted_unfetched: 0
evictions: 0
reclaimed: 1 导出数据
[[email protected] ~]# memcached-tool 127.0.0.1:11211 dump > data.txt
Dumping memcache contents
Number of buckets: 1
Number of items: 2
Dumping bucket 1 - 2 total items 重启Memcached服务
[[email protected] ~]# systemctl restart memcached 导入数据
[[email protected] ~]# nc 127.0.0.1 11211 < data.txt
STORED
STORED
注:成功导入了两个数据#查看是否导入成功
[[email protected] ~]# telnet 127.0.0.1 11211
Trying 127.0.0.1...
Connected to 127.0.0.1.
Escape character is '^]'.
get name
END
get key6
END
^]
telnet> quit
Connection closed.
[[email protected] ~]# cat data.txt
add key6 1 1507621386 3
wqd
add name 1 1507621386 4
dong
说明:进入数据库查看数据,结果是get不到,是因为data.txt的数据有一个时间戳已经过期.
修改data.txt文件数据的时间戳

说明:修改为1个小时后的时间戳


[[email protected] ~]# date -d "+1 hour" +%s
1507707396
[[email protected] ~]# vim data.txt
add key6 1 1507707396
add key6 1 1507707396 3
wqd
add name 1 1507707396 4
dong 重新导入数据

说明: 现在更改了过期的时间戳,就可以查数据.


#重启Memcached服务
[[email protected] ~]# systemctl restart memcached
#导入数据
[[email protected] ~]# nc 127.0.0.1 11211 < data.txt
STORED
STORED
#进入数据库查看数据
[[email protected] ~]# telnet 127.0.0.1 11211
Trying 127.0.0.1...
Connected to 127.0.0.1.
Escape character is '^]'.
get name
VALUE name 1 4
dong
END
get key6
VALUE key6 1 3
wqd
END php连接memcached

说明:虽然可以使用telnet连接,但是在网站里面(LNMP架构),如何PHP去连接memcached呢? Memcached它总得要有自己的客户端吧. 在前面编译PHP,PHP是支持连接MySQL的,如何PHP不支持连接MySQL的话,是不可能做到搭建Discuz论坛(查看帖子或发帖子),可以做到安装Discuz论坛(查看帖子或发帖子),是因为PHP和MySQL是可以连接的. 同理PHP也需要连接Memcached,所以在PHP的模块里面需要Memcache这样的一个模块,它作为PHP连接Memcached的一个中间件,如果没有这个模块的话,PHP也是无法连接Memcached的.


安装php的memcached扩展
[[email protected] ~]# cd /usr/local/src
#下载
[[email protected] src]# wget http://www.apelearn.com/bbs/data/attachment/forum/memcache-2.2.3.tgz
#解压
[[email protected] src]# tar -zxvf memcache-2.2.3.tgz
package.xml
memcache-2.2.3/config.m4
memcache-2.2.3/config9.m4
memcache-2.2.3/config.w32
memcache-2.2.3/CREDITS
memcache-2.2.3/example.php
memcache-2.2.3/memcache.c
memcache-2.2.3/memcache_queue.c
memcache-2.2.3/memcache_session.c
memcache-2.2.3/memcache_standard_hash.c
memcache-2.2.3/memcache_consistent_hash.c
memcache-2.2.3/memcache.dsp
memcache-2.2.3/php_memcache.h
memcache-2.2.3/memcache_queue.h
memcache-2.2.3/README
#进入memcached目录
[[email protected] src]# cd memcache-2.2.3
[[email protected] memcache-2.2.3]# ls
config9.m4config.w32example.phpmemcache_consistent_hash.cmemcache_queue.cmemcache_session.c php_memcache.h
config.m4 CREDITS memcache.c memcache.dspmemcache_queue.hmemcache_standard_hash.cREADME#生成configure文件
[[email protected] 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
[[email protected] memcache-2.2.3]# ls
acinclude.m4config.guessconfigure.inltmain.sh memcache_queue.c mkinstalldirs
aclocal.m4config.h.in config.w32Makefile.globalmemcache_queue.h php_memcache.h
autom4te.cacheconfig.m4 CREDITSmemcache.cmemcache_session.c README
buildconfig.subexample.php memcache_consistent_hash.cmemcache_standard_hash.crun-tests.php
config9.m4configure install-shmemcache.dspmissing
#编译
[[email protected] memcache-2.2.3]# ./configure --with-php-config=/usr/local/php-fpm/bin/php-config
[[email protected] memcache-2.2.3]# echo $?
0
#make
[[email protected] memcache-2.2.3]# make && make install#make之后会在/usr/local/php-fpm/lib/php/extensions/no-debug-non-zts-20131226/这里生成一个Memcache.so的文件
[[email protected] memcache-2.2.3]# ls /usr/local/php-fpm/lib/php/extensions/no-debug-non-zts-20131226/
memcache.soopcache.aopcache.so 编辑/usr/local/php-fpm/etc/php.ini
[[email protected] memcache-2.2.3]# vim /usr/local/php-fpm/etc/php.ini
添加:
extension=memcache.so 查看php模块可以看到有memcache的模块
[[email protected] memcache-2.2.3]# /usr/local/php-fpm/sbin/php-fpm -m
[PHP Modules]
cgi-fcgi
Core
ctype
curl
date
dom
ereg
fileinfo
filter
ftp
gd
hash
iconv
json
libxml
mbstring
memcache
mysql
mysqli
openssl
pcre
PDO
pdo_mysql
pdo_sqlite
Phar
posix
Reflection
session
SimpleXML
soap
SPL
sqlite3
standard
tokenizer
xml
xmlreader
xmlwriter
zlib
[Zend Modules] 测试php是否支持memcache这个扩展

说明:下载测试脚本


[[email protected] ~]# curl www.apelearn.com/study_v2/.memcache.txt > 1.php 2> /dev/null
[[email protected] ~]# cat 1.php
<?php
//连接Memcache Memcache
$mem = new Memcache;
$mem->connect("localhost", 11211);
//保存数据
$mem->set('key1', 'This is first value', 0, 60);
$val = $mem->get('key1');
echo "Get key1 value: " . $val ."
";
//替换数据
$mem->replace('key1', 'This is replace value', 0, 60);
$val = $mem->get('key1');
echo "Get key1 value: " . $val . "
";
//保存数组数据
$arr = array('aaa', 'bbb', 'ccc', 'ddd');
$mem->set('key2', $arr, 0, 60);
$val2 = $mem->get('key2');
echo "Get key2 value: ";
print_r($val2);
echo "
";
//删除数据
$mem->delete('key1');
$val = $mem->get('key1');
echo "Get key1 value: " . $val . "
";
//清除所有数据
$mem->flush();
$val2 = $mem->get('key2');
echo "Get key2 value: ";
print_r($val2);
echo "
";
//关闭连接
$mem->close();
?> 测试

说明: 测试成功.


[[email protected] ~]# /usr/local/php-fpm/bin/php 1.php
Get key1 value: This is first value
Get key1 value: This is replace value
Get key2 value: Array
(
[0] => aaa
[1] => bbb
[2] => ccc
[3] => ddd
)
Get key1 value:
Get key2 value:
[[email protected] ~]#
memcached中存储session

说明:memcached中存储session一般用在负载均衡,比如是lnmp架构,这多web服务器,在用户登录的时候就遇到一个问题,第一次登录在A服务器上,第二次在B服务器上,这时候呢,就无法让某一个用户他的登录状态始终在某服务器上. 如果我们使用的是nginx代理的话,可以使用IP_HASH, 但如果是使用LVS,那么就无法让某一个用户他的登录状态始终在某服务器上. 但有个方发让session不再存到磁盘上,而是存到memcached里面去,而memcached可以作为一个公共的服务器,那它访问的时候使用其中某个内网IP,而不是127.0.0.1.把session存到memcached里面,需要在php-fpm.conf配置文件里定义.


打开php.ini配置文件
vim /usr/local/php-fpm/etc/php.ini
........
[Session]
; Handler used to store/retrieve data.
; http://php.net/session.save-handler
session.save_handler = files
........
说明:默认session定义的是files,说明session存在本地磁盘
在/tmp/目录下
#目前/tmp/下是没有存有任何session文件
[[email protected] ~]# ls /tmp/
percona-version-checksystemd-private-18b3b32e01794693acbd981fddf019fa-vmtoolsd.service-v1SklX下载一个PHP文件,是存储session的脚本.
[[email protected] ~]# wget http://study.lishiming.net/.mem_se.txt
[[email protected] ~]# cat .mem_se.txt
<?php
session_start();
if (!isset($_SESSION['TEST'])) {
$_SESSION['TEST'] = time();
}
$_SESSION['TEST3'] = time();
print $_SESSION['TEST'];
print "
";
print $_SESSION['TEST3'];
print "
";
print session_id();
?>把.mem_se.txt文件移动到/data/www/default/目录下,并重名为1.PHP

说明:/data/www/default/这个是默认虚拟主机


[[email protected] ~]# cd /data/www/default/
[[email protected] default]# mv /root/.mem_se.txt 1.php
curl访问测试

说明:这样相当于存了一个session在/tmp/下


[[email protected] vhost]# curl localhost/1.php
1507710970
1507710970
vouvkcg3kfegqq2cpsk5ihkof2
查看/tmp/目录

说明:可以看到多了一个sess_vouvkcg3kfegqq2cpsk5ihkof2文件


[[email protected] vhost]# ls -lt /tmp/
总用量 8
-rw------- 1 php-fpm php-fpm 37 10月 11 16:36 sess_vouvkcg3kfegqq2cpsk5ihkof2
drwx------ 3 rootroot17 10月 10 15:37 systemd-private-18b3b32e01794693acbd981fddf019fa-vmtoolsd.service-v1SklX
-rw-r--r-- 1 rootroot57 10月1 11:51 percona-version-check 在php-fpm.conf定义

说明:在php-fpm.conf增加以下这两行配置: php_value[session.save_handler] = memcache php_value[session.save_path] = " tcp://192.168.2.115:11211 "


[[email protected] ~]# vim /usr/local/php-fpm/etc/php-fpm.conf
[global]
pid = /usr/local/php-fpm/var/run/php-fpm.pid
error_log = /usr/local/php-fpm/var/log/php-fpm.log
[www]
#listen = /tmp/php-fcgi.sock
listen = 127.0.0.1:9000
listen.mode = 666
user = php-fpm
group = php-fpm
pm = dynamic
pm.max_children = 50
pm.start_servers = 20
pm.min_spare_servers = 5
pm.max_spare_servers = 35
pm.max_requests = 500
rlimit_files = 1024
php_value[session.save_handler] = memcache
php_value[session.save_path] = " tcp://192.168.2.115:11211 " 删除/tmp/下的session文件
[[email protected] ~]# rm -f /tmp/sess*
[[email protected] ~]# ls -lt /tmp/
总用量 4
drwx------ 3 root root 17 10月 10 15:37 systemd-private-18b3b32e01794693acbd981fddf019fa-vmtoolsd.service-v1SklX
-rw-r--r-- 1 root root 57 10月1 11:51 percona-version-checkcurl访问测试
[[email protected] ~]# curl localhost/1.php
1507713147
1507713147
qmt424v6croeptqegfoi9q4lm0进入数据库查看,可以查看到数据的
[[email protected] ~]# telnet 127.0.0.1 11211
Trying 127.0.0.1...
Connected to 127.0.0.1.
Escape character is '^]'.
get qmt424v6croeptqegfoi9q4lm0
VALUE qmt424v6croeptqegfoi9q4lm0 0 37
TEST|i:1507713147;TEST3|i:1507713147;
END

分享给朋友:
您可能感兴趣的文章:
随机阅读: