【转】centos安装memcached+php多服务器共享+session多机共享问题
参考博文:
centos安装memcached 源码安装
PHP如何将session保存到memcached中?如何分布式保存PHP session 这个参考价值高
centos系统为php安装memcached扩展 参考价值很高
session多机共享双向备份参考博文(很好):Repcached实现Memcached主从复制功能
一、安装Memcache
1. Yum安装Memcache
查找memcached
yum search memcached
该命令可以查询yum库中有关memcached的安装包信息,以下是搜寻结果截图:
安装 memcached
yum -y install memcached
该命令用来安装memcached,不必关心其中各个安装包之间的依赖。命令执行结果如下图所示:
(备注:如果执行yum –y install memcached报网络解析异常,则说明当前无法联网,或者说明dns服务器没有正确配置,如果因为dns没有配置原因,可以通过 vi /etc/resolv.conf
添加 nameserver 192.98.18.20(dns服务器地址) 来配置dns映射)
验证安装memcached
memcached –h
该命令主要的功能是提供memcached的帮助信息,罗列memcached命令有哪些接口以及接口参数。具体信息参考截图:
修改memcached配置文件
chkconfig --level 2345 memcached on 执行该命令将memcached加入linux服务。添加成功以后,你可以使用service memcached start|stop|restart等命令来启动、关闭、重启memcached服务。
修改memcached的参数,主要涉及memcached启动绑定的端口、用户、最大连接、最大内存等,命令:vi /etc/sysconfig/memcached 命令响应结果如截图:
通过vi命令即可修改上面各个参数。PORT,服务启动端口。USER,服务启动用户。MAXCONN,服务配置支持最大连接数。CACHESIZE,服务支持的最大内存,以M为单位。
重启memcached服务
修改上面的参数以后,需要重启memcached服务,不然参数是无法立即生效的。重启服务,只需执行:service memcached restart。服务重启以后,即可使用memcached-tool来检测memcached服务状态。
使用memcached-tool检测memcached服务
执行memcached-tool 127.0.0.1:11211 stats即可检测memcached的服务状态。如果报错,连接不上,那么说明memcached没有正确安装或者没有启动。执行正常,内容如截图所示:
1. 通过yum安装
yum -y install memcached #安装完成后执行: memcached -h #出现memcached帮助信息说明安装成功
加入启动服务
chkconfig --level 2345 memcached on
2.Memcached是基于libevent实现的,所以要首先确保已经安装libevent
yum install libevent
3. 配置Memcached
vim /etc/sysconfig/memcached #文件中内容如下,按需要修改: PORT="11211" #端口 USER="root" #用户名 MAXCONN="1024" #最大连接数 CACHESIZE="64" #内存大小 OPTIONS="" #附加参数
4. 安装libmemached
wget https://launchpad.net/libmemcached/1.0/1.0.16/+download/libmemcached-1.0.16.tar.gz tar -zxvf libmemcached-1.0.16.tar.gz cd libmemcached-1.0.16 ./configure -prefix=/usr/local/libmemcached -with-memcached make && make install
5. 安装php-devel
yum install php-devel
6. 安装PHP Memcached扩展
我的php是yum安装的 所以调整下路径
wget http://pecl.php.net/get/memcached-2.1.0.tgz tar -zxvf memcached-2.1.0.tgz cd memcached-2.1.0 /usr/bin/phpize ./configure -enable-memcached -with-php-config=/usr/bin/php-config -with-zlib-dir -with-libmemcached-dir=/usr/local/libmemcached -prefix=/usr/local/phpmemcached make && make install
配置 php.ini
[Session] ; Handler used to store/retrieve data. ; http://www.php.net/manual/en/session.configuration.php#ini.session.save-handler ;session.save_handler = files ;myadd extension = memcached.so session.save_handler = memcached session.save_path = "tcp://192.168.239.128:11211,tcp://192.168.239.130"
打开phpinfot.php 显示session信息如下:
测试配置成功与否:
<?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(); ?>
可以直接用sessionid 去 memcached 里查询一下:
telnet 127.0.0.1 11211
具体查询参考: 查看 memcached 的 key 與快取資料
由于 memcache 和mecached 命令有所不同 所以网上查的验证代码都测试通不过!
配置中碰到的错误
1、Warning: Unknown: Failed to write session data (memcached). Please verify that the current setting of session.save_path is correct (tcp://127.0.0.1:11211) in Unknown on line 0
决方案是把tcp://去掉
extension = memcached.so
session.save_handler=memcached session.save_path="127.0.0.1:11211"
cp://开头的是memcache扩展的写法,memchached扩展不需要。
2、Fatal error: Call to undefined function memcache_connect()
应该是memcached的函数 名称 或访问代码不对 待处理!
3、两台虚拟机 一台按照上面的配置 成功,另外一台配置出问题 telnet 后 没有session 数据 郁闷啊!
session多机共享问题
两台服务器互相拷贝session 实现同步双向备份。
由于之前已经安装了 memcached 本人先是把 memcached 卸载 然后按照 参考博文 Repcached实现Memcached主从复制功能
安装 但是考虑到一个问题,php 如何识别 memcached, 后来哥们不管那么多 直接 安装 竟然可以成功复制!
按照参考博文中的 方案2执行如下:
1. 安装libevent:
wget http://downloads.sourceforge.net/levent/libevent-2.0.22-stable.tar.gz tar zxvf libevent-2.0.22-stable.tar.gz cd libevent-2.0.22-stable ./configure --prefix=/usr/local/lib make && make install
2. 将libevent的库文件添加到动态库中:
vi /etc/ld.so.conf 在最后添加如下行: /usr/local/lib #此处为要添加的libevent库目录 在此注意:/usr/local/lib目录下是否有 libevent-2.0.so* 如果没有 那么libevent-2.0.so*路径为 :/usr/local/lib/lib 重新加载动态lib库 ldconfig
注意:如果无此步骤,在启动memcached时,会提示看不到libevent的库文件。
3. 测试libevent是否安装成功:
ls -al /usr/lib | grep libevent*
4. 创建启动帐号:
groupadd memcached
useradd -g memcached memcached
5. 创建PID进程目录并修改所有者:
mkdir /var/run/memcached
chown -R memcached.memcached /var/run/memcached
6. 安装整合memcached-repcached包:
wget http://softlayer-dal.dl.sourceforge.net/project/repcached/repcached/2.2.1-1.2.8/memcached-1.2.8-repcached-2.2.1.tar.gz cp memcached-1.2.8-repcached-2.2.1.tar.gz /usr/local cd /usr/local tar zxvf memcached-1.2.8-repcached-2.2.1.tar.gz mv memcached-1.2.8-repcached-2.2.1 memcached cd memcached ./configure --prefix=/usr/local/memcached --with-libevent=/usr/local/lib --enable-replication --enable-64bit
注意:默认memcached单个进程只支持到2G内存,需要更大内存支持的话,需要打开64位支持,编译的时候加参数: --enable-64bit
make && make install
提示编译出错:
make all-recursive make[1]: Entering directory `/usr/local/memcached' Making all in doc make[2]: Entering directory `/usr/local/memcached/doc' make[2]: Nothing to be done for `all'. make[2]: Leaving directory `/usr/local/memcached/doc' make[2]: Entering directory `/usr/local/memcached' gcc -DHAVE_CONFIG_H -I. -DNDEBUG -m64 -g -O2 -MT memcached-memcached.o -MD MP -MF .deps/memcached-memcached.Tpo -c -o memcached-memcached.o `test -f memcached.c' || echo './'`memcached.c memcached.c: In function ‘add_iov’: memcached.c:697: error: ‘IOV_MAX’ undeclared (first use in this function) memcached.c:697: error: (Each undeclared identifier is reported only once memcached.c:697: error: for each function it appears in.) make[2]: *** [memcached-memcached.o] Error 1 make[2]: Leaving directory `/usr/local/memcached' make[1]: *** [all-recursive] Error 1 make[1]: Leaving directory `/usr/local/memcached' make: *** [all] Error 2
解决方案:
vi memcached.c
将下面的几行
/* FreeBSD 4.x doesn't have IOV_MAX exposed. */ #ifndef IOV_MAX #if defined(__FreeBSD__) || defined(__APPLE__) # define IOV_MAX 1024 #endif #endif
修改为
/* FreeBSD 4.x doesn't have IOV_MAX exposed. */ #ifndef IOV_MAX # define IOV_MAX 1024 #endif
重新编译和安装:
make && make install
查看帮助:
./bin/memcached -h memcached 1.2.8 repcached 2.2.1 -p <num> TCP port number to listen on (default: 11211) -U <num> UDP port number to listen on (default: 11211, 0 is off) -s <file> unix socket path to listen on (disables network support) -a <mask> access mask for unix socket, in octal (default 0700) -l <ip_addr> interface to listen on, default is INDRR_ANY -d run as a daemon -r maximize core file limit -u <username> assume identity of <username> (only when run as root) -m <num> max memory to use for items in megabytes, default is 64 MB -M return error on memory exhausted (rather than removing items) -c <num> max simultaneous connections, default is 1024 -k lock down all paged memory. Note that there is a limit on how much memory you may lock. Trying to allocate more than that would fail, so be sure you set the limit correctly for the user you started the daemon with (not for -u <username> user; under sh this is done with 'ulimit -S -l NUM_KB'). -v verbose (print errors/warnings while in event loop) -vv very verbose (also print client commands/reponses) -h print this help and exit -i print memcached and libevent license -P <file> save PID in <file>, only used with -d option -f <factor> chunk size growth factor, default 1.25 -n <bytes> minimum space allocated for key+value+flags, default 48 -R Maximum number of requests per event limits the number of requests process for a given con nection to prevent starvation. default 20 -b Set the backlog queue limit (default 1024) -x <ip_addr> hostname or IP address of peer repcached -X <num:num> TCP port number for replication. <listen:connect> (default: 11212)
修改memcached目录所有者:
cd ..
chown -R memcached.memcached memcached
配置主从复制
参数说明:
-x 设置从哪个IP上进行同步。
-X 指定数据同步的端口。
Memcached默认服务端口是11211,默认同步监听端口是11212。
启动Master:
/usr/local/memcached/bin/memcached -d -m 100 -l 192.168.11.51 -p 11211 -u memcached -c 1024 -x 192.168.11.52 -X 11212 -P /var/run/memcached/memcached-rep.pid
查看监听端口:
netstat -tupln | grep memcached tcp 0 0 192.168.11.51:11211 0.0.0.0:* LISTEN 18634/memcached tcp 0 0 192.168.11.51:11212 0.0.0.0:* LISTEN 18634/memcached udp 0 0 192.168.11.51:11211 0.0.0.0:* 18634/memcached
启动Slave:
/usr/local/memcached/bin/memcached -d -m 100 -l 192.168.11.52 -p 11211 -u memcached -c 1024 -x 192.168.11.51 -X 11212 -P /var/run/memcached/memcached-rep.pid
说明:-x 192.168.11.51用于同步的Master的IP地址。
查看监听端口:
netstat -tupln | grep memcached tcp 0 0 192.168.11.52:11211 0.0.0.0:* LISTEN 24262/memcached udp 0 0 192.168.11.52:11211 0.0.0.0:* 24262/memcached