memcached安装指导

来源:互联网 发布:io域名注册 编辑:程序博客网 时间:2024/05/20 14:27
memcached是什么?
memcached 是以LiveJournal 旗下Danga Interactive 公司的Brad Fitzpatric 为首开发的一款软件。
现在已成为 mixi、 hatena、 Facebook、 Vox、LiveJournal等众多服务中提高Web应用扩展性的重要因素。
许多Web应用都将数据保存到RDBMS中,应用服务器从中读取数据并在浏览器中显示。但随着数据量的增大、访问的集中,就会出现RDBMS的负担加重、数据库响应恶化、网站显示延迟等重大影响。这时就该memcached大显身手了。memcached是高性能的分布式内存缓存服务器。一般的使用目的是,通过缓存数据库查询结果,减少数据库访问次数,以提高动态Web应用的速度、提高可扩展性。

magent是一款开源的Memcached代理服务器软件,其项目网址为: http://code.google.com/p/memagent/
一、安装步骤:
1.编译安装libevent:
roothomes$ wget http://monkey.org/~provos/libevent-1.4.9-stable.tar.gz
roothomes$ tar zxvf libevent-1.4.9-stable.tar.gz
roothomes$ cd libevent-1.4.9-stable/
roothomes$ ./configure --prefix=/usr
roothomes$ make && make install
roothomes$ cd ../

2.编译安装Memcached:
roothomes$ wget http://danga.com/memcached/dist/memcached-1.2.6.tar.gz
roothomes$ tar zxvf memcached-1.2.6.tar.gz
roothomes$ cd memcached-1.2.6/
roothomes$ ./configure --with-libevent=/usr
roothomes$ make && make install
roothomes$ cd ../
3.编译安装magent:
roothomes$ mkdir magent
roothomes$ cd magent/
roothomes$ wget http://memagent.googlecode.com/files/magent-0.5.tar.gz
roothomes$ tar zxvf magent-0.5.tar.gz
roothomes$ /sbin/ldconfig
roothomes$ sed -i "s#LIBS = -levent#LIBS = -levent -lm#g" Makefile
roothomes$ make
roothomes$ cp magent /usr/bin/magent
roothomes$ cd ../
二、启动memcached和magent实例:
roothomes$ memcached -m 10 -u root -d -l 127.0.0.1 -p 11211
roothomes$ memcached -m 10 -u root -d -l 127.0.0.1 -p 11212
roothomes$ memcached -m 10 -u root -d -l 127.0.0.1 -p 11213
roothomes$ magent -u root -n 51200 -l 127.0.0.1 -p 12000 -s 127.0.0.1:11211 -s 127.0.0.1:11212 -b 127.0.0.1:11213

-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
1、分别在11211、11212、11213端口启动3个Memcached进程,在12000端口开启magent代理程序;
2、在magent代理程序设置11211、11212端口为主Memcached,11213端口为备份Memcached;
3、连接上12000的magent进行数据存储,写入命令式是set KEYNAME;
如set key1和set key2,根据哈希算法,key1被写入11212和11213端口的Memcached,key2被写入11212和11213端口的Memcached;
4、当11211、11212端口的Memcached死掉,连接到12000端口的magent取数据,数据会从11213端口的Memcached取出;
5、当11211、11212端口的Memcached重启复活,连接到12000端口,magent会从11211或11212端口的 Memcached取数据;
由于这两台Memcached重启后无数据,因此magent取得的将是空值,尽管11213端口的Memcached还有数据(此问题尚待改进)。


三、实例程序练习
1.在magent里面填入数据:
roothomes$ telnet 127.0.0.1 12000
Trying 127.0.0.1...
Connected to 127.0.0.1.
Escape character is '^]'.
stats
memcached agent v0.4
matrix 1 -> 127.0.0.1:11211, pool size 0
matrix 2 -> 127.0.0.1:11212, pool size 0
END
set key1 0 0 8
zhangyan
STORED
set key2 0 0 8
zhangyan
STORED
quit
Connection closed by foreign host.
2.在3个memcached里面获取数据:
roothomes$ telnet 127.0.0.1 11211
Trying 127.0.0.1...
Connected to 127.0.0.1.
Escape character is '^]'.
get key1
END
get key2
VALUE key2 0 8
zhangyan
END
quit
Connection closed by foreign host.

roothomes$ telnet 127.0.0.1 11212
Trying 127.0.0.1...
Connected to 127.0.0.1.
Escape character is '^]'.
get key1
VALUE key1 0 8
zhangyan
END
get key2
END
quit
Connection closed by foreign host.

roothomes$ telnet 127.0.0.1 11213
Trying 127.0.0.1...
Connected to 127.0.0.1.
Escape character is '^]'.
get key1
VALUE key1 0 8
zhangyan
END
get key2
VALUE key2 0 8
zhangyan
END
quit
Connection closed by foreign host.
总结:
在magent里面添加的数据被动态分给memcached里面存储;
其中11213里面会存储所有的内容,而11211和11212里面存储不同的内容。

3.模拟11211、11212端口的Memcached死掉:
杀掉memcached 11211。
通过memcached 12000与memcached 11213可以查询所有内容。
通过memcached 11212只可以查询其中的一些。
roothomes$ ps -ef | grep memcached
root 26367 1 0 11:06 ? 00:00:00 memcached -m 10 -u root -d -l 127.0.0.1 -p 11211
root 26370 1 0 11:06 ? 00:00:00 memcached -m 10 -u root -d -l 127.0.0.1 -p 11212
root 26372 1 0 11:06 ? 00:00:00 memcached -m 10 -u root -d -l 127.0.0.1 -p 11213
root 26482 26330 0 11:27 pts/0 00:00:00 grep memcached
roothomes$ kill 26367
roothomes$ ps -ef | grep memcached
root 26370 1 0 11:06 ? 00:00:00 memcached -m 10 -u root -d -l 127.0.0.1 -p 11212
root 26372 1 0 11:06 ? 00:00:00 memcached -m 10 -u root -d -l 127.0.0.1 -p 11213
root 26484 26330 0 11:27 pts/0 00:00:00 grep memcached
roothomes$ telnet 127.0.0.1 12000
Trying 127.0.0.1...
Connected to 127.0.0.1.
Escape character is '^]'.
stats
memcached agent v0.4
matrix 1 -> 127.0.0.1:11211, pool size 1
matrix 2 -> 127.0.0.1:11212, pool size 1
END
get key1
VALUE key1 0 10
zhangyangg
END
get key2
VALUE key2 0 8
zhangyan
END
quit
Connection closed by foreign host.
roothomes$ telnet 127.0.0.1 11212
Trying 127.0.0.1...
Connected to 127.0.0.1.
Escape character is '^]'.
get key1
VALUE key1 0 10
zhangyangg
END
get key2
END
quit
Connection closed by foreign host.
roothomes$ telnet 127.0.0.1 11213
Trying 127.0.0.1...
Connected to 127.0.0.1.
Escape character is '^]'.
get key1
VALUE key1 0 10
zhangyangg
END
get key2
VALUE key2 0 8
zhangyan
END
quit
Connection closed by foreign host.
roothomes$

重新复活memcached 11211:
roothomes$ memcached -m 10 -u root -d -l 127.0.0.1 -p 11211
在 magent 12000会自动分配存储内容到各个memcached里面。




















原创粉丝点击