转自:http://blog.csdn.net/zj7758/article/details/6200641
memcached安装
1. 下载, memcached需要先安装libevent
memcached的下载地址:http://www.danga.com/memcached/dist/memcached-1.2.6.tar.gzs
http://memcached.googlecode.com/files/memcached-1.4.5.tar.gz最新版
libevent的下载地址 :http://www.monkey.org/~provos/libevent-1.2.tar.gz
http://www.monkey.org/~provos/libevent/
#cd /usr/local
#mkdir memcached
#mkdir libevent
2. 安装libevent
# tar xzvf libevent-1.2.tar.gz
# cd libevent-1.2
# ./configure --prefix=/usr/local/libevent
# make
# make install
检查是否安装成功:
#cd /usr/local/libevent/lib
如果有libevent-1.3c.so.1 libevent-1.3c.so.1.0.3 libevent.a libevent.la libevent.so 这几个文件存在,说明安装成功
3.安装memcached
# tar xzvf memcached-1.2.6.tar.gz
# cd memcached-1.2.6
# ./configure --with-libevent=/usr/local/libevent --prefix=/usr/local/memcached
# make
# make install
检查是否安装成功:
#cd /usr/local/memcached/bin
如果memcached memcached-debug这2个文件存在,说明安装成功
4.执行
#cd /usr/local/memcached/bin
# ./memcached -h
如果出现:
1. memcached 1.2.6
2. -p TCP port number to listen on (default: 11211)
3. -U UDP port number to listen on (default: 0, off)
4. -s unix socket path to listen on (disables network support)
5. -l interface to listen on, default is INDRR_ANY
6. -d run as a daemon
7. -r maximize core file limit
8. -u assume identity of (only when run as root)
9. -m max memory to use for items in megabytes, default is 64 MB
10. -M return error on memory exhausted (rather than removing items)
11. -c max simultaneous connections, default is 1024
12. -k lock down all paged memory
13. -v verbose (print errors/warnings while in event loop)
14. -vv very verbose (also print client commands/reponses)
15. -h print this help and exit
16. -i print memcached and libevent license
17. -b run a managed instanced (mnemonic: buckets)
18. -P save PID in , only used with -d option
19. -f chunk size growth factor, default 1.25
20. -n minimum space allocated for key+value+flags, default 48
说明安装成功,并且路径配置正确。
有可能会出现:
memcached: error while loading shared libraries: libevent-1.2.so.1: cannot open shared object file: No such file or directory
说明 没有找到文件:libevent-1.2.so.1
解决办法:
第一步. 查看下lib路径:
[farman@localhost bin]$ LD_DEBUG=libs /usr/local/memcached/bin/memcached -v
31386: find library=libevent-1.2.so.1 [0]; searching
31386: search cache=/etc/ld.so.cache
31386: search path=/lib/tls/i686/sse2:/lib/tls/i686:/lib/tls/sse2:/lib/tls:/lib/i686/sse2:/lib/i686:/lib/sse2:/lib:/usr/lib/tls/i686/sse2:/usr/lib/tls/i686:/usr/lib/tls/sse2:/usr/lib/tls:/usr/lib/i686/sse2:/usr/lib/i686:/usr/lib/sse2:/usr/lib (system search path)
31386: trying file=/lib/tls/i686/sse2/libevent-1.2.so.1
31386: trying file=/lib/tls/i686/libevent-1.2.so.1
31386: trying file=/lib/tls/sse2/libevent-1.2.so.1
31386: trying file=/lib/tls/libevent-1.2.so.1
31386: trying file=/lib/i686/sse2/libevent-1.2.so.1
31386: trying file=/lib/i686/libevent-1.2.so.1
31386: trying file=/lib/sse2/libevent-1.2.so.1
31386: trying file=/lib/libevent-1.2.so.1
31386: trying file=/usr/lib/tls/i686/sse2/libevent-1.2.so.1
31386: trying file=/usr/lib/tls/i686/libevent-1.2.so.1
31386: trying file=/usr/lib/tls/sse2/libevent-1.2.so.1
31386: trying file=/usr/lib/tls/libevent-1.2.so.1
31386: trying file=/usr/lib/i686/sse2/libevent-1.2.so.1
31386: trying file=/usr/lib/i686/libevent-1.2.so.1
31386: trying file=/usr/lib/sse2/libevent-1.2.so.1
31386: trying file=/usr/lib/libevent-1.2.so.1
31386:
/home/farman/cacheinstall/bin/memcached: error while loading shared libraries: libevent-1.2.so.1: cannot open shared object file: No such file or directory
第二步. 将libevent-1.3c.so.1拷贝到任何一个列出的lib 下就可以了。
或者 软链接
[farman@localhost lib]$ su root
Password:
[root@localhost lib]# ln -s /home/farman/install/lib/libevent-1.2.so.1 /usr/lib/libevent-1.2.so.1
启动服务 :
[root@localhost lib]# exit
不能用root启动
前台启动测试
[farman@localhost bin]#./memcached -m 10 -p 12000 -c 256 -vv
后台启动
[farman@localhost bin]#./memcached -d -m 10 -p 12000 -c 256 -P /tmp/memcached.pid
./memcached -d -m 128 -u root -l 127.0.0.1 -p 11211 -c 64 -P /tmp/memcached.pid
参数说明:
-d选项是启动一个守护进程
-m是分配给Memcache使用的内存数量,单位是MB,我这里是10MB
-u是运行Memcache的用户,我这里是root
-l是监听的服务器IP地址
-p是设置Memcache监听的端口,最好是1024以上的端口
-c选项是最大运行的并发连接数,默认是1024,按照你服务器的负载量来设定
-P是设置保存Memcache的pid文件
转自:http://my.oschina.net/flynewton/blog/9694
核心步骤:wget下载libevent和memcached 1.先安装libevent。配置时需要指定安装路径 即./configure --prefix=/usr;然后make;然后make install; 2.安装memcached,配置时需要指定libevent的安装路径 即./configure --with-libevent=/usr;然后make;然后make install;
Ubuntu下只需sudo apt-get install memcached即可
memcached下载地址:http://www.danga.com/memcached/dist/memcached-1.4.0.tar.gz
Memcache用到了libevent这个库用于Socket的处理,所以还需要安装libevent。(如果你的系统已经安装了libevent,可以不用安装)
官网:http://www.monkey.org/~provos/libevent/
测试libevent是否安装成功:
测试是否成功安装memcached:
安装完成后的memcached在 /usr/local/bin/memcached
这时运行memcached -m 500 -p 11333, 有可能会出现:
memcached: error while loading shared libraries: libevent-1.2.so.1: cannot open shared object file: No such file or directory
这说明没有找到文件:libevent-1.2.so.1
解决办法如下:
第一步. 查看下lib路径:
which memcached 得到/usr/local/memcached/bin/memcached
LD_DEBUG=libs /usr/local/memcached/bin/memcached -v
第二步. 将libevent-1.2.so.1拷贝到任何一个列出的lib下。或者软链接到列出的lib下(很多情况下是/usr/lib下有该文件,需要把该文件拷贝到/usr/lib64/下)
memcached -d -m 10 -u root -l 192.168.40.4 -p 12000 -c 256 -P /tmp/memcached.pid
参数说明:
-d选项是启动一个守护进程
-m是分配给Memcache使用的内存数量,单位是MB,我这里是10MB
-u是运行Memcache的用户,我这里是root
-l是监听的服务器IP地址
-p是设置Memcache监听的端口,最好是1024以上的端口
-c选项是最大运行的并发连接数,默认是1024,按照你服务器的负载量来设定
-P是设置保存Memcache的pid文件
Memcached 操作:
telnet localhost 11334
set foo 0 0 3
bar
STORED
get foo
VALUE foo 0 3
bar
quit
Memcache命令:
存储命令: set/add/replace/append/prepend/cas
获取命令: get/gets
其他命令:delete/stats等
Shell操作memcache:
1、数据存储(假设key为foo,value为123)
printf "set foo 0 0 3\r\n123\r\n" | nc 127.0.0.1 11334
STORED
2、数据取回(假设key为foo)
printf "get foo\r\n" | nc 127.0.0.1 11334
VALUE foo 0 3
123
END
3、数值增加1(假设key为foo,并且value为正整数)
printf "incr foo 1\r\n" | nc 127.0.0.1 11334
124
4、数值减少3(假设key为foo,并且value为正整数)
printf "decr foo 3\r\n" | nc 127.0.0.1 11334
121
5、数据删除(假设key为foo)
printf "delete foo\r\n" | nc 127.0.0.1 11334
DELETED
6、查看Memcached状态
printf "stats\r\n" | nc 127.0.0.1 11334
STAT pid 22563
STAT uptime 90706
7、模拟top命令,查看Memcached状态:
watch "printf 'stats\r\n' | nc 127.0.0.1 11334"
watch "echo stats | nc 127.0.0.1 11334"