标签:memcached数据的导入和导出 php连接memcached
[toc]Memcached类似于mysql一样,同样支持类似于mysql中创建一个库,创建一个表,插入一个表,查看表数据等。
[root@xavi ~]# 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
其中key2 代表着 key值
0代表着 flags 标记
30代表着 过期时间
2代表着 2个字节
set key2 0 30 2
abc
CLIENT_ERROR bad data chunk
ERROR
12
ERROR
set key1 0 20 3
abc
STORED
set key1 0 30 3
cds
STORED
get key1 //注意这里设定的过期时间是30秒,要再过期前查询有效
VALUE key1 0 3
cds
END
示例展示:(在telnet模式下,使用“Ctrl + delete键删除”)
set key3 1 100 4 //存储一个4字节值
abcd
STORED
get key3 //查询key3的值
VALUE key3 1 4
abcd
END
replace key3 1 200 5 //替换key3的值
abcdx
STORED
get key3 //查询替换后的值
VALUE key3 1 5
abcdx
END
delete key3 //删除key3的值
DELETED
get key3
END
随意建立几条数据
set name 1 0 6
xavier
STORED
set age 1 0 2
20
STORED
set k1 1 0 5
12345
STORED
按下"ctrl+]" ,然后输入quit
首先查看下之前的操作
cmd_get和cmd_set
[root@xavi ~]# memcached-tool 127.0.0.1:11211 dump
Dumping memcache contents
Number of buckets: 1
Number of items : 3
Dumping bucket 1 - 3 total items
add k1 1 1527337188 5
12345
add name 1 1527337188 6
xavier
add age 1 1527337188 2
20
[root@xavi ~]# memcached-tool 127.0.0.1:11211 dump >data.txt //可以把导出数据重定向到txt文档中
Dumping memcache contents
Number of buckets: 1
Number of items : 3
Dumping bucket 1 - 3 total items
[root@xavi ~]# cat data.txt
add k1 1 1527337188 5
12345
add name 1 1527337188 6
xavier
add age 1 1527337188 2
20
若nc命令不存在,yum install nc
[ ] 注意:导出的数据是带有一个时间戳的,这个时间戳就是该条数据过期的时间点,如果当前时间已经超过该时间戳,那么是导入不进去的。
[root@xavi ~]# systemctl restart memcached
[root@xavi ~]# nc 127.0.0.1 11211 < data.txt
STORED
STORED
STORED
[root@xavi ~]# cd /usr/local/src
[root@xavi src]# wget http://www.apelearn.com/bbs/data/attachment/forum/memcache-2.2.3.tgz
[root@xavi src]# tar zxf memcache-2.2.3.tgz
[root@xavi src]# cd memcache-2.2.3
[root@xavi 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
执行配置文件并编译
./configure --with-php-config=/usr/local/php-fpm/bin/php-config
[root@xavi memcache-2.2.3]# make
[root@xavi memcache-2.2.3]# make install
Installing shared extensions: /usr/local/php-fpm/lib/php/extensions/no-debug-non-zts-20131226/
查看生成的so文件
[root@xavi memcache-2.2.3]# ls /usr/local/php-fpm/lib/php/extensions/no-debug-non-zts-20131226/
memcache.so opcache.a opcache.so
vim /usr/local/php-fpm/etc/php.ini
添加一行:
extension="memcache.so"
/usr/local/php-fpm/sbin/php-fpm -m
发现已经出现了 memcache 模块
编辑测试脚本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 ."<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();
?>
----------------------------
如上脚本意为:
连接memcache set一个数据, 保存 替换删除 关闭 等。
执行这个php文件脚本
[root@xavi memcache-2.2.3]# cd
[root@xavi ~]# vim 1.php
[root@xavi ~]# /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>
本节应用场景为LNMP架构下做的负载均衡。假如第一次登录是在A服务器上,第二次登录是在B服务器上,假如使用的是nginx代理upstream可以使用ip_hash;如果使用LVS呢?解决方法是:把session不存在服务器的磁盘上,而是存在memcached上去。memcached作为一个公共的服务器,任何web服务器都可以连接!
编辑php.ini添加两行(待测试)
vim /usr/local/php-fpm/etc/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 "
[root@xavi ~]# cat .mem_se.txt
<?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();
?>
打开php.ini文件,查找session
将其移动到虚拟主机目录中:
如何找到虚拟主机的目录呢
vim /usr/local/nginx/conf/vhost/aaa.com.conf
server
{
listen 80 default_server;
server_name aaa.com;
index index.html index.htm index.php;
root /data/nginx/default; //这是root路径
location ~ \.php$
{
include fastcgi_params;
fastcgi_pass unix:/tmp/xavi.sock;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME /data/nginx/default$fastcgi_script_name;
}
}
[root@xavi ~]# mv /root/.mem_se.txt /data/wwwroot/www.dsf.com
[root@xavi ~]# cd /data/wwwroot/www.dsf.com/
[root@xavi www.dsf.com]# cp .mem_se.txt 1.php
cp .mem_se.txt 1.php
未能验证成功,待查明原因
标签:memcached数据的导入和导出 php连接memcached
原文地址:http://blog.51cto.com/12995218/2125299