Ubuntu server 11.04安装memcache及php使用memcache来存储session的方法
本文实例讲述了Ubuntuserver11.04安装memcache及php使用memcache来存储session的方法。分享给大家供大家参考,具体如下:
1、首先安装memcache服务端:
sudoapt-getinstallmemcached
安装完成后系统自动启动了memcached服务占用11211端口
如需重新配置11211端口的服务需要关闭已开启的memcached服务
手动启动:
memcached-d-m128-p11211-umemcache
这里需要说明一下memcached服务的启动参数:
-p监听的端口
-l连接的IP地址,默认是本机
-dstart启动memcached服务
-drestart重起memcached服务
-dstop|shutdown关闭正在运行的memcached服务
-dinstall安装memcached服务
-duninstall卸载memcached服务
-u以的身份运行(仅在以root运行的时候有效)
-m最大内存使用,单位MB。默认64MB
-M内存耗尽时返回错误,而不是删除项
-c最大同时连接数,默认是1024
-f块大小增长因子,默认是1.25-n最小分配空间,key+value+flags默认是48
-h显示帮助
2、安装PHPMemecache客户端
$sudoapt-getinstallphp5-memcache
重启web服务器
测试memcache代码:
<?php $mem=newMemcache;//创建Memcache对象 $mem->connect("127.0.0.1",11211);//连接Memcache服务器 $val="这是一个Memcache的测试."; $key=md5($val); if(($k=$mem->get($key))){//判断是否获取到指定的key echo'fromcache:'.$k; }else{ echo'normal';//这里我们在实际使用中就需要替换成查询数据库并创建缓存. $mem->set($key,$val,0,120);//增加插入一条缓存,缓存时间为120s }
用memcache存储session
一般地,Session是以文本文件形式存储在服务器端的。如果使用Seesion,或者该PHP文件要调用Session变量,那么就必须在调用Session之前启动它,使用session_start()函数。其它都不需要你设置了,PHP自动完成Session文件的创建。其默认Session的存放路径是服务器的系统临时文件夹。
但是如果碰到大数据量的Sesstion的时候,使用基于文件的Session存取瓶颈可能都是在磁盘IO操作上,现在利用Memcached来保存Session数据,直接通过内存的方式,效率自然能够提高不少。在读写速度上会比files时快很多,而且在多个服务器需要共用session时会比较方便,将这些服务器都配置成使用同一组memcached服务器就可以,减少了额外的工作量。
其缺点是session数据都保存在memory中,一旦宕机,数据将会丢失。但对session数据来说并不是严重的问题。
如何用memcached来存储session呢?以下是基本的配置步骤:
1.安装memcached(略过,不清楚的筒子可以查看前面一篇文章:https://www.nhooo.com/article/85510.htm)
在phpinfo输出中的"Registeredsavehandlers"会有"filesusersqlite"。
2.修改配置文件,
①.在php.ini中全局设置(*需要重启服务器)
session.save_handler=memcache session.save_path="tcp://127.0.0.1:11211"
②.或者某个目录下的.htaccess:
php_valuesession.save_handler"memcache" php_valuesession.save_path"tcp://127.0.0.1:11211"
③.也可以在某个一个应用中:
ini_set("session.save_handler","memcache");
ini_set("session.save_path","tcp://127.0.0.1:11211");
注:使用多个memcachedserver时用逗号","隔开,并且和Memcache::addServer()文档中说明的一样,可以带额外的参数"persistent"、"weight"、"timeout"、"retry_interval"等等,类似这样的:
"tcp://host:port?persistent=1&weight=2,tcp://host2:port2"。
3.启动memcached
memcached-d-m10-uroot-l127.0.0.1-p11211-c256-P/tmp/memcached.pid
4.测试创建一个session
<?php //set_session.php session_start(); if(!isset($_SESSION['admin'])){ $_SESSION['admin']='wan'; } print$_SESSION['admin']; print"/n"; printsession_id(); ?>
5.用sessionid去memcached里查询一下
<?php //get_session.php $mem=newMemcache; $mem->connect("127.0.0.1",11211); var_dump($mem->get('0935216dbc0d721d629f89efb89affa6')); ?>
[root@localhosthtml]#/usr/local/webserver/php/bin/php-fget_session.php
输出结果:
string(16)"admin|s:3:"wan";"
证明session正常工作。
再次深入多域名网站利用MEMCACHE方式共享SESSION数据
通过了解SESSION的工作原理,我们可以发现,在默认情况下,各个服务器会各自分别对同一个客户端产生SESSIONID,如对于同一个用户浏览器,A服务器产生的SESSIONID是30de1e9de3192ba6ce2992d27a1b6a0a,而B服务器生成的则是c72665af28a8b14c0fe11afe3b59b51b。另外,PHP的SESSION数据都是分别保存在本服务器的文件系统中。
确定了问题所在之后,就可以着手进行解决了。想要共享SESSION数据,那就必须实现两个目标:一个是各个服务器对同一个客户端产生的SESSIONID必须相同,并且可通过同一个COOKIE进行传递,也就是说各个服务器必须可以读取同一个名为PHPSESSID的COOKIE;另一个是SESSION数据的存储方式/位置必须保证各个服务器都能够访问到。简单地说就是多服务器共享客户端的SESSIONID,同时还必须共享服务器端的SESSION数据。
第一个目标的实现其实很简单,只需要对COOKIE的域(domain)进行特殊地设置即可,默认情况下,COOKIE的域是当前服务器的域名/IP地址,而域不同的话,各个服务器所设置的COOKIE是不能相互访问的,如www.aaa.com的服务器是不能读写www.bbb.com服务器设置的COOKIE的。这里我们所说的同一网站的服务器有其特殊性,那就是他们同属于同一个一级域,如:tieba.xiaoyuan.com和www.xiaoyuan.com都属于域.xiaoyuan.com,那么我们就可以设置COOKIE的域为.xiaoyuan.com,这样tieba.xiaoyuan.com、www.xiaoyuan.com等等都可以访问此COOKIE。PHP代码中的设置方法如下:
<?php ini_set('session.cookie_domain','xiaoyuan.com'); ?>
这样各个服务器共享同一客户端SESSIONID的目的就达到了。
于是可以在a.domain.com下
session.php
<?php ini_set('session.cookie_domain','domain.com'); session_start(); if(!isset($_SESSION['admin'])){ $_SESSION['admin']='wan'; } print$_SESSION['admin']; print"\n"; printsession_id();
在b.domain.com下
session2.php
<?php ini_set('session.cookie_domain','domain.com'); session_start(); echo$_SESSION['admin'];
希望本文所述对大家Ubuntu平台上的php程序设计有所帮助。