安装memcached

[root@localhost ~]# yum install -y epel-release ; yum install -y libevent memcached libmemcached

启动程序

[root@localhost ~]# /etc/init.d/memcached start


启动参数

[root@localhost ~]# cat /etc/sysconfig/memcached

# 端口

PORT="11211"

# 用户

USER="memcached"

# 并发数

MAXCONN="1024"

# 内存

CACHESIZE="64"

# 选项

OPTIONS=""

# 启动参数说明,启动参数用在OPTIONS=""配置项,如OPTIONS="-l 127.0.0.1"

# -d 启动一个守护进程

# -m 分配给Memcached 使用的内存数量

# -l 监听IP

# -p 监听端口,默认是11211

# -c 最大运行并发数,默认是1024

# -P 大写P,设置保存Memcached 的PID文件


查看进程,查看启动参数

[root@localhost ~]# ps aux | grep memcached

498       1306  0.0  0.3  56276   820 ?        Ssl  18:46   0:00 memcached -d -p 11211 -u memcached -m 64 -c 1024 -P /var/run/memcached/memcached.pid -l 127.0.0.1

查看监听端口

[root@localhost ~]# netstat -nlp | grep memcached

tcp        0      0 127.0.0.1:11211             0.0.0.0:*                   LISTEN      1551/memcached

udp        0      0 127.0.0.1:11211             0.0.0.0:*                               1551/memcached

查看运行状态

[root@localhost ~]# memcached-tool 127.0.0.1:11211 stats

[root@localhost ~]# echo stats | nc 127.0.0.1 11211

[root@localhost ~]# memstat --servers=127.0.0.1:11211

其中nc和memstat需要安装工具

[root@localhost ~]# yum install -y nc libmemcached

登陆,需要安装工具telnet

[root@localhost ~]# yum install -y telnet

[root@localhost ~]# telnet 127.0.0.1 11211


简单数据存/取过程

[root@localhost ~]# telnet 127.0.0.1 11211

Trying 127.0.0.1...

Connected to 127.0.0.1.

Escape character is '^]'.

# set 表示设置键属性

# key1 表示键名

# 0 表示标记,可以随意填

# 300 表示过期时间

# 3 表示值位数

set key1 0 300 3

ab1

# 输入完值后,加车,出现STORED表示存储完毕

STORED

# get key1 表示查看键key1的值

get key1

# 显示key1键属性,再显示key1键值

VALUE key1 0 3

ab1

# 当出现END表示结束

END

# 同上

set key2 219 5 7

# 上面设置值位数为7,输入的为6位,显示错误

asdqas

CLIENT_ERROR bad data chunk

ERROR

# 退出

quit

Connection closed by foreign host.


Mamcached 语法规则

\r\n \r\n

注:\r\n在windows下是Enter键。

a) 可以是"set","add","replace"。

"set" 表示按照相应的存储该数据,如果没有就增加,有就覆盖。

"add" 表示按照相应的添加该数据,但如果该已经存在,则会操作失败。

"replace" 表示按照相应的替换数据,但是如果该不存在则操作失败。

b)客户端需要保存数据的key。

c) 是一个16位的无符号的整数(以十进制的方式表示)。

该标志将和需要存储的数据一起存储,并在客户端get数据时返回。

客户可以将此标志用做特殊用途,此标志对服务器来说是不透明的。

d) 过期时间。

若为0表示存储的数据永远不过期(但可被服务器算法:LRU等替换)。

如果非0(unix时间或者距离此时的秒数),当过期后,服务器可以保证用户得不到该数据(以服务器时间为标准)。

e) 需要存储的字节数,当用户希望存储空数据时可以为0。

f) 需要存储的内容,输入完成后,最后客户端需要加上"\r\n"(直接点击Enter)作为"命令头"的结束标志。


[root@localhost ~]# telnet 127.0.0.1 11211

Trying 127.0.0.1...

Connected to 127.0.0.1.

Escape character is '^]'.

# 存储数据

set key1 1122 120 3

abc

STORED

# 读取数据

get key1

VALUE key1 1122 3

abc

END

# 替换数据

replace key1 3344 240 2

a1

STORED

get key1

VALUE key1 3344 2

a1

END

# 删除数据

delete key1

DELETED


php连接memcache

[root@localhost ~]# wget http://www.lishiming.net/data/p_w_upload/forum/memcache-2.2.3.tgz

[root@localhost ~]# tar zxvf memcache-2.2.3.tgz

[root@localhost ~]# cd memcache-2.2.3

[root@localhost memcache-2.2.3]# yum install m4 autoconf

[root@localhost memcache-2.2.3]# /usr/local/php/bin/phpize

Configuring for:

PHP Api Version:         20090626

Zend Module Api No:      20090626

Zend Extension Api No:   220090626

[root@localhost memcache-2.2.3]# ./configure --with-php-config=/usr/local/php/bin/php-config

[root@localhost memcache-2.2.3]# make && make install

记住下面这行

Installing shared extensions:     /usr/local/php/lib/php/extensions/no-debug-non-zts-20090626/

[root@localhost memcache-2.2.3]# vi /usr/local/php/etc/php.ini

; extension_dir = "./"

修改为

extension_dir = "/usr/local/php/lib/php/extensions/no-debug-non-zts-20090626/"

并添加行

extension="memcache.so"

检查有没有添加成功

[root@localhost memcache-2.2.3]# /usr/local/php/bin/php -m | grep memcache

下载测试文档

[root@localhost memcache-2.2.3]# curl www.apelearn.com/study_v2/.memcache.txt > 1.php

查看1.php内容

[root@localhost memcache-2.2.3]# cat 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();

?>

测试,如果结果如下,表示正常

[root@localhost memcache-2.2.3]# /usr/local/php/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:


memcached 实现session 共享

===============================================================================

LNMP环境确认nginx有没有启用虚拟主机配置

[root@localhost memcache-2.2.3]# cat /usr/local/nginx/conf/nginx.conf | grep "include vhosts"

    include vhosts/*.conf;

确认网站根目录

[root@localhost memcache-2.2.3]# cat /usr/local/nginx/conf/vhosts/myweb.conf | grep root

    root /usr/local/nginx/html;

==============================================================================

LAMP环境确认apache有没有启用虚拟主机配置

[root@localhost memcache-2.2.3]# cat /usr/local/apache2/conf/httpd.conf | grep ^"Include conf"

Include conf/extra/httpd-vhosts.conf

确认网站根目录

[root@localhost memcache-2.2.3]# cat /usr/local/apache2/conf/extra/httpd-vhosts.conf | grep DocumentRoot

=====================================================================================

以下3个方法分别对应php、apache、nginx,请根据不同的环境(LAMP或LNMP)选择其中一个方法配置,

一、通用。更改php配置文件

[root@localhost memcache-2.2.3]# vi /usr/local/php/etc/php.ini

把下面这行注释起来或者删掉

session.save_handler = files

添加以下两行

session.save_handler = "memcache"

127.0.0.1 为服务器IP,需要修改为实际环境中对应的IP

session.save_path = "tcp://127.0.0.1:11211"

二、LAMP环境。更改apache虚拟主机文件,在内加入内容

[root@localhost memcache-2.2.3]# vi /usr/local/apache2/conf/extra/httpd-vhosts.conf

php_value session.save_handler "memcache"

php_value session.save_path "tcp://127.0.0.1:11211"

三、LNMP环境。

[root@localhost memcache-2.2.3]# vi /usr/local/php/etc/php-fpm.conf

在对应的pool内添加

php_value[session.save_handler] = memcache

php_value[session.save_path] = "tcp://127.0.0.1:11211"

====================================================================================

LAMP环境写入测试文档

[root@localhost memcache-2.2.3]# vi /usr/local/apache2/htdocs/masker.net.cn/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();

?>

[root@localhost memcache-2.2.3]# /usr/local/apache2/bin/apachectl restart

==================================================================================

LNMP环境写入测试文档

[root@localhost memcache-2.2.3]# vi /usr/local/nginx/html/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();

?>

[root@localhost memcache-2.2.3]# /etc/init.d/nginx restart

=================================================================================

[root@localhost memcache-2.2.3]# curl localhost/session.php

1452682942

1452682942

7aloo42a8dohhv3udqo1uac0p6

[root@localhost memcache-2.2.3]# curl -x127.0.0.1:80 localhost/session.php

1452684165

1452684165

99qfsqo263jdmij0r879vqolc4

[root@localhost memcache-2.2.3]# telnet 127.0.0.1 11211

Trying 127.0.0.1...

Connected to 127.0.0.1.

Escape character is '^]'.

get q4c8gd5tj2jm5qdsvdeojv3ho0

VALUE q4c8gd5tj2jm5qdsvdeojv3ho0 0 37

TEST|i:1452683072;TEST3|i:1452683072;

END

quit

Connection closed by foreign host.


暂时到这里。