4323213 发表于 2017-3-3 11:18:42

Memcached实现Session共享

一、memcached安装
1. memcached安装和启动、查看进程
    安装扩展源 yum install -y epel-release
    安装软件、组件、工具包 yum install -y libevent memcached libmemcached
    启动 /etc/init.d/memcached start
    进程 ps aux |grep memcached


   解释:
   -d启动一个守护进程
   -p监听的端口,默认11211
   -u运行memcached用户
   -m 内存大小,单位MB
   -c并发连接数,默认1024
   -P pid文件
   -l监听的服务器IP地址

2. 编辑配置文件/etc/sysconfig/memcached
    说明:配置文件中添加监听地址 -l 127.0.0.1




3. 查看memcached运行状态
1) 使用memcached自带命令查看,一般优良的memcache,cmd_hits/cmd_get命中率大于80%
       命令 memcached-tool 127.0.0.1:11211 stats

2) 使用nc工具命令查看
       说明:需yum install -y nc
       命令 echo status |nc 127.0.0.1 11211

3) 使用libmemcached工具的命令查看
       命令 memstat --servers=127.0.0.1:11211



二、memcached操作
   Memcached需在网站代码里指定,一般由开发人员操作,也可在测试和查看数据时,执行这些操作,一般用于php访问和存储

1. memcached登录
    telenet方式:Telnet 127.0.0.1 11211


2. memcached语法规则
    格式:
    <command name><key><flags><exptime><bytes> \r\n <data block> \r\n

   说明:
   1) \r\n在Windows下是Enter键
   2) <command name>可以是set(已存在则覆盖)、add(已存在则失败)、replace(不存在则失败)
   3) <key>
   4) <flags> 16位无符号十进制整数
   5) <exptime> 0表示永不过期,但可以被服务器算法LRU替换
   6) <bytes> 存储的字节数,空数据设为0
   7) <data block> 存储的数据

3. 操作数据
1) 存入和查看一条数据
      说明:键名为1;1表示键标记;80为过期时间;2为字节大小;回车输入2个字节的数据


2)替换一条数据


3) 删除一条数据


4. 查看chunk值
    命令:memcached-tool 127.0.0.1:11211 display



三、memcached连接实现共享
   Memcache作为php的一个扩展模块, 数据存到了memcached里面后,php通过memcache模块去和memcached服务交互

1. 下载和安装扩展工具memcache模块
    说明:源码编译时,需用到/usr/local/php/bin/phpize工具生成configure文件,如没有需yum install php-devel
    命令 wget http://www.apelearn.com/bbs/data/attachment/forum/memcache-2.2.3.tgz
            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



2. 编辑php配置文件/usr/local/php/etc/php.ini,指定扩展模块的目录extension_dir(默认)和模块文件,并重启查看是否加载/usr/local/php/bin/php -m







3. 编辑测试脚本1.php,并用/usr/local/php/bin/php测试与php的解析

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
<?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();
?>




4. memcached实现session共享(lamp/lnmp环境下实现方式有多种)

[*]/usr/local/php/etc/php.ini文件中添加

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


[*]httpd.conf(apahce)虚拟主机配置文件添加

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


[*]php-fpm.conf(nginx)对应的pool中添加

   php_value=memcache
      php_value="tcp:127.0.0.1:11211"

a. 编辑php配置文件vim /usr/local/php/etc/php.ini,填写session保存方式路径
    说明:如memcached服务器安装的远程机器上,在监听远程机器的IP
    内容:
   session.save_handler = memcache
   session.save_path = "tcp://127.0.0.1:11211"



b. 编辑php测试脚本session.php,移至网站目录下/data/www/
    内容:

1
2
3
4
5
6
7
8
9
10
11
12
<?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();
?>





c. 用curl或者浏览器访问session.php,获取返回的session值
    命令 curl -x127.0.0.1:80 172.16.111.195/session.php



d. telnet登录memcached服务器,查看本地保存的session信息与curl获取的一致



5. 通过phpinfo查看session相关配置




天下123 发表于 2017-3-17 16:06:33

学习了
页: [1]
查看完整版本: Memcached实现Session共享