olik 发表于 2015-12-14 09:42:18

memcached 内存缓存服务器

memcached 内存缓存服务器
hash一致算法

【确保sasl-devel安装包已经安装,可以使用ssl认证】

1
yum -y installcyrus-sasl-devel






【编译安装 libevent 事件驱动】

1
2
3
4
5
6
7
8
9
10
11
12
http://libevent.org/ 下载 libevent-2.0.20-stable.tar.gz
./configure --prefix=/usr/local/libevent
make && make install
make: Entering directory`/home/libevent-2.0.20-stable/test'
make: Entering directory`/home/libevent-2.0.20-stable/test'
make: Nothing to be donefor `install-exec-am'.
make: Nothing to be donefor `install-data-am'.
make: Leaving directory`/home/libevent-2.0.20-stable/test'
make: Leaving directory`/home/libevent-2.0.20-stable/test'
make: Leaving directory`/home/libevent-2.0.20-stable/test'
make: Leaving directory `/home/libevent-2.0.20-stable'
安装结束






【memcached-1.4.24.tar.gz】

1
2
3
4
5
6
7
8
9
10
11
./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: Leaving directory`/home/memcached-1.4.24'
make: Leaving directory`/home/memcached-1.4.24'
make: Leaving directory`/home/memcached-1.4.24'
编译安装完成





【看看帮助】

1
/usr/local/memcached/bin/memcached-h





默认端口tcp udp 11211

-M            return error on memory exhausted(rather than removing items)

指定为128M 最小20字节增长因子1—2之间详细 nobody身份运行

1
2
3
4
5
/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





【看端口监听】

1
2
3
4
5
6
netstat-tunlp
# 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
查看状态 statsadd mykey 0 30 5 添加键mykey flags为0 超时30秒 5个字符,回车输入5个字符get mykey   看看mykey里面是什么get mykey   30秒后再来看<36 get mykey>36 END30秒后不再返回给你,但并不清理【杀掉所有memcached进程】killall memcached
【看看还有没有】netstat –tunlp
【写一个服务脚本】

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
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/memcachedORT="11211"USER="nobody"MAXCONN="1024"CACHESIZE="128"OPTIONS=""【测试当前连接11211】telnet localhost 11211
查看状态 statslimit_maxbytes 67108864就是当前的64Mvim /etc/sysconfig/memcached改CACHESIZE="128"service memcached restarttelnet localhost 11211STAT limit_maxbytes134217728 就是改好的128M
OK!配置已生效
【memcached的客户端】
vim fastcgi_params看看配置好了吗启动nginx service nginxrestartvim nginx.conf改一改

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
server {
      listen       80;
      server_namelocalhost;

      #charset koi8-r;

      #access_loglogs/host.access.logmain;

      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;
1
2
3
4
5
6
7
8
location ~ \.php$ {
index index.php index.html;
             root         /web/htdocs;
             fastcgi_pass   127.0.0.1:9000;
             fastcgi_indexindex.php;
             fastcgi_paramSCRIPT_FILENAME/scripts$fastcgi_script_name;
             include      fastcgi_params;
         }





测试 首页vim /web/htdocs/index.php【访问测试http://172.16.0.102/】


【安装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
【访问测试http://172.16.0.102/】



【继续测试】
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测试】
#telnet localhost 11211Trying ::1...Connected to localhost.Escape character is '^]'.get testkeyVALUE testkey 0 11Hello WorldENDquit存储成功!



【配置php将会话保存至memcached中】比php保存在本机硬盘上快很多
编辑php.ini文件,确保如下两个参数的值分别如下所示:session.save_handler =memcachesession.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:<?phpsession_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:<?phpsession_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.phpphphttp://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
















页: [1]
查看完整版本: memcached 内存缓存服务器