memcache学习笔记(一)

memcache学习笔记(一)

一、安装

注:memcache用到libevent这个库用于Socket处理,所以还需要安装libevent,官网:http://www.monkey.org/~provos/libevent/,目前最新的版本是2.0.22
安装中遇到的问题:本来打算在mac中安装,然而安装libevent时的最后一步sudo make install 总是过不去,

~libevent-1.4.13-stable sudo make install
/Library/Developer/CommandLineTools/usr/bin/make  install-recursive
Making install in .
test -z "/usr/bin" || ./install-sh -c -d "/usr/bin"
 /usr/bin/install -c 'event_rpcgen.py' '/usr/bin/event_rpcgen.py'
install: /usr/bin/event_rpcgen.py: Operation not permitted
make[3]: *** [install-binSCRIPTS] Error 71
make[2]: *** [install-am] Error 2
make[1]: *** [install-recursive] Error 1
make: *** [install] Error 2

OS X EI Capitan的rootless机制搞得好尴尬,经常遇到这种Operation not permitted都不知道该如何是好,只好安装虚拟机,在虚拟机里安装Linux系统安装memcached了。

1. 先安装libevent

derek@root:~/Downloads$ tar zxvf libevent-2.0.22-stable.tar.gz
derek@root:~/Downloads$ cd libevent-2.0.22-stable/
derek@root:~/Downloads/libevent-2.0.22-stable$ ./configure -prefix=/usr
derek@root:~/Downloads/libevent-2.0.22-stable$ make
derek@root:~/Downloads/libevent-2.0.22-stable$ sudo make install

详细信息如下:

derek@root:~/Downloads$ ll
total 1716
drwxr-xr-x  2 derek derek   4096  911 21:59 ./
drwxr-xr-x 16 derek derek   4096  911 21:52 ../
-rw-r--r--  1 derek derek 499603  911 21:56 libevent-1.4.13-stable.tar.gz
-rw-r--r--  1 derek derek 854987  911 21:59 libevent-2.0.22-stable.tar.gz
-rw-r--r--  1 derek derek 389502  911 21:58 memcached-1.4.31.tar.gz

derek@root:~/Downloads$ tar zxvf libevent-2.0.22-stable.tar.gz
libevent-2.0.22-stable/
libevent-2.0.22-stable/evmap-internal.h
libevent-2.0.22-stable/event_iocp.c
libevent-2.0.22-stable/changelist-internal.h
......
......
libevent-2.0.22-stable/evhttp.h
libevent-2.0.22-stable/event_rpcgen.py
libevent-2.0.22-stable/Makefile.am
libevent-2.0.22-stable/minheap-internal.h

derek@root:~/Downloads$ ls
libevent-1.4.13-stable.tar.gz  libevent-2.0.22-stable  libevent-2.0.22-stable.tar.gz  memcached-1.4.31.tar.gz

derek@root:~/Downloads$ cd libevent-2.0.22-stable/
derek@root:~/Downloads/libevent-2.0.22-stable$ ./configure -prefix=/usr
checking for a BSD-compatible install... /usr/bin/install -c
checking whether build environment is sane... yes
checking for a thread-safe mkdir -p... /bin/mkdir -p
......
......
config.status: creating include/Makefile
config.status: creating test/Makefile
config.status: creating sample/Makefile
config.status: creating config.h
config.status: executing depfiles commands
config.status: executing libtool commands

derek@root:~/Downloads/libevent-2.0.22-stable$ make
test -d include/event2 || /bin/mkdir -p include/event2
/bin/sed -f ./make-event-config.sed < config.h > include/event2/event-config.hT
mv -f include/event2/event-config.hT include/event2/event-config.h
make  all-recursive
make[1]: Entering directory '/home/derek/Downloads/libevent-2.0.22-stable'
Making all in .
......
......
make[3]: Leaving directory '/home/derek/Downloads/libevent-2.0.22-stable/test'
make[2]: Leaving directory '/home/derek/Downloads/libevent-2.0.22-stable/test'
make[1]: Leaving directory '/home/derek/Downloads/libevent-2.0.22-stable'

derek@root:~/Downloads/libevent-2.0.22-stable$ sudo make install
[sudo] password for derek:
make  install-recursive
make[1]: Entering directory '/home/derek/Downloads/libevent-2.0.22-stable'
Making install in .
make[2]: Entering directory '/home/derek/Downloads/libevent-2.0.22-stable'
make[3]: Entering directory '/home/derek/Downloads/libevent-2.0.22-stable'
......
......
make[4]: Leaving directory '/home/derek/Downloads/libevent-2.0.22-stable/test'
make[3]: Leaving directory '/home/derek/Downloads/libevent-2.0.22-stable/test'
make[2]: Leaving directory '/home/derek/Downloads/libevent-2.0.22-stable/test'
make[1]: Leaving directory '/home/derek/Downloads/libevent-2.0.22-stable'

2. 测试libevent是否安装成功

derek@root:~/Downloads/libevent-2.0.22-stable$ ls -al /usr/lib | grep libevent
lrwxrwxrwx   1 root root       21  911 22:01 libevent-2.0.so.5 -> libevent-2.0.so.5.1.9
-rwxr-xr-x   1 root root  1216984  911 22:01 libevent-2.0.so.5.1.9
-rw-r--r--   1 root root  2156264  911 22:01 libevent.a
lrwxrwxrwx   1 root root       26  911 22:01 libevent_core-2.0.so.5 -> libevent_core-2.0.so.5.1.9
-rwxr-xr-x   1 root root   744784  911 22:01 libevent_core-2.0.so.5.1.9
-rw-r--r--   1 root root  1406608  911 22:01 libevent_core.a
-rwxr-xr-x   1 root root      974  911 22:01 libevent_core.la
lrwxrwxrwx   1 root root       26  911 22:01 libevent_core.so -> libevent_core-2.0.so.5.1.9
lrwxrwxrwx   1 root root       27  911 22:01 libevent_extra-2.0.so.5 -> libevent_extra-2.0.so.5.1.9
-rwxr-xr-x   1 root root   494440  911 22:01 libevent_extra-2.0.so.5.1.9
-rw-r--r--   1 root root   749728  911 22:01 libevent_extra.a
-rwxr-xr-x   1 root root      981  911 22:01 libevent_extra.la
lrwxrwxrwx   1 root root       27  911 22:01 libevent_extra.so -> libevent_extra-2.0.so.5.1.9
-rwxr-xr-x   1 root root      939  911 22:01 libevent.la
lrwxrwxrwx   1 root root       30  911 22:01 libevent_pthreads-2.0.so.5 -> libevent_pthreads-2.0.so.5.1.9
-rwxr-xr-x   1 root root    25608  911 22:01 libevent_pthreads-2.0.so.5.1.9
-rw-r--r--   1 root root    25222  911 22:01 libevent_pthreads.a
-rwxr-xr-x   1 root root     1002  911 22:01 libevent_pthreads.la
lrwxrwxrwx   1 root root       30  911 22:01 libevent_pthreads.so -> libevent_pthreads-2.0.so.5.1.9
lrwxrwxrwx   1 root root       21  911 22:01 libevent.so -> libevent-2.0.so.5.1.9
derek@root:~/Downloads/libevent-2.0.22-stable$

安装OK!

3. 安装memcached

安装中需要制定libevent的安装位置

derek@root:~/Downloads$ tar zxvf memcached-1.4.31.tar.gz
derek@root:~/Downloads$ cd memcached-1.4.31/
derek@root:~/Downloads/memcached-1.4.31$ ./configure -with-libevent=/usr
derek@root:~/Downloads/memcached-1.4.31$ make
derek@root:~/Downloads/memcached-1.4.31$ sudo make install

详细信息如下:

derek@root:~/Downloads$ ll
total 1720
drwxr-xr-x  3 derek derek   4096  911 22:00 ./
drwxr-xr-x 16 derek derek   4096  911 21:52 ../
-rw-r--r--  1 derek derek 499603  911 21:56 libevent-1.4.13-stable.tar.gz
drwxr-xr-x 10 derek derek   4096  911 22:00 libevent-2.0.22-stable/
-rw-r--r--  1 derek derek 854987  911 21:59 libevent-2.0.22-stable.tar.gz
-rw-r--r--  1 derek derek 389502  911 21:58 memcached-1.4.31.tar.gz
derek@root:~/Downloads$ tar zxvf memcached-1.4.31.tar.gz
memcached-1.4.31/
memcached-1.4.31/config.guess
memcached-1.4.31/assoc.h
......
......
memcached-1.4.31/thread.c
memcached-1.4.31/depcomp
memcached-1.4.31/configure
memcached-1.4.31/Makefile.in
derek@root:~/Downloads$ cd memcached-1.4.31/
derek@root:~/Downloads/memcached-1.4.31$
derek@root:~/Downloads/memcached-1.4.31$
derek@root:~/Downloads/memcached-1.4.31$ ./configure -with-libevent=/usr
checking build system type... x86_64-pc-linux-gnu
checking host system type... x86_64-pc-linux-gnu
checking for a BSD-compatible install... /usr/bin/install -c
checking whether build environment is sane... yes
checking for a thread-safe mkdir -p... /bin/mkdir -p
......
.....
config.status: creating doc/Makefile
config.status: creating config.h
config.status: executing depfiles commands
derek@root:~/Downloads/memcached-1.4.31$ make
make  all-recursive
make[1]: Entering directory '/home/derek/Downloads/memcached-1.4.31'
Making all in doc
make[2]: Entering directory '/home/derek/Downloads/memcached-1.4.31/doc'
make  all-am
......
......
make[2]: Leaving directory '/home/derek/Downloads/memcached-1.4.31'
make[1]: Leaving directory '/home/derek/Downloads/memcached-1.4.31'
derek@root:~/Downloads/memcached-1.4.31$ sudo make install
[sudo] password for derek:
make  install-recursive
make[1]: Entering directory '/home/derek/Downloads/memcached-1.4.31'
Making install in doc
......
......
make[3]: Leaving directory '/home/derek/Downloads/memcached-1.4.31'
make[2]: Leaving directory '/home/derek/Downloads/memcached-1.4.31'
make[1]: Leaving directory '/home/derek/Downloads/memcached-1.4.31'
derek@root:~/Downloads/memcached-1.4.31$

4. 测试是否成功安装memcached

derek@root:~/Downloads/memcached-1.4.31$ ls -al /usr/local/bin | grep memcached
-rwxr-xr-x  1 root root 617016  911 22:17 memcached
derek@root:~/Downloads/memcached-1.4.31$

安装OK!

二、启动Memcache服务

1. 启动Memcache

derek@root:~$ cd /usr/local/bin
derek@root:/usr/local/bin$ ll
total 612
drwxr-xr-x  2 root root   4096  911 22:17 ./
drwxr-xr-x 10 root root   4096 1021  2015 ../
-rwxr-xr-x  1 root root 617016  911 22:17 memcached*
derek@root:/usr/local/bin$ memcached -d -m 50 -u derek -l 192.168.0.102 -p 12121 -c 256 -P /tmp/memcached.pid
derek@root:/usr/local/bin$

进入/usr/local/bin目录下,然后启动memcached,需要带些参数:

  • -d 启动一个守护进程
  • -m 是分配给Memcache使用的内存大小,单位MB,此处分配50MB
  • -u 运行Memcache的用户,此处为derek
  • -l 设置 监听的服务器ip地址
  • -p 设置 监听的端口,此处为:12121, 最好是1024以上的端口
  • -c 选项是最大运行的并发连接数,默认是1024
  • -P 设置保存Memcache的pid文件

/tmp/memcached.pid中的内容为(进程号):

derek@root:/tmp$ cat memcached.pid
10180
derek@root:/tmp$

2. 结束Memcache

结束Memcache进程,执行:

kill `cat /tmp/memcached.pid`

也可以启动多个守护进程,不过端口不能重复(没有试)

3. 测试Memcache

➜  ~ telnet 192.168.0.102 12121
Trying 192.168.0.102...
Connected to 192.168.0.102.
Escape character is '^]'.
set key1 0 60 4
derek
CLIENT_ERROR bad data chunk
ERROR

ERROR
set key1 0 60 9
hello
STORED
CLIENT_ERROR bad data chunk
ERROR
get key1
END

ERROR
set key1 0 60 9
seaprince
STORED
get key1
VALUE key1 0 9
seaprince
END
stats
STAT pid 10180
STAT uptime 764
STAT time 1473605258
STAT version 1.4.31
STAT libevent 2.0.22-stable
STAT pointer_size 64
STAT rusage_user 0.272000
STAT rusage_system 0.176000
STAT curr_connections 5
STAT total_connections 7
STAT connection_structures 6
STAT reserved_fds 20
STAT cmd_get 2
STAT cmd_set 4
STAT cmd_flush 0
STAT cmd_touch 0
STAT get_hits 1
STAT get_misses 1
STAT get_expired 0
STAT get_flushed 0
STAT delete_misses 0
STAT delete_hits 0
STAT incr_misses 0
STAT incr_hits 0
STAT decr_misses 0
STAT decr_hits 0
STAT cas_misses 0
STAT cas_hits 0
STAT cas_badval 0
STAT touch_hits 0
STAT touch_misses 0
STAT auth_cmds 0
STAT auth_errors 0
STAT bytes_read 185
STAT bytes_written 209
STAT limit_maxbytes 52428800
STAT accepting_conns 1
STAT listen_disabled_num 0
STAT time_in_listen_disabled_us 0
STAT threads 4
STAT conn_yields 0
STAT hash_power_level 16
STAT hash_bytes 524288
STAT hash_is_expanding 0
STAT malloc_fails 0
STAT log_worker_dropped 0
STAT log_worker_written 0
STAT log_watcher_skipped 0
STAT log_watcher_sent 0
STAT bytes 78
STAT curr_items 1
STAT total_items 1
STAT expired_unfetched 0
STAT evicted_unfetched 0
STAT evictions 0
STAT reclaimed 0
STAT crawler_reclaimed 0
STAT crawler_items_checked 0
STAT lrutail_reflocked 0
END
quit
Connection closed by foreign host.
➜  ~

注意:set key1 0 60 9

  • key1 表示key的名字
  • 0 表示一个跟该key有关的自定义数据
  • 60 表示该key的有效时间
  • 9 表示key1值得字节数

最后,Memcache安装启动成功!

未完待续……

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值