1,模拟web集群
我启动了二个memcached进程,分别模拟二台服务器
/usr/local/bin/memcached -d -m 20 -u zhangy -p 12000 -P ./memcached.pid
/usr/local/bin/memcached -d -m 20 -u zhangy -p 13000 -P ./mem.pid
2,修改php的配置
vi /usr/local/php/lib/php.ini
session.save_handler = “memcache”
memcache.hash_strategy = “consistent”
session.save_path = “tcp://127.0.0.1:13000?weight=10,tcp://127.0.0.1:12000″
说明:第一行,session的储存方式是memcache;第二行,memcache的hash算法是consistent;第三行,session储存的地位;
3,重起apache
查看phpinfo
session
Session Support | enabled |
Registered save handlers | files user sqlite memcache |
Registered serializer handlers | php php_binary |
紧接着下面是
session.save_path | tcp://127.0.0.1:13000,tcp://127.0.0.1:12000 | tcp://127.0.0.1:13000,tcp://127.0.0.1:12000 |
4,做个简单测试
a),准备文件session.php
- <?php
- session_start();
- $_SESSION[‘username’] = “abcabc”;
- echo session_id();
- ?>
<?php session_start(); $_SESSION['username'] = "abcabc"; echo session_id(); ?>
b),显示session内容文件
- <?php
- $mem = new Memcache;
- $mem->addServer(“127.0.0.1”,12000)or die (“Could not add server 12000”);
- $mem->addServer(“127.0.0.1”,13000)or die (“Could not add server 13000”);
- $val = $mem->get(‘qp0mrob2ovcqle3u4lbr4obsa5’); //echo session_id();
- echo $val;
- ?>
<?php $mem = new Memcache; $mem->addServer("127.0.0.1",12000)or die ("Could not add server 12000"); $mem->addServer("127.0.0.1",13000)or die ("Could not add server 13000"); $val = $mem->get('qp0mrob2ovcqle3u4lbr4obsa5'); //echo session_id(); echo $val; ?>
c),结果是username|s:6:”abcabc”;