ubuntu 15.10 安装 Memcache
1, Memcached需要libevent支持,安装libevent。
Debian/Ubuntu:
sudo apt-get install libevent-dev
Redhat/Centos:
sudo yum install libevent-devel
2, 下载 Memcached 编译安装。
官方网址:http://memcached.org/downloads
wget http://memcached.org/latest (将 latest改为最新稳定版,实例如下)
wget http://memcached.org/files/memcached-1.4.31.tar.gz
tar -zxvf memcached-1.x.x.tar.gz
cd memcached-1.x.x
./configure && make && make test && sudo make install
3,命令行下启动
memcached -d -m 128 -u root -l 127.0.0.1 -p 11211 -c 1024 -P
说明一下memcached服务的启动参数:
-p 监听的端口
-l 连接的IP地址, 默认是本机
-d start 启动memcached服务
-d restart 重起memcached服务
-d stop|shutdown 关闭正在运行的memcached服务
-d install 安装memcached服务
-d uninstall 卸载memcached服务
-u 以的身份运行 (仅在以root运行的时候有效)
-m 最大内存使用,单位MB。默认64MB
-M 内存耗尽时返回错误,而不是删除项
-c 最大同时连接数,默认是1024
-f 块大小增长因子,默认是1.25-n 最小分配空间,key+value+flags默认是48
-h 显示帮助
启动/结束
memcached -d -m 10 -u root -l 192.168.0.122 -p 11200 -c 256 -P /tmp/memcached.pid
-d 选项是启动一个守护进程,
-m 是分配给Memcache使用的内存数量,单位是MB,这里是10MB
-u 是运行Memcache的用户,这里是root
-l 是监听的服务器IP地址,如果有多个地址的话,这里指定了服务器的IP地址192.168.0.122
-p 是设置Memcache监听的端口,这里设置了12000,最好是1024以上的端口
-c 选项是最大运行的并发连接数,默认是1024,这里设置了256,按照你服务器的负载量来设定
-P 是设置保存Memcache的pid文件
kill `cat /tmp/memcached.pid`
获取运行状态
echo stats | nc 127.0.0.1 11211
watch "echo stats | nc 127.0.0.1 11211" (实时状态)
4,命令行下连接/断开
连接:
$ telnet 127.0.0.1 11211
Trying 127.0.0.1...
Connected to 127.0.0.1.
Escape character is '^]'.
按 “ctrl + ]”进入 telnet>
断开:
telnet> quit
Connection closed.
5, php测试
$mem = new Memcache;
$mem->connect("127.0.0.1", 11211);
$mem->set('key', 'This is a memcached test!', 0, 60);
$val = $mem->get('key');
echo $val;
xin@xin-VirtualBox:/var/www/memcached-1.4.31$ make test
./sizes
Slab Stats 64
Thread stats -8528
Global stats 160
Settings 208
Item (no cas) 48
Item (cas) 56
Libevent thread 184
Connection 520
----------------------------------------
libevent thread cumulative 4456
Thread stats cumulative 4272
./testapp
1..52
ok 1 - cache_create
ok 2 - cache_constructor
ok 3 - cache_constructor_fail
ok 4 - cache_destructor
ok 5 - cache_reuse
ok 6 - cache_redzone
ok 7 - issue_161
ok 8 - strtol
ok 9 - strtoll
ok 10 - strtoul
ok 11 - strtoull
ok 12 - issue_44
ok 13 - vperror
ok 14 - issue_101
Signal handled: Terminated.
ok 15 - start_server
ok 16 - issue_92
ok 17 - issue_102
ok 18 - binary_noop
ok 19 - binary_quit
ok 20 - binary_quitq
ok 21 - binary_set
ok 22 - binary_setq
ok 23 - binary_add
ok 24 - binary_addq
ok 25 - binary_replace
ok 26 - binary_replaceq
ok 27 - binary_delete
ok 28 - binary_deleteq
ok 29 - binary_get
ok 30 - binary_getq
ok 31 - binary_getk
ok 32 - binary_getkq
ok 33 - binary_gat
ok 34 - binary_gatq
ok 35 - binary_gatk
ok 36 - binary_gatkq
ok 37 - binary_incr
ok 38 - binary_incrq
ok 39 - binary_decr
ok 40 - binary_decrq
ok 41 - binary_version
ok 42 - binary_flush
ok 43 - binary_flushq
ok 44 - binary_append
ok 45 - binary_appendq
ok 46 - binary_prepend
ok 47 - binary_prependq
ok 48 - binary_stat
ok 49 - binary_illegal
ok 50 - binary_pipeline_hickup
Signal handled: Interrupt.
ok 51 - shutdown
ok 52 - stop_server
prove ./t
t/00-startup.t .............. 1/20 getaddrinfo(): Name or service not known
failed to listen on TCP port 40389: Success
t/00-startup.t .............. 13/20 slab class 1: chunk size 96 perslab 10922
slab class 2: chunk size 120 perslab 8738
slab class 3: chunk size 152 perslab 6898
slab class 4: chunk size 192 perslab 5461
slab class 5: chunk size 240 perslab 4369
slab class 6: chunk size 304 perslab 3449
slab class 7: chunk size 384 perslab 2730
slab class 8: chunk size 480 perslab 2184
slab class 9: chunk size 600 perslab 1747
slab class 10: chunk size 752 perslab 1394
slab class 11: chunk size 944 perslab 1110
slab class 12: chunk size 1184 perslab 885
slab class 13: chunk size 1480 perslab 708
slab class 14: chunk size 1856 perslab 564
slab class 15: chunk size 2320 perslab 451
slab class 16: chunk size 2904 perslab 361
slab class 17: chunk size 3632 perslab 288
slab class 18: chunk size 4544 perslab 230
slab class 19: chunk size 5680 perslab 184
slab class 20: chunk size 7104 perslab 147
slab class 21: chunk size 8880 perslab 118
slab class 22: chunk size 11104 perslab 94
slab class 23: chunk size 13880 perslab 75
slab class 24: chunk size 17352 perslab 60
slab class 25: chunk size 21696 perslab 48
slab class 26: chunk size 27120 perslab 38
slab class 27: chunk size 33904 perslab 30
slab class 28: chunk size 42384 perslab 24
slab class 29: chunk size 52984 perslab 19
slab class 30: chunk size 66232 perslab 15
slab class 31: chunk size 82792 perslab 12
slab class 32: chunk size 103496 perslab 10
slab class 33: chunk size 129376 perslab 8
slab class 34: chunk size 161720 perslab 6
slab class 35: chunk size 202152 perslab 5
slab class 36: chunk size 252696 perslab 4
slab class 37: chunk size 315872 perslab 3
slab class 38: chunk size 394840 perslab 2
slab class 39: chunk size 493552 perslab 2
slab class 40: chunk size 616944 perslab 1
slab class 41: chunk size 771184 perslab 1
slab class 42: chunk size 1048576 perslab 1
<26 server listening (auto-negotiate)
<27 server listening (auto-negotiate)
<28 send buffer was 212992, now 268435456
<32 send buffer was 212992, now 268435456
<31 server listening (udp)
<35 server listening (udp)
<30 server listening (udp)
<34 server listening (udp)
<29 server listening (udp)
<33 server listening (udp)
<28 server listening (udp)
<32 server listening (udp)
<36 new auto-negotiating client connection
<36 connection closed.
slab class 1: chunk size 96 perslab 10922
slab class 2: chunk size 120 perslab 8738
slab class 3: chunk size 152 perslab 6898
slab class 4: chunk size 192 perslab 5461
slab class 5: chunk size 240 perslab 4369
slab class 6: chunk size 304 perslab 3449
slab class 7: chunk size 384 perslab 2730
slab class 8: chunk size 480 perslab 2184
slab class 9: chunk size 600 perslab 1747
slab class 10: chunk size 752 perslab 1394
slab class 11: chunk size 944 perslab 1110
slab class 12: chunk size 1184 perslab 885
slab class 13: chunk size 1480 perslab 708
slab class 14: chunk size 1856 perslab 564
slab class 15: chunk size 2320 perslab 451
slab class 16: chunk size 2904 perslab 361
slab class 17: chunk size 3632 perslab 288
slab class 18: chunk size 4544 perslab 230
slab class 19: chunk size 5680 perslab 184
slab class 20: chunk size 7104 perslab 147
slab class 21: chunk size 8880 perslab 118
slab class 22: chunk size 11104 perslab 94
slab class 23: chunk size 13880 perslab 75
slab class 24: chunk size 17352 perslab 60
slab class 25: chunk size 21696 perslab 48
slab class 26: chunk size 27120 perslab 38
slab class 27: chunk size 33904 perslab 30
slab class 28: chunk size 42384 perslab 24
slab class 29: chunk size 52984 perslab 19
slab class 30: chunk size 66232 perslab 15
slab class 31: chunk size 82792 perslab 12
slab class 32: chunk size 103496 perslab 10
slab class 33: chunk size 129376 perslab 8
slab class 34: chunk size 161720 perslab 6
slab class 35: chunk size 202152 perslab 5
slab class 36: chunk size 252696 perslab 4
slab class 37: chunk size 315872 perslab 3
slab class 38: chunk size 394840 perslab 2
slab class 39: chunk size 493552 perslab 2
slab class 40: chunk size 616944 perslab 1
slab class 41: chunk size 771184 perslab 1
slab class 42: chunk size 1048576 perslab 1
<26 server listening (ascii)
<27 server listening (ascii)
<28 send buffer was 212992, now 268435456
<32 send buffer was 212992, now 268435456
<31 server listening (udp)
<35 server listening (udp)
<30 server listening (udp)
<34 server listening (udp)
<29 server listening (udp)
<33 server listening (udp)
<28 server listening (udp)
<32 server listening (udp)
slab class 1: chunk size 96 perslab 10922
slab class 2: chunk size 120 perslab 8738
slab class 3: chunk size 152 perslab 6898
slab class 4: chunk size 192 perslab 5461
slab class 5: chunk size 240 perslab 4369
slab class 6: chunk size 304 perslab 3449
slab class 7: chunk size 384 perslab 2730
slab class 8: chunk size 480 perslab 2184
slab class 9: chunk size 600 perslab 1747
slab class 10: chunk size 752 perslab 1394
slab class 11: chunk size 944 perslab 1110
slab class 12: chunk size 1184 perslab 885
slab class 13: chunk size 1480 perslab 708
slab class 14: chunk size 1856 perslab 564
slab class 15: chunk size 2320 perslab 451
slab class 16: chunk size 2904 perslab 361
slab class 17: chunk size 3632 perslab 288
slab class 18: chunk size 4544 perslab 230
slab class 19: chunk size 5680 perslab 184
slab class 20: chunk size 7104 perslab 147
slab class 21: chunk size 8880 perslab 118
slab class 22: chunk size 11104 perslab 94
slab class 23: chunk size 13880 perslab 75
slab class 24: chunk size 17352 perslab 60
slab class 25: chunk size 21696 perslab 48
slab class 26: chunk size 27120 perslab 38
slab class 27: chunk size 33904 perslab 30
slab class 28: chunk size 42384 perslab 24
slab class 29: chunk size 52984 perslab 19
slab class 30: chunk size 66232 perslab 15
slab class 31: chunk size 82792 perslab 12
slab class 32: chunk size 103496 perslab 10
slab class 33: chunk size 129376 perslab 8
slab class 34: chunk size 161720 perslab 6
slab class 35: chunk size 202152 perslab 5
slab class 36: chunk size 252696 perslab 4
slab class 37: chunk size 315872 perslab 3
slab class 38: chunk size 394840 perslab 2
slab class 39: chunk size 493552 perslab 2
slab class 40: chunk size 616944 perslab 1
slab class 41: chunk size 771184 perslab 1
slab class 42: chunk size 1048576 perslab 1
<26 server listening (binary)
<27 server listening (binary)
<28 send buffer was 212992, now 268435456
<32 send buffer was 212992, now 268435456
<31 server listening (udp)
<35 server listening (udp)
<30 server listening (udp)
<34 server listening (udp)
<29 server listening (udp)
<33 server listening (udp)
<28 server listening (udp)
<32 server listening (udp)
<36 new binary client connection.
<36 connection closed.
Invalid value for binding protocol: http
-- should be one of auto, binary, or ascii
t/00-startup.t .............. 17/20 Maximum connections must be greater than 0
t/00-startup.t .............. 18/20 Maximum connections must be greater than 0
t/00-startup.t .............. 19/20 Number of threads must be greater than 0
t/00-startup.t .............. ok
t/64bit.t ................... ok
t/binary-get.t .............. ok
t/binary-sasl.t ............. This server is not built with SASL support.
t/binary-sasl.t ............. ok
t/binary.t .................. ok
t/bogus-commands.t .......... ok
t/cas.t ..................... ok
t/chunked-items.t ........... ok
t/daemonize.t ............... ok
t/dash-M.t .................. ok
t/dyn-maxbytes.t ............ ok
t/evictions.t ............... ok
t/expirations.t ............. ok
t/flags.t ................... ok
t/flush-all.t ............... ok
t/getset.t .................. ok
t/idle-timeout.t ............ ok
t/incrdecr.t ................ ok
t/issue_104.t ............... ok
t/issue_108.t ............... ok
t/issue_14.t ................ ok
t/issue_140.t ............... skipped: Fix for Issue 140 was only an illusion
t/issue_152.t ............... ok
t/issue_163.t ............... ok
t/issue_183.t ............... ok
t/issue_192.t ............... ok
t/issue_22.t ................ ok
t/issue_260.t ............... skipped: Only possible to test #260 under artificial conditions
t/issue_29.t ................ ok
t/issue_3.t ................. ok
t/issue_41.t ................ ok
t/issue_42.t ................ ok
t/issue_50.t ................ ok
t/issue_61.t ................ ok
t/issue_67.t ................ ok
t/issue_68.t ................ ok
t/issue_70.t ................ ok
t/item_size_max.t ........... 1/7 Item max size cannot be less than 1024 bytes.
t/item_size_max.t ........... 2/7 Cannot set item size limit higher than 1/4 of memory max.
t/item_size_max.t ........... ok
t/line-lengths.t ............ ok
t/lru-crawler.t ............. ok
t/lru-maintainer.t .......... ok
t/lru.t ..................... ok
t/maxconns.t ................ ok
t/multiversioning.t ......... ok
t/noreply.t ................. ok
t/refhang.t ................. 124/127
# Failed test 'some ooms happened'
# at t/refhang.t line 51.
# Failed test 'nonzero lrutail_reflocked'
# at t/refhang.t line 53.
# got: '0'
# expected: anything else
# Failed test 'nonzero total lrutail_reflocked'
# at t/refhang.t line 56.
# got: '0'
# expected: anything else
# Looks like you failed 3 tests of 127.
t/refhang.t ................. Dubious, test returned 3 (wstat 768, 0x300)
Failed 3/127 subtests
t/slabs-reassign-chunked.t .. ok
t/slabs-reassign2.t ......... ok
t/slabs_reassign.t .......... ok
t/stats-conns.t ............. ok
t/stats-detail.t ............ ok
t/stats.t ................... ok
t/touch.t ................... ok
t/udp.t ..................... ok
t/unixsocket.t .............. ok
t/watcher.t ................. ok
t/whitespace.t .............. fatal: Not a git repository (or any of the parent directories): .git
t/whitespace.t .............. skipped: Skipping tests probably because you don't have git.
Test Summary Report
-------------------
t/refhang.t (Wstat: 768 Tests: 127 Failed: 3)
Failed tests: 124, 126-127
Non-zero exit status: 3
Files=57, Tests=25794, 212 wallclock secs ( 6.26 usr 0.42 sys + 12.31 cusr 3.91 csys = 22.90 CPU)
Result: FAIL
Makefile:1588: recipe for target 'test' failed
make: *** [test] Error 1