memcached 内存缓存服务器
hash一致算法
【确保sasl-devel安装包已经安装,可以使用ssl认证】
yum -y installcyrus-sasl-devel
【编译安装 libevent 事件驱动】
http://libevent.org/ 下载 libevent-2.0.20-stable.tar.gz ./configure --prefix=/usr/local/libevent make && make install make[3]: Entering directory`/home/libevent-2.0.20-stable/test‘ make[4]: Entering directory`/home/libevent-2.0.20-stable/test‘ make[4]: Nothing to be donefor `install-exec-am‘. make[4]: Nothing to be donefor `install-data-am‘. make[4]: Leaving directory`/home/libevent-2.0.20-stable/test‘ make[3]: Leaving directory`/home/libevent-2.0.20-stable/test‘ make[2]: Leaving directory`/home/libevent-2.0.20-stable/test‘ make[1]: Leaving directory `/home/libevent-2.0.20-stable‘ 安装结束
【memcached-1.4.24.tar.gz】
./configure --enable-sasl --prefix=/usr/local/memcached--with-libevent=/usr/local/libevent/ 这是一行 config.status: creatingMakefile config.status: creatingdoc/Makefile config.status: creating config.h config.status: executingdepfiles commands 检查通过 make && make install make[3]: Leaving directory`/home/memcached-1.4.24‘ make[2]: Leaving directory`/home/memcached-1.4.24‘ make[1]: Leaving directory`/home/memcached-1.4.24‘ 编译安装完成
【看看帮助】
/usr/local/memcached/bin/memcached -h
默认端口tcp udp 11211
-M return error on memory exhausted(rather than removing items)
指定为128M 最小20字节增长因子1—2之间 详细 nobody身份运行
/usr/local/memcached/bin/memcached-m 128 -n 20 -f 1.15 -vv -u nobody slab class 1: chunk size 72 perslab 14563空闲空间 【后台运行】 /usr/local/memcached/bin/memcached-m 128 -n 20 -f 1.15 -vv -u nobody -d
【看端口监听】
netstat-tunlp [root@localhost memcached-1.4.24]# netstat-tnulp | grep "11211" tcp 0 0 0.0.0.0:11211 0.0.0.0:* LISTEN 10743/memcached tcp 0 0 :::11211 :::* LISTEN 10743/memcached udp 0 0 0.0.0.0:11211 0.0.0.0:* 10743/memcached udp 0 0 :::11211 :::* 10743/memcached
成功开启
【安装telnet】yum -y install telnet
【测试连接11211】 telnet localhost 11211
查看状态 stats
add mykey 0 30 5 添加键mykey flags为0 超时30秒 5个字符,回车输入5个字符
get mykey 看看mykey里面是什么
get mykey 30秒后再来看
<36 get mykey
>36 END
30秒后不再返回给你,但并不清理
【杀掉所有memcached进程】killall memcached
【看看还有没有】netstat –tunlp
【写一个服务脚本】
memcachedSysV的startup脚本代码如下所示,将其建立为/etc/init.d/memcached文件: #!/bin/bash # # Init file for memcached # # chkconfig: - 86 14 # description: Distributed memory caching daemon # # processname: memcached # config: /etc/sysconfig/memcached . /etc/rc.d/init.d/functions ## Default variables PORT="11211" USER="nobody" MAXCONN="1024" CACHESIZE="64" OPTIONS="" [ -f /etc/sysconfig/memcached ] && ./etc/sysconfig/memcachedRETVAL=0 prog="/usr/local/memcached/bin/memcached" desc="Distributed memory caching" lockfile="/var/lock/subsys/memcached" start() { echo -n $"Starting $desc(memcached): " daemon $prog -d -p $PORT -u$USER -c $MAXCONN -m $CACHESIZE $OPTIONS RETVAL=$? echo [ $RETVAL -eq 0 ] &&touch $lockfile return $RETVAL } stop() { echo -n $"Shutting down$desc (memcached): " killproc $prog RETVAL=$? echo [ $RETVAL -eq 0 ] &&rm -f $lockfile return $RETVAL } restart() { stop start } reload() { echo -n $"Reloading$desc ($prog): " killproc $prog -HUP RETVAL=$? echo return $RETVAL } case "$1" in start) start ;; stop) stop ;; restart) restart ;; condrestart) [ -e $lockfile ] &&restart RETVAL=$? ;; reload) reload ;; status) status $prog RETVAL=$? ;; *) echo $"Usage: $0{start|stop|restart|condrestart|status}" RETVAL=1 esac exit $RETVAL
【给他权限】chmod +x/etc/init.d/memcached
【开启服务】servicememcached restart
【开启是否成功】netstat -tunlp
创建给脚本传参数的文件
vim /etc/sysconfig/memcached
ORT="11211"
USER="nobody"
MAXCONN="1024"
CACHESIZE="128"
OPTIONS=""
【测试当前连接11211】 telnet localhost 11211
查看状态 stats
limit_maxbytes 67108864 就是当前的64M
vim /etc/sysconfig/memcached
改CACHESIZE="128"
service memcached restart
telnet localhost 11211
STAT limit_maxbytes134217728 就是改好的128M
OK!配置已生效
【memcached的客户端】
vim fastcgi_params看看配置好了吗
启动nginx service nginxrestart
vim nginx.conf
改一改
server { listen 80; server_name localhost; #charset koi8-r; #access_log logs/host.access.log main; location / { root /web/htdocs; index index.php index.html; # proxy_pass http://172.16.0.104/; # if ($request_method = "PUT") { # proxy_pass http://172.16.0.105; # } }
启用php,改好路径 /web/htdocs;
location ~ \.php$ { index index.php index.html; root /web/htdocs; fastcgi_pass 127.0.0.1:9000; fastcgi_index index.php; fastcgi_param SCRIPT_FILENAME /scripts$fastcgi_script_name; include fastcgi_params; }
测试 首页 vim /web/htdocs/index.php
【安装memcache-2.2.7.tgz 客户端】不是memcached
【执行】/usr/local/php/bin/phpize
cd memcache-2.2.7
【检查】./configure --with-php-config=/usr/local/php/bin/php-config--enable-memcache
config.status: creatingconfig.h 检查Ok
【编译并安装】make && make install
关键的路径:/usr/local/php/lib/php/extensions/no-debug-non-zts-20100525/
[root@localhostmemcache-2.2.7]# mkdir /etc/php.d/
[root@localhostmemcache-2.2.7]# vim /etc/php.d/memcache.ini
mkdir/etc/php.d
vim/etc/php.d/memcache.ini
extension=/usr/local/php/lib/php/extensions/no-debug-non-zts-20100525/memcache.so
servicephp-fpm restart
【继续测试】
vim/web/htdocs/test.php
<?php
$mem = new Memcache;
$mem->connect("127.0.0.1", 11211) or die("Could not connect");
$version = $mem->getVersion();
echo "Server‘s version: ".$version."<br/>\n";
$mem->set(‘testkey‘, ‘Hello World‘, 0, 600) or die("Failed tosave data at the memcached server");
echo "Store data in the cache (data will expire in 600seconds)<br/>\n";
$get_result = $mem->get(‘testkey‘);
echo "$get_result is from memcached server.";
?>
【访问测试】http://172.16.0.102/test.php
会显示
Server‘s version: 1.4.24
Store data in the cache (data will expire in 600 seconds)
Hello World is from memcached server.
【telnet测试】
[root@localhost htdocs]#telnet localhost 11211
Trying ::1...
Connected to localhost.
Escape character is ‘^]‘.
get testkey
VALUE testkey 0 11
Hello World
END
quit
存储成功!
【配置php将会话保存至memcached中】比php保存在本机硬盘上快很多
编辑php.ini文件,确保如下两个参数的值分别如下所示:
[Session]
session.save_handler =memcache
session.save_path= "tcp://172.16.0.102:11211?persistent=1&weight=1&timeout=1&retry_interval=15"
注意改为本机IP
这是什么 session.name = PHPSESSID
cd /web/htdocs/
新建php页面setsess.php,为客户端设置启用session:
<?php
session_start();
if(!isset($_SESSION[‘www.MageEdu.com‘])) {
$_SESSION[‘www.MageEdu.com‘] = time();
}
print$_SESSION[‘www.MageEdu.com‘];
print"<br><br>";
print"Session ID: " . session_id();
?>
新建php页面showsess.php,获取当前用户的会话ID:
<?php
session_start();
$memcache_obj =new Memcache;
$memcache_obj->connect(‘172.16.200.11‘,11211);
$mysess=session_id();
var_dump($memcache_obj->get($mysess));
$memcache_obj->close();
?>
【测试】http://172.16.0.102/setsess.phpphp
http://172.16.0.102/showsess.php
【memadmin-master.zip】
解压 unzip memadmin-master.zip
mvmemadmin-master /web/htdocs/memaster
vim/web/htdocs/memaster/config.php 可以修改配置文件
浏览器访问 http://172.16.0.102/memaster/
默认帐号 admin
默认密码 admin
本文出自 “魂斗罗” 博客,请务必保留此出处http://990487026.blog.51cto.com/10133282/1722509
原文地址:http://990487026.blog.51cto.com/10133282/1722509