1、memcached命令行:那么如何在memcache里操作:比如创建和存储数据;
使用telnet命令,如果没有则安装: yum install -y telnet
使用命令 telnet memcached的IP地址 端口
telnet 127.0.0.1 11211 #进入memcached:
[root@localhost_001 ~]# telnet 127.0.0.1 11211
Trying 127.0.0.1...
Connected to 127.0.0.1.
Escape character is '^]'.
(1):存储和查询操作: 因为 key -- value 类型
存储数据: set key1 0 20 3 === set key1的名字 20(过期时间) 3(必须3位的)
查询数据:get key3
[root@localhost_001 ~]# telnet 127.0.0.1 11211
Trying 127.0.0.1...
Connected to 127.0.0.1.
Escape character is '^]'.
set key1 0 20 3
abc
STORED
get key1
END
set key2 0 40 4
123
CLIENT_ERROR bad data chunk
ERROR
set key3 0 50 4
trew
STORED
get key3
VALUE key3 0 4
trew
END
注释:set时最后一位表示位数(如key2 的后面是4,就必须是4位,多或少都会报错;)
set后面的分别是 键值key 0 50过期时间 4位数 | 查询时用get;
格式:<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>不存在则操作失败。
<key> 客户端需要保存数据的key;
set 0 30 4 ==== set flage exptime bytes
<flags>,即在使用set时 后面的0, 是一个16位的无符号的整数(以十进制的方式表示)。该标志将和需要存储的数据一起存储,并在客户端get数据时返回。客户端可以将此标志用做特殊用途,此标志对服务器来说是不透明的。
<exptime> 为过期的时间。若为0表示存储的数据永远不过期(但可被服务器算法:LRU 等替换)。如果非0(unix时间或者距离此时的秒数),当过期后,服务器可以保证用户得不到该数据(以服务器时间为标准)。
<bytes> j即在使用set时最后买的位数;需要存储的字节数,当用户希望存储空数据时<bytes>可以为0,
<data block>需要存储的内容,输入完成后,最后客户端需要加上\r\n(直接点击Enter)作为结束标志。
#####数据示例: 注释:
set key3 1 100 4
abcd
STORED
replace key3 1 0 5
abcde
STORED
get key3
VALUE key3 1 5
abcde
END
add key3 1 0 4
abcd
NOT_STORED
注释:退格时可以使用ctrl+退格键才可以;
2、memcached的数据导入和导出; 使用memcache-tool工具;
有时候memcached需要重启的时候,最好把数据导入一下,启动在导入进去;
此时我们先写入几条永久不过期的数据,注意第三列 0 表示永不过期,如下;
set name 1 0 5
ayuan
STORED
set age 1 0 2
20
STORED
set ke1 1 0 5
12345
STORED
^]
telnet> quit
Connection closed.
退出时使用 ctrl+] ------> quit
使用memstat查看下有几个项目了, cmd_get 13个项目 cmd_set 13个项目 get_hits;
[root@localhost_001 ~]# memstat --servers=127.0.0.1:11211
Server: 127.0.0.1 (11211)
pid: 1826
uptime: 14220
time: 1542213147
total_connections: 15
connection_structures: 11
reserved_fds: 20
cmd_get: 13
cmd_set: 13
cmd_flush: 0
cmd_touch: 0
get_hits: 3
get_misses: 10
delete_misses: 0
delete_hits: 0
2:导出数据,使用memcache-tool工具;
memcache-tool 127.0.0.1:11211 dump > data.txt 导出数据到data.txt文本;
nc 127.0.0.1 11211 < data.txt 导入data.txt的数据到memcached里;
[root@localhost_001 ~]# 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 ke1 1 1542198927 5
12345
add name 1 1542198927 5
ayuan
add age 1 1542198927 2
20
add key3 1 1542198927 5
abcde
[root@localhost_001 ~]# memcached-tool 127.0.0.1:11211 dump > data.txt #导出数据到data.txt
Dumping memcache contents
Number of buckets: 1
Number of items : 4
Dumping bucket 1 - 4 total items
3:此时重启memcached,会清空数据,然后再导入; 不支持持久化;
[root@localhost_001 ~]# systemctl restart memcached
[root@localhost_001 ~]# nc 127.0.0.1 11211 < data.txt
STORED
STORED
STORED
STORED
[root@localhost_001 ~]# nc 127.0.0.1 11211
注释:此时导入后查看发现没有数据的,这是因为数据在到导出来是有一个时间戳,这个时间戳就是这条数据过期的时间点,如果当前的时候已经超过时间戳,是无法导入进去的;如下,查看时间戳;
[root@localhost_001 ~]# cat data.txt
add ke1 1 1542198927 5
12345
add name 1 1542198927 5
ayuan
add age 1 1542198927 2
20
add key3 1 1542198927 5
abcde
[root@localhost_001 ~]# date -d @1542198927
2018年 11月 14日 星期三 20:35:27 CST
那怎么办了就是在导入之前需要重新导出数据的时间戳的;如下;查看两小时后的时间戳,并重新导入;
[root@localhost_001 ~]# systemctl restart memcached #重启服务,清空memcache;
[root@localhost_001 ~]# date -d "+3 hour" +%s #查看3小时后的时间戳;
1542228042
[root@localhost_001 ~]# vim data.txt #修改第四列的时间戳;
add ke1 1 1542228042 5
12345
add name 1 1542228042 5
ayuan
add age 1 1000 2
20
[root@localhost_001 ~]# nc 127.0.0.1 11211 < data.txt #导入后:
STORED
STORED
STORED
[root@localhost_001 ~]# telnet 127.0.0.1 11211 #查看数据;
Trying 127.0.0.1...
Connected to 127.0.0.1.
Escape character is '^]'.
get key1
END
get name
VALUE name 1 5
ayuan
END
get age
VALUE age 1 2
20
END
^]
telnet> quit
Connection closed.
注释:系统在创建这个数据的时候,又指定一个过期时间的,在导出的时候会自己换算成时间戳,就是 0 也会标记成在创建哪一刻的时间戳;
3、php连接memcached:使用LAMP这个架构时,怎么用php去连接memcached;
使用memcached作为php的扩展存在;那么如何安装这个;
1:首先下载memcached这个包;
wget http://www.apelearn.com/bbs/data/attachment/forum/memcache-2.2.3.tgz
2:解压这个包; tar zxvf memcache-2.2.3.tgz
3:使用phpize命令 生成 config 文件;
cd memcache-2.2.3
/usr/local/php/bin/phpize
4:使用config进行编译;
./configure -with-php-config=/usr/local/php/bin/php-config
5: make && make install
[root@localhost_002 src]# wget http://www.apelearn.com/bbs/data/attachment/forum/memcache-2.2.3.tgz
已发出 HTTP 请求,正在等待回应... 200 OK
长度:27366 (27K) [application/octet-stream]
正在保存至: “memcache-2.2.3.tgz”
100%[===========================================================================================>] 27,366 --.-K/s 用时 0.02s
2018-11-16 15:54:20 (1.15 MB/s) - 已保存 “memcache-2.2.3.tgz” [27366/27366])
[root@localhost_002 src]# tar zxvf memcache-2.2.3.tgz
[root@localhost_002 src]# cd memcache-2.2.3
[root@localhost_002 memcache-2.2.3]# /usr/local/php/bin/phpize
Configuring for:
PHP Api Version: 20131106
Zend Module Api No: 20131226
Zend Extension Api No: 220131226
[root@localhost_002 memcache-2.2.3]# ./configure --with-php-config=/usr/local/php/bin/php-config^C
[root@localhost_002 memcache-2.2.3]# echo $?
130
[root@localhost_002 memcache-2.2.3]# make
[root@localhost_002 memcache-2.2.3]# make install
Installing shared extensions: /usr/local/php/lib/php/extensions/no-debug-zts-20131226/
4:查下memcached的so文件;
[root@localhost_002 memcache-2.2.3]# ls /usr/local/php/lib/php/extensions/no-debug-zts-20131226/
memcache.so opcache.so
5:编辑php.ini的配置文件; 添加memcached的扩展 /usr/local/php/bin/php.ini
[root@localhost_002 memcache-2.2.3]# vim /usr/local/php/bin/php.ini|grep extension=memcache
extension=memcache.so
注释:可以放到最后,也可以放到php的模块区域;
6:查看php的扩展模块,看是否有加载memcached; /usr/local/php/bin/php -m
[root@localhost_002 memcache-2.2.3]# /usr/local/php/bin/php -m
[PHP Modules]
libxml
mbstring
mcrypt
memcache
mysql
mysqli
注释:看到有加载memcache模块;
4:接下来使用LAMP测试下,memcache扩展是否生效;
1:首先下载一个测试文件:也可以复制下面的内容了;
[root@localhost_002 ~]# curl www.apelearn.com/study_v2/.memcache.txt > 1.php 2>/dev/null
[root@localhost_002 ~]# cat 1.
1.php 1.sh 1.txt
[root@localhost_002 ~]# cat 1.php
<?php
//连接Memcache Memcache
$mem = new Memcache;
$mem->connect("192.168.149.129", 11211);
//保存数据
$mem->set('key1', 'This is first value', 0, 60);
$val = $mem->get('key1');
echo "Get key1 value: " . $val ."<br>";
//替换数据
$mem->replace('key1', 'This is replace value', 0, 60);
$val = $mem->get('key1');
echo "Get key1 value: " . $val . "<br>";
//保存数组数据
$arr = array('aaa', 'bbb', 'ccc', 'ddd');
$mem->set('key2', $arr, 0, 60);
$val2 = $mem->get('key2');
echo "Get key2 value: ";
print_r($val2);
echo "<br>";
//删除数据
$mem->delete('key1');
$val = $mem->get('key1');
echo "Get key1 value: " . $val . "<br>";
//清除所有数据
$mem->flush();
$val2 = $mem->get('key2');
echo "Get key2 value: ";
print_r($val2);
echo "<br>";
//关闭连接
$mem->close();
?>
注释:因为是在192.168.149.129这台上面安装的memcache,所以IP地址哪里要注意一下哈;
2:使用php的命令测试; /usr/local/php/bin/php 1.php
[root@localhost_002 ~]# /usr/local/php/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@localhost_002 ~]# echo $?
0
注释:出现如上情况,表示安装成功;
3:使用memcached中缓存session,用在lnmp的架构下,如果使用了lvs负载均衡,这么多的web服务器,用户登录第一次在A服务器,第二次登录在B服务器,没办法让某一个用户固定在一台服务器上,可以把session不在存在服务上,而是存在一台公共的服务上;
注释:session默认会放在本地的/tmp目录下,并以文件的形式;
此时开启192.168.149.130(002)机器;在002机器操作如下;
首先在002机器下载的测试session的文件;并移动当前网站的根目录下;
[root@localhost_002 ~]# wget http://study.lishiming.net/.mem_se.txt
[root@localhost_002 ~]# mv .mem_se.txt /data/wwwroot/abc.com/1.php
[root@localhost_002 ~]# cat /data/wwwroot/abc.com/1.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();
?>
2:然后测试: curl localhost/1.php 并查看/tmp/目录下是否有生成session文件;
[root@localhost_002 ~]# curl localhost/1.php
1542361239<br><br>1542361239<br><br>a93f74dac2a1541a45dc180295730db8
[root@localhost_002 ~]# ls -l /tmp/
srwxrwxrwx 1 mysql mysql 0 10月 5 11:00 mysql.sock
-rw------- 1 daemon daemon 37 11月 16 17:40 sess_a93f74dac2a1541a45dc180295730db8
注释:有生成了一个session的文件;每执行一次就会生成一个sessioon文件;
那么如何在php关联memcached,需要修改apache的虚拟主机配置文件的文件: /usr/local/apache2.4/conf/extra/http-vhosts.conf
在对应虚拟主机<VirtualHost *:80> 添加 </VirtualHost >
[root@localhost_002 php]# vim /usr/local/apapche2.4/conf/extra/httpd-vhosts.conf
php_value session.save_handler "memcache"
php_value session.save_path "tcp://192.168.149.129:11211"
重启apache服务;
[root@localhost_002 ~]# /usr/local/apapche2.4/bin/apachectl graceful
注释:如果是nginx,则再相应的pool池添加如下内容;
php-fpm.conf对应的pool中添加
php_value[session.save_handler] = memcache
php_value[session.save_path] = " tcp://192.168.149.129:11211 "
然后在002上这台访问: curl localhost/1.php 会得到最后一个参数;
php_value_session.save_handler "memcache"
php_value_session.save_path "tcp://192.168.1.115:11211"
然后用telnet 192.168.149.129 11211
get 最后一个参数