yum install -y epel-release
yum install -y libevent memcached libmemcached
/etc/init.d/memcached start
netstat -lnp |grep mem
tcp 0 0 0.0.0.0:11211 0.0.0.0:* LISTEN
1549/memcached
ps aux |grep memcached
memcached -d -p 11211 -u memcached -m 64 -c 1024 -P /var/run/memcached/memcached.pid
-d选项是启动一个守护进程
-m是分配给Memcache使用的内存数量,单位是MB,这里是200MB
-u是运行Memcache的用户,如果当前为 root 的话,需要使用此参数指定用户。
-l是监听的服务器IP地址
-p是设置Memcache监听的端口,默认是11211
-c选项是最大运行的并发连接数,默认是1024
-P是设置保存Memcache的pid文件
相关的参数在vim /etc/init.d/memcached
vim /etc/sysconfig/memcached
PORT="11211"
USER="memcached"
MAXCONN="1024"
CACHESIZE="128"
OPTIONS="-l 192.168.1.124"
查看memcached运行状态
memcached-tool 127.0.0.1:11211 stats
或者 echo stats |nc 127.0.0.1 11211 yum install -y nc
若安装libmemcached后,可以使用命令
memstat --servers=127.0.0.1:11211 查看memcached服务状态
[root@bogon ~]# telnet 192.168.1.124 11211
Trying 192.168.1.124...
Connected to 192.168.1.124.
Escape character is '^]'.
set key1 0(标记) 300 3
asd
STORED
get key1
VALUE key1 0 3
asd
END
set key2 111 20 5
asdfg
STORED
get key2
VALUE key2 111 5
asdfg
END
[root@bogon ~]# memcached-tool 192.168.1.124:11211 display
# Item_Size Max_age Pages Count Full? Evicted Evict_Time OOM
1 96B 330s 1 2 no 0 0 0
set”表示按照相应的
,没有的时候增加,有的覆盖。
add”表示按照相应的
replace”表示按照相应的
PHP连接Memcached
PHP连接Memcached
先安装php的memcache扩展
tar zxf memcache-2.2.3.tgz
cd memcache-2.2.3
/usr/local/php/bin/phpize
./configure --with-php-config=/usr/local/php/bin/php-config
make && make install
安装完后会有类似这样的提示:
installing shared extensions: /usr/local/php/lib/php/extensions/no-debug-non-zts-
20090626/
把这个记住,
vim /usr/local/php/etc/php.ini
然后修改php.ini,把 extension_dir = "./" 修改为xtension_dir = "/usr/local/php/ext"
mkdir /usr/local/php/ext
cp /usr/local/php/lib/php/extensions/no-debug-non-zts-20090626/memcache.so
usr/local/php/ext
vim /usr/local/php/etc/php.ini
并添加一行extension="memcache.so"
/usr/local/php/bin/php -m 查看是否有没有memcache.so
下载测试php脚本
/usr/local/php/bin/php 1.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();
?>
memcached实现session共享
本实例是在lamp/lnmp环境下实现
编辑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 "
session.save_path = 如果memcached在其他的服务器上那么就写远程服务器的ip
Memcached session测试
mv .mem_se.txt /usr/local/apache2/htdocs/session.php
session_start();
if (!isset($_SESSION['TEST'])) {
$_SESSION['TEST'] = time();
}
$_SESSION['TEST3'] = time();
print $_SESSION['TEST'];
print "
";
print $_SESSION['TEST3'];
print "
";
print session_id();
?>
curl localhost/session.php
类似于1443702394
1443702394
i44nunao0g3o7vf2su0hnc5440
telnet 127.0.0.1 11211
get i44nunao0g3o7vf2su0hnc5440