首先:配置本地源,挂载光驱,安装编译器
Yum install gcc gcc-c++
wget http://monkey.org/~provos/libevent-1.4.9-stable.tar.gz
tar zxvf libevent-1.4.9-stable.tar.gz
cd libevent-1.4.9-stable/
Mkdir /usr/local/libevent
./configure --prefix=/usr/local/libevent
make && make install
2、编译安装Memcached:1、编译安装libevent:
wget http://danga.com/memcached/dist/memcached-1.2.6.tar.gz
tar zxvf memcached-1.2.6.tar.gz
cd memcached-1.2.6/
Mkdir /usr/local/memcached
./configure --prefix=/usr/local/memcached --with-libevent=/usr/local/libevent
make && make install
/usr/local/memcached/bin/memcached -m 100 -u root -d -l 127.0.0.1 -p 11211
3、启动memcached服务器:
[如果报错]
/usr/local/memcached/bin/memcached: error while loading shared libraries: libevent-1.4.so.2: cannot open shared object file: No such file or directory
那是你的memcache没有找到libevent-1.4.so.2 只需要传建一个软连接 到memcache找到的地方
ln -s /usr/local/你的libevent安装位置 /usr/ 要把快捷方式安装到哪里
现在软连接 已经创建完成
memcached参数详解:
-p TCP port number to listen on (default: 11211)
-U UDP port number to listen on (default: 0, off)
-s unix socket path to listen on (disables network support)
-a access mask for unix socket, in octal (default 0700)
-l interface to listen on, default is INDRR_ANY
-d run as a daemon
-r maximize core file limit
-u assume identity of (only when run as root)
-m max memory to use for items in megabytes, default is 64 MB
-M return error on memory exhausted (rather than removing items)
-c 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 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
-b run a managed instanced (mnemonic: buckets)
-P save PID in , only used with -d option
-f chunk size growth factor, default 1.25
-n minimum space allocated for key+value+flags, default 48
memcache代理服务器magent安装配置(192.168.1.246)
1、 magent是一款开源的Memcached代理服务器软件,其项目网址为:http://code.google.com/p/memagent/ 目前最新版本为0.6
2、 安装,先配置本地源,挂载光驱,安装编译器
Yum install gcc gcc-c++ libgnomeui-devel
wget http://monkey.org/~provos/libevent-1.4.9-stable.tar.gz //下载安装libevent
tar zxvf libevent-1.4.9-stable.tar.gz
cd libevent-1.4.9-stable/
Mkdir /usr/local/libevent
./configure
make && make install
4、 安装magent3、 编译安装libevent:
mkdir magent
cp magent-0.6.tar.gz ./magent
cd magent
tar xzvf ./magent-0.6.tar.gz
/sbin/ldconfig
sed -i "s/LIBS = -levent/LIBS = -levent -lm/g" Makefile
make
继续makemagent.c:729: 错误:‘SSIZE_MAX’未声明(在此函数内第一次使用)
magent.c:729: 错误:(即使在一个函数内多次出现,每个未声明的标识符在其
magent.c:729: 错误:所在的函数内也只报告一次。)
make: *** [magent.o] 错误 1
解决方案:
#在ketama.h开头加入(过滤掉特殊字符)
#ifndef SSIZE_MAX
#define SSIZE_MAX 32767
#endif
cp magent /usr/bin/magent
启动memagent:
/usr/bin/magent -u root -n 4096 -l 192.168.1.246 -p 12000 -s 192.168.1.247:11211 -s 192.168.1.248:11211 -b 192.168.1.249:11211
magent命令详解:
-h this message
-u uid
-g gid
-p port, default is 11211. (0 to disable tcp support)
-s ip:port, set memcached server ip and port
-b ip:port, set backup memcached server ip and port
-l ip, local bind ip address, default is 0.0.0.0
-n number, set max connections, default is 4096
-D do not go to background
-k use ketama key allocation algorithm
-f file, unix socket path to listen on. default is off
-i number, max keep alive connections for one memcached server, default is 20
-v verbose
PHP开启扩展php_memcache.Dll注意版本的问题
//phpinfo(); //查看扩展是否开启
$mem = new Memcache();
//直接连接代理服务器
$mem->connect('192.168.1.246',12000) or die('connect fail!<br>');
echo "connect success!<br>";
for($i=0;$i<10;$i++){
if($mem->set('key'.$i , 'data'.$i , 0 , 3600)){
echo "set ok!";
}else{
echo "set false!";
}
}
使用memadmin监控memcached服务器的存储状态:报错提示:
Notice: Memcache::set(): Server 192.168.1.246 (tcp 12000) failed with: Failed reading line from stream (0)
原因是:代理magent没有连接到memcached服务器
解决方案:
启动memcached的时候,不能使用 –l 127.0.0.1或者-l localhost,要设置当前IP
备份服务器bak_249请求次数10次,s1_247五次,s2_248五次,memadmin统计信息如下图:
接着测试magent +memcached的强大功能
环境说明: centeros 5.4(虚拟机) IP:192.168.128.133
测试客户端:win32
先在133上面开5个memcached端口
依次执行
memcached -m 1 -u root -d -l 192.168.128.133 -p 10001
memcached -m 1 -u root -d -l 192.168.128.133 -p 10002
memcached -m 1 -u root -d -l 192.168.128.133 -p 10003
memcached -m 1 -u root -d -l 192.168.128.133 -p 10004
memcached -m 1 -u root -d -l 192.168.128.133 -p 10005
先看看是否启动监听端口成功
ps -ef | grep memcached
发现结果6行,最后一行是执行这个查看命令的 说明成功
接着启动magent 设置启动参数 -u当然是启动账号 -n是最大连接数,-l是magent监听的IP -p是magent监听的端口
后面的格式依次是 -s IP:端口 代表是主服务器 然后指定IP:端口的格式
-b IP:端口的意思是 备用服务器,
这里我设置了10001,10002,10003 为主Memcached服务器
10004,10005 为memcached备份服务器
magent -u root -n 51200 -l 192.168.128.133 -p 12000 -s 192.168.128.133:10001 -s 192.168.128.133:10002 -s 192.168.128.133:10003 -b 192.168.128.133:10004 -b 192.168.128.133:10005
执行命令后 可以先查看是否启动成功
ps -ef | grep magent
出现2行 OK,继续
在XP客户端上测试
直接连接magent 端口 12000
telnet 192.168.128.133 12000
执行stats
说明magent 里面已经负载了3个memcached 端口
接着我们继续set
set key1 0 0 1
1
STORED
set key2 0 0 1
2
STORED
set key3 0 0 1
3
STORED
好了 依次设置了3个key ,这里说明下参数 <command name> <key> <flags> <exptime> <bytes>\r\n
详细的解释大家请看:http://blog.zol.com.cn/737/article_736087.html
既然设置了3个key,我们来读取看
执行命令get key1 get key2 get key3
依次返回了
到此,一个缓存写入已经存取的过程就完成了,但是我们要了解其中的过程呀,哈哈
然后我们直接登陆memcached的端口get数据看 它是怎么样保存的
先telnet 192.168.128.133 10001
执行stats
STAT pid 7496
STAT uptime 894
STAT time 1319202978
STAT version 1.2.6
STAT pointer_size 32
STAT rusage_user 0.000000
STAT rusage_system 0.001999
STAT curr_items 1
STAT total_items 1
STAT bytes 54
STAT curr_connections 3
STAT total_connections 4
STAT connection_structures 4
STAT cmd_get 1
STAT cmd_set 1
STAT get_hits 1
STAT get_misses 0
STAT evictions 0
STAT bytes_read 36
STAT bytes_written 32
STAT limit_maxbytes 1048576
STAT threads 1\STAT threads 1\
说明10001只缓存了1个key
我们看是存的哪一个
get key1
VALUE key1 0 1
1
END
OK存的key1
再telnet 192.168.128.133 10002
直接
get key1
END
get key2
VALUE key2 0 1
2
END
get key3
END
get key4
END
get key5
END
说明10002上缓存的key2啊,
这样不难看出10003上应该是缓存的key3了
试试
telnet 192.168.128.133 10003
然后
get key1
END
get key2
END
get key3
VALUE key3 0 1
3
END
get key4
END
get key5
END
到此 知道了magent是根据自己的算法 把要缓存的内容依次分发到后端的memcached的,至于具体的算法就不知道了,我个人认为应该是依次分发的吧,当然此memcached存活的前提下,
知道了主服务器是分发负载的,不知道备用服务器是不是这样,
进telnet 192.168.128.133 10004
依次
get key1
END
get key2
VALUE key2 0 1
2
END
get key3
END
get key4
END
get key5
END
发现 备用服务10004 并没有全部缓存3个key,就是之前的key1 key2 key3
再进 telnet 192.168.128.133 10005
get key1
VALUE key1 0 1
1
END
get key2
END
get key3
VALUE key3 0 1
3
END
get key4
END
get key5
END
看来备用服务器也是实现了负载啊,哈哈,估计算法还是跟主服务器的一样吧,
然后我把主服务器的10001 10002 10003的进程全部kill了
root 7496 1 0 21:01 ? 00:00:00 memcached -m 1 -u root -d -l 192.168.128.133 -p 10001
root 7498 1 0 21:01 ? 00:00:00 memcached -m 1 -u root -d -l 192.168.128.133 -p 10002
root 7500 1 0 21:01 ? 00:00:00 memcached -m 1 -u root -d -l 192.168.128.133 -p 10003kill 7496 7498 7500
再在magent的端口上get key1
VALUE key1 0 1
1
END
get key2
VALUE key2 0 1
2
END
get key3
VALUE key3 0 1
3
END
并不影响取值。
不过如果备用服务器也死了的话,就无力了,
到此测试结束咯
转载于:https://blog.51cto.com/dabaobei/1642056