8.memcached的安装?
memcached的安装比较简单,很多平台支持memcached,常见的有:linux、FreeBSD、Solaris、
windows。
这里以centos6.4为例进行说明。
软件地址:
Memcached下载地址:http://www.danga.com/memcached/
memcached其他下载地址:http://memcached.googlecode.com/files/memcached-1.4.15tar.gz
memcached其他下载地址:http://coode.google.com/p/memcached/downloads/list
libevent下载地址:http://monkey.org/provs/libevent/
网友安装资料:http://instance.iteye.com/blog/1691705
安装:
第一步:安装libevent
# wget http://www.mokey.org/~provos/libevent-1.4.1.-stable.tar.gz
# tar zxf libevent-1.4.13-stable.tar.gz
# cd libevent-1.4.13-stable
# ./configure
# make
# make install
# cd ../
Centos系统也可也使用域名安装libevent
第二步:安装Memcached
# wget http://memcached.googlecode.com/files/memcached-1.4.13tar.gz
# tar zxf memcached-1.4.13tar.gz
# cd memcached-1.4.13
# ./configure
# make
# make install
# cd ../
提示:memcache-2.2.5.tgz 是客户端
memcached-1.4.13.tar.gz是服务端 (看memcache后加不加d )
第三步:启动memcached
(1)配置ld.so.conf路径防止启动memcached时报错
# memcached --help
memcached:error while loading shared libraries: libevent-1.4.so.2:
cannot open shared object file:No such file or directory
解决:echo"/usr/local/lib" >> /etc/ld.so.conf
ldconfig
提示centos5.8不做上面的配置也行
(2)开始启动memcached
# which memcached
# memcached -m 1m -p 11212 -d -c 8192
(-m 为memcached分配的内存大小 -p指定对外提供服务端口号 -d 后台启动 -u指定用户 -c并发连接数 -f调优因子)
(3)多实例、直接改端口就行
(4)写入数据检查结果
向memcached中添加数据:键值对
key1-values1
key2-values2
memcached的命令语法:
set key 0 0(过期时间) 10(字节数)\r\n
printf "set key008 0 0 10\r\noldboy0987\r\n"|nc 127.0.0.1 11211
a.通过nc写入
写入# printf "set key008 0 0 10\r\noldboy0987\r\n"|nc 127.0.0.1 11211
取出# printf "get key008 0 0 10\r\noldboy0987\r\n"|nc 127.0.0.1 11211
删除# printf "deldet key008 0 0 10\r\noldboy0987\r\n"|nc 1
b.通过telnet写入
# telnet 127.0.0.1
(5)客户端的安装
# wget http://pecl.php.net/get/memcache-2.2.5.tgz
# tar zxvf memcache-2.2.5.tgz
# cd memcache-2.2.5.tgz
/application/php/bin/phpize
./configure --enable-memcache --with-php-config=/applicaton/php/bin/php-config --with-z
make
make install
会有:memcached.so的成功提示
(2)修改php.ini的文件
cd /application/php/lib/
extension_dir = "./"
修改为:
extension_dir = "memcach.so的文件路径"
并添加一行:
extension=memcache.so
(3)重启apache或者nginx服务使php的配置生效
apache:
# /application/apache/bin/apachectl -t
Syntax OK
# /application/apache/bin/apachectl graceful
如果是nginx:
# /application/nginx/sbin/nginx -t
# /application/nginx/sbin/nginx -s reload
# pkill php-fpm
# /application/php/sbin/php-fpm
(4)打开浏览器访问phpinfo页面测试是否安装成功
8.session共享
在web上加上
session.save_handler = memcache
session.save_path = "tcp://10.0.0.7:1121(ip与端口)"