memcached安装配置
1.下载地址:
http://www.memcached.org/
2.Memcache的安装先决条件:先安装libevent
Libevent介绍:libevent是一个事件触发的网络库,适用于windows、linux、bsd等多种平台,内部使用select、epoll、kqueue等系统调用管理事件机制。著名的用于apache的php缓存库memcached据说也是libevent based,而且libevent在使用上可以做到跨平台,而且根据libevent官方网站上公布的数据统计,似乎也有着非凡的性能。
下载地址:http://monkey.org/~provos/libevent/
3.需要有gcc编译器
gcc -v 查看(yum install gcc方式可安装)
4.libevent安装(系统自带的时候就不用安装了。。)
[root@localhost]#tar -zxvf libevent-2.0.19-stable.tar.gz
[root@localhost]#cd libevent-2.0.19-stable
[root@localhost]#./configure --prefix=/usr/local/libevent
[root@localhost]#make
[root@localhost]#make install
测试是否安装成功
[root@localhost]# ls /usr/local/libevent/lib |grep libevent
5.安装memcached
[root@localhost]#tar -zxvf memcached-1.2.6.tar.gz
[root@localhost]#cd memcached-1.2.6
//指定安装路径到/usr/local/memcached/目录下,同时指定libevent的安装位置,系统自带libevent的时候就不用指定了。。
[root@localhost]# ./configure --prefix=/usr/local/memcached --with-libevent=/usr/local/libevent
[root@localhost]#make
[root@localhost]#make install
测试memcache:
[root@localhost]# ls -al/usr/local/memcached/bin
6.启动memcache
启动Memcache的服务器端:
[root@localhost]# /usr/local/memcached/bin/memcached -d -m 100 -u root -l 124.248.33.50 -p 12000 -c 1024-P /tmp/memcached.pid
memcached启动参数描述:
-d :启动一个守护进程,
-m:分配给Memcache使用的内存数量,单位是MB,默认是64MB,这里是100MB
-u :运行Memcache的用户,这里是root
-l :监听的服务器IP地址,这里指定了服务器的IP地址124.248.33.50
-p :设置Memcache监听的端口,默认是11211 注:-p(p为小写),这里使用12000
-c :设置最大并发连接数,默认是1024
-P :设置保存Memcache的pid文件 注:-P(P为大写),这里是保存在/tmp/memcached.pid,
Memcache可以搭建备份的。主要是-l -p -b参数的设置,具体没试验过
2)、如果要结束Memcache进程,执行:
[root@localhost]# kill cat /tmp/memcached.pid
7、 监测是否启动成功方式
我们可以使用telnet来对我们的memcache服务器进行访问
例如:telnet 124.248.33.50 12000
连接上后,直接敲击stats命令看当前缓存服务器状态
8、 设定memcache的telnet访问限制
请限定telnet的访问,使之只能在中转机上访问
9、 memcache的常见概念
memcached会预先分配内存,memcached分配内存方式称之为allocator,首先,这里有3个概念:
1 slab
2 page
3 chunk
解释一下,一般来说一个memcahced进程会预先将自己划分为若干个slab,每个slab下又有若干个page,每个page下又有多个chunk,如果我们把这3个咚咚看作是object得话,这是两个一对多得关系。再一般来说,slab得数量是有限得,几个,十几个,或者几十个,这个跟进程配置得内存有关。而每个slab下得page默认情况是1m,也就是说如果一个slab占用100m得内存得话,那么默认情况下这个slab所拥有得page得个数就是100,而chunk就是我们得数据存放得最终地方。
10、 Memcache的常用命令
Memcache常见的命令都在协议文件上:安装文件的的doc目录下的protocol.txt文件中有详细说明
1)、stats 查询状态命令:2)、version 查询版本号
3)、quit 退出命令
4)、stats items 显示各个slab的信息,包括chunk的大小、数目、使用情况等:stats slabs5)、显示各个slab中item的数目和最老item的年龄(最后一次访问距离现在的秒数):
6)、stats malloc 显示内存分配
7)、flush_all 清空缓存数据(其实是将所有缓存数据标记为过期)
如果要结束Memcache进程,执行:kill cat pid文件路径
11.java的测试代码
需要支持包memcached.jar、java_memcached-release_2.6.3.jar
package exp.vo;
import java.util.Date;
import com.danga.MemCached.*;
public class BasicTest {
private static final String POOL_NAME="test_pool";
protected static MemCachedClient mcc;
static {
//设置缓存服务器列表,当使用分布式缓存的时,可以指定多个缓存服务器
String[] servers =
{
"124.248.33.50:12000",
};
//与服务器列表中对应的各服务器的权重
Integer[] weights = {3};
//创建Socked连接池
SockIOPool pool = SockIOPool.getInstance(POOL_NAME);
//向连接池设定服务器和权重
pool.setServers( servers );
pool.setWeights( weights );
//连接池参数
pool.setInitConn( 5 );
pool.setMinConn( 5 );
pool.setMaxConn( 250 );
pool.setMaxIdle( 1000 * 60 * 60 * 6 );
// set the sleep for the maint thread
// it will wake up every x seconds and
// maintain the pool size
pool.setMaintSleep( 30 );
// set some TCP settings
// disable nagle
// set the read timeout to 3 secs
// and don't set a connect timeout
pool.setNagle( false );
pool.setSocketTO( 3000 );
pool.setSocketConnectTO( 0 );
// initialize the connection pool
pool.initialize();
// lets set some compression on for the client
// compress anything larger than 64k
mcc=new MemCachedClient(POOL_NAME);
mcc.setCompressEnable( true );
mcc.setCompressThreshold( 64 * 1024 );
}
public static void main(String[] args) throws Exception{
/System.currentTimeMillis()正式用时需删掉。不知道怎么偏移的~~~
mcc.set("msg","Hello,world!",new Date(System.currentTimeMillis()+1300));
Thread.sleep(500);
System.out.println(mcc.get("msg"));
}
}
一些telnet的命令
| Command | Description | Example |
|---|---|---|
| get | Reads a value | get mykey |
| set | Set a key unconditionally | set mykey 0 60 5 |
| add | Add a new key | add newkey 0 60 5 |
| replace | Overwrite existing key | replace key 0 60 5 |
| append | Append data to existing key | append key 0 60 15 |
| prepend | Prepend data to existing key | prepend key 0 60 15 |
| incr | Increments numerical key value by given number | incr mykey 2 |
| decr | Decrements numerical key value by given number | decr mykey 5 |
| delete | Deletes an existing key | delete mykey |
| flush_all | Invalidate specific items immediately | flush_all |
| Invalidate all items in n seconds | flush_all 900 | |
| stats | Prints general statistics | stats |
| Prints memory statistics | stats slabs | |
| Prints memory statistics | stats malloc | |
| Print higher level allocation statistics | stats items | |
| stats detail | ||
| stats sizes | ||
| Resets statistics | stats reset | |
| version | Prints server version. | version |
| verbosity | Increases log level | verbosity |
| quit | Terminate telnet session | quit |
1.安装libevent
[root@localhost source]# tar zxf libevent-2.0.19-stable.tar.gz
[root@localhost source]# cd libevent-2.0.19-stable
[root@localhost libevent-2.0.19-stable]# ./configure --prefix=/opt/libevent
[root@localhost libevent-2.0.19-stable]# make
[root@localhost libevent-2.0.19-stable]# make install
[root@localhost libevent-2.0.19-stable]# echo '/opt/libevent/lib' > /etc/ld.so.conf.d/libevent.conf
[root@localhost libevent-2.0.19-stable]# ldconfig
2.安装memcached
[root@localhost source]# tar zxf memcached-1.4.5.tar.gz
[root@localhost source]# cd memcached-1.4.5
[root@localhost memcached-1.4.5]# ./configure --prefix=/opt/memcached --with-libevent=/opt/libevent
[root@localhost memcached-1.4.5]# make
[root@localhost memcached-1.4.5]# make install
3.启动memcached
[root@localhost ~]# /opt/memcached/bin/memcached -d -m 1024 -u root -p 12000
本文详细介绍了memcached的安装前准备、安装步骤、启动与监控方法,以及常见概念和命令使用,帮助开发者快速掌握memcached的基本使用。

2622

被折叠的 条评论
为什么被折叠?



