一、引言
写有关NoSQL数据库有关的文章已经有一段时间了,可以高兴的说,Redis暂时就算写完了,从安装到数据类型,在到集群,几乎都写到了。如果以后有了心得,再补充吧。然后就是MongoDB了,有关MongoDB的文章也写了一些了,安装、配置和基本使用都写了,然后就是一些高级的课题了。最近由于工作比较忙的原因,写东西就慢了,MongoDB的文章也暂缓了。今天正好有时间,我就把在Linux环境下安装和配置Memcached的过程写下来。其实这个文章不是今天写的了,写了有几天了,只是利用今天把文章发出来。
二、Memcached简介
Memcached是一个*开源的,高性能,分布式内存对象缓存系统。是以LiveJournal旗下Danga Interactive公司的Brad Fitzpatric为首开发的一款软件。现在已成为mixi、hatena、Facebook、Vox、LiveJournal等众多服务中提高Web应用扩展性的重要因素。
Memcached是一种基于内存的key-value存储,用来存储小块的任意数据(字符串、对象)。这些数据可以是数据库调用、API调用或者是页面渲染的结果。Memcached简洁而强大。它的简洁设计便于快速开发,减轻开发难度,解决了大数据量缓存的很多问题。它的API兼容大部分流行的开发语言。 本质上,它是一个简洁的key-value存储系统。和Redis是有些类似的,但是也有很大的不同了。
三、Memcached在Linux上的安装
今天我们开始讲如何在Linux系统上安装和配置 Memcached 服务,过程很简单,马上开始我们的安装吧。
1、准备安装环境
yum install gcc wget make cmake libtool autoconf
2、下载相应的库和Memcached源码
libevent,因为Memcached依赖这个Libevent库,所以必须先下载安装
官网地址:http://libevent.org/
3、先安装 Libevent 库文件
首先检查系统中是否安装了libevent
[root@linux memcached]# pwd
/root/software/download/memcached [root@linux memcached]# rpm -qa |grep libevent
[root@linux memcached]# //无值说明libevent没有安装
如果安装了则查看libevent的安装路径,后续安装时需要用到
[root@linux bin]# rpm -ql libevent
未安装软件包 libevent
[root@linux bin]#
如果没有安装,则先安装libevent
3.1、下载libevent
[root@linux download]# ls
libevent memcached mongodb redis [root@linux download]# cd libevent/
[root@linux libevent]# wget https://github.com/libevent/libevent/releases/download/release-2.1.8-stable/libevent-2.1.8-stable.tar.gz [root@linux libevent]# ls
libevent-2.1.-stable.tar.gz
[root@linux libevent]#
3.2、tar 解压Libevent库文件
[root@linux libevent]# pwd
/root/software/download/libevent [root@linux libevent]# tar zxvf libevent-2.1.-stable.tar.gz
[root@linux libevent]# ls
libevent-2.1.-stable libevent-2.1.-stable.tar.gz
3.3、cd 进入根目录
[root@linux libevent]# cd libevent-2.1.-stable
[root@linux libevent-2.1.-stable]# pwd
/root/software/download/libevent/libevent-2.1.-stable [root@linux libevent-2.1.-stable]# ./configure -h/-help //在根目录下面查看一下配置文件,方便以后使用
3.4、设置安装路径,安装目录可以自定义。我的安装目录是:/root/application/program/memcached/libevent
[root@linux libevent-2.1.-stable]# ./configure --prefix=/root/application/program/memcached/libevent
checking for a BSD-compatible install... /usr/bin/install -c
checking whether build environment is sane... yes
checking for a thread-safe mkdir -p... /usr/bin/mkdir -p
checking for gawk... gawk
checking whether make sets $(MAKE)... yes
checking whether make supports nested variables... yes
checking whether make supports nested variables... (cached) yes
checking for style of include used by make... GNU
checking for gcc... gcc
......
......
checking size of pthread_t...
checking for library containing ERR_remove_thread_state... -lcrypto
checking that generated files are newer than configure... done
configure: creating ./config.status
config.status: creating libevent.pc
config.status: creating libevent_openssl.pc
config.status: creating libevent_pthreads.pc
config.status: creating libevent_core.pc
config.status: creating libevent_extra.pc
config.status: creating Makefile
config.status: creating config.h
config.status: creating evconfig-private.h
config.status: evconfig-private.h is unchanged
config.status: executing depfiles commands
config.status: executing libtool commands
[root@linux libevent-2.1.-stable]#
3.5、编译和安装 make && make install
[root@linux libevent-2.1.-stable]# make && make install
4、安装Memcached源文件
//这不可以不执行,默认有相应的权限。如果没有,修改安装包的执行操作权限,chmod 777 memcached-1.4.15.tar.gz
4.1、下载 memcached-1.4.15 源程序
[root@linux memcached]# pwd
/root/software/download/memcached
[root@linux memcached]# wget memcached 的地址 [root@linux memcached]# ls
memcached-1.4..tar.gz
[root@linux memcached]#
4.2、解压Memcached 压缩文件
[root@linux memcached]# pwd
/root/software/download/memcached [root@linux memcached]# tar zxvf memcached.tar.gz
memcached-1.4./
memcached-1.4./Makefile.am
memcached-1.4./trace.h
memcached-1.4./config.guess
memcached-1.4./cache.c
memcached-1.4./util.c
memcached-1.4./assoc.c
....
....
memcached-1.4./doc/xml2rfc/rfc2629-other.ent
memcached-1.4./doc/xml2rfc/reference.RFC..xml
memcached-1.4./doc/xml2rfc/rfc2629-xhtml.ent
memcached-1.4./doc/xml2rfc/rfc2629-noinc.xsl
memcached-1.4./doc/xml2rfc/rfc2629-refchk.xsl
memcached-1.4./doc/xml2rfc/rfc2629.dtd
memcached-1.4./doc/protocol.txt
memcached-1.4./doc/Makefile
memcached-1.4./compile
[root@linux memcached]#
4.3、进入到 memcached 根目录
[root@linux memcached]# ls
memcached-1.4. memcached-1.4..tar.gz
[root@linux memcached]# cd memcached-1.4.
[root@linux memcached-1.4.]# pwd
/root/software/download/memcached/memcached-1.4.
4.4、配置安装目录
[root@linux memcached-1.4.]# pwd
/root/software/download/memcached/memcached-1.4. [root@linux memcached]# ./configure -h/-help //可以查看memcached 配置文件,方便使用 [root@linux memcached-1.4.]# ./configure --prefix=/root/application/program/memcached/memcachedfile --with-libevent=/root/application/program/memcached/libevent
checking build system type... x86_64-unknown-linux-gnu
checking host system type... x86_64-unknown-linux-gnu
checking target system type... x86_64-unknown-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... /usr/bin/mkdir -p
....
....
checking for xml2rfc... no
checking for xsltproc... /usr/bin/xsltproc
configure: creating ./config.status
config.status: creating Makefile
config.status: creating doc/Makefile
config.status: creating config.h
config.status: executing depfiles commands
[root@linux memcached-1.4.]#
4.5、编译和安装 make && make install
[root@linux memcached-1.4.]# make && make install
5、启动Memcached服务环境
//启动程序必须在安装Memcached服务的目录的Bin目录下执行操作
[root@linux bin]# pwd
/root/application/program/memcached/memcachedfile/bin [root@linux bin]# ./memcached -m -p -u nobody -vvv 前端
slab class : chunk size perslab
slab class : chunk size perslab
slab class : chunk size perslab
slab class : chunk size perslab
slab class : chunk size perslab
slab class : chunk size perslab
....
....
< server listening (udp)
< server listening (udp)
< server listening (udp)
< server listening (udp)
< server listening (udp)
< server listening (udp)
< server listening (udp)
< server listening (udp)
//当前处于阻塞状态,说明Memcached启动成功,但是该窗口不能关闭,否则服务也会关闭 [root@linux bin]# ./memcached -m -p -u nobody -d 后台启动
[root@linux bin]# //查看memcache是否开启:
[root@linux ~]# netstat -ntpl|grep memcache
6、关闭Memcached服务环境
[root@linux ~]# kill PID(Memcached服务的进程号) [root@linux ~]# kill
7、配置
启动参数介绍如下:和上面的命令不对应
-d选项是启动一个守护进程,
-m是分配给Memcache使用的内存数量,单位是MB,这里是10MB,
-u是运行Memcache的用户,这里是root,
-l是监听的服务器IP地址,如果有多个地址的话,这里指定了服务器的IP地址192.168.0.200,
-p是设置Memcache监听的端口,这里设置了12000,最好是1024以上的端口,
-c选项是最大运行的并发连接数,默认是1024,这里设置了256,按照服务器的负载量来设定,
-P是设置保存Memcache的pid文件,我这里是保存在 /tmp/memcached.pid,也可以启动多个守护进程,不过端口不能重复。
8、我们可以查看memcached命令的帮助文件
[root@linux bin]# pwd
/root/application/program/memcached/memcachedfile/bin
[root@linux bin]# ./memcached -h/-help
memcached 1.4.
-p <num> TCP port number to listen on (default: )
-U <num> UDP port number to listen on (default: , is off)
-s <file> UNIX socket path to listen on (disables network support)
-a <mask> access mask for UNIX socket, in octal (default: )
-l <addr> interface to listen on (default: INADDR_ANY, all addresses)
<addr> may be specified as host:port. If you don't specify a port number, the value you specified with -p or -U is used. You may specify multiple addresses separated by comma or by using -l multiple times
-d run as a daemon
-r maximize core file limit
-u <username> assume identity of <username> (only when run as root)
-m <num> max memory to use for items in megabytes (default: MB)
-M return error on memory exhausted (rather than removing items)
-c <num> max simultaneous connections (default: )
-k lock down all paged memory. Note that there is a limit on how much memory you may lock. Trying to allocate more than that would fail, so be sure you set the limit correctly for the user you started the daemon with (not for -u <username> user;under sh this is done with 'ulimit -S -l NUM_KB').
-v verbose (print errors/warnings while in event loop)
-vv very verbose (also print client commands/reponses)
-vvv extremely verbose (also print internal state transitions)
-h print this help and exit
-i print memcached and libevent license
-P <file> save PID in <file>, only used with -d option
-f <factor> chunk size growth factor (default: 1.25)
-n <bytes> minimum space allocated for key+value+flags (default: )
-L Try to use large memory pages (if available). Increasing the memory page size could reduce the number of TLB misses and improve the performance. In order to get large pages from the OS, memcached will allocate the total item-cache in one large chunk.
-D <char> Use <char> as the delimiter between key prefixes and IDs.This is used for per-prefix stats reporting. The default is ":" (colon). If this option is specified, stats collection is turned on automatically; if not, then it may be turned on by sending the "stats detail on" command to the server.
-t <num> number of threads to use (default: )
-R Maximum number of requests per event, limits the number of requests process for a given connection to prevent starvation (default: )
-C Disable use of CAS
-b Set the backlog queue limit (default: )
-B Binding protocol - one of ascii, binary, or auto (default)
-I Override the size of each slab page. Adjusts max item size (default: 1mb, min: 1k, max: 128m)
-o Comma separated list of extended or experimental options
- (EXPERIMENTAL) maxconns_fast: immediately close new connections if over maxconns limit
- hashpower: An integer multiplier for how large the hash table should be. Can be grown at runtime if not big enough.Set this based on "STAT hash_power_level" before a restart.
[root@linux bin]#
9、我们使用telnet命令来连接Linux系统上的Memcached服务。
9.1、在Window环境下通过telnet连接Memcached服务
C:\Users\Administrator>telnet192.168.127. //当前处于黑屏状态,等待输入命令,直接输入stats,直接回车。
stats
STAT pid
STAT uptime
STAT time
STAT version 1.4.
STAT libevent 2.1.-stable
STAT pointer_size
STAT rusage_user 0.005197
STAT rusage_system 0.067566
STAT curr_connections
STAT total_connections
STAT connection_structures
STAT reserved_fds
STAT cmd_get
STAT cmd_set
STAT cmd_flush
STAT cmd_touch
STAT get_hits
STAT get_misses
STAT delete_misses
STAT delete_hits
STAT incr_misses
STAT incr_hits
STAT decr_misses
STAT decr_hits
STAT cas_misses
STAT cas_hits
STAT cas_badval
STAT touch_hits
STAT touch_misses
STAT auth_cmds
STAT auth_errors
STAT bytes_read
STAT bytes_written
STAT limit_maxbytes
STAT accepting_conns
STAT listen_disabled_num
STAT threads
STAT conn_yields
STAT hash_power_level
STAT hash_bytes
STAT hash_is_expanding
STAT bytes
STAT curr_items
STAT total_items
STAT expired_unfetched
STAT evicted_unfetched
STAT evictions
STAT reclaimed
END //能连接上,说明memcache成功启用,可使用stats命令查看当前状态
9.2、在Linux环境下使用telnet命令连接Memcached服务
[root@linux bin]# telnet 192.168.127.130
Trying 192.168.127.130...
Connected to 192.168.127.130.
Escape character is '^]'.
//当前窗口处于等待状态,可以直接输入命令执行
stats //直接运行该命令
STAT pid
STAT uptime
STAT time
STAT version 1.4.
STAT libevent 2.1.-stable
STAT pointer_size
STAT rusage_user 0.005197
STAT rusage_system 0.067566
STAT curr_connections
STAT total_connections
STAT connection_structures
STAT reserved_fds
STAT cmd_get
STAT cmd_set
STAT cmd_flush
STAT cmd_touch
STAT get_hits
STAT get_misses
STAT delete_misses
STAT delete_hits
STAT incr_misses
STAT incr_hits
STAT decr_misses
STAT decr_hits
STAT cas_misses
STAT cas_hits
STAT cas_badval
STAT touch_hits
STAT touch_misses
STAT auth_cmds
STAT auth_errors
STAT bytes_read
STAT bytes_written
STAT limit_maxbytes
STAT accepting_conns
STAT listen_disabled_num
STAT threads
STAT conn_yields
STAT hash_power_level
STAT hash_bytes
STAT hash_is_expanding
STAT bytes
STAT curr_items
STAT total_items
STAT expired_unfetched
STAT evicted_unfetched
STAT evictions
STAT reclaimed
END //能连接上,说明memcache成功启用,可使用stats命令查看当前状态
10、查看Memcached服务是否启动:ps aux|grep memcached 或者 ps -ef|grep memcached
[root@linux ~]# ps aux|grep memcached
nobody 0.0 0.0 ? Ssl : : ./memcached -m -p -u nobody -d
root 0.0 0.0 pts/ S+ : : grep --color=auto memcached [root@linux ~]# ps -ef|grep memcached
nobody : ? :: ./memcached -m -p -u nobody -d
root : pts/ :: grep --color=auto memcached
11、如果我们通过telnet命令无法连接Linux上的Linux系统上的Memcached服务,可能和端口有关系。
//在使用window的cmd连接Linux环境上的Memcached的时候,必须先把11211这个端口增加到防火墙,否则无法连接,执行命令如下:
[root@linux ~]# firewall-cmd --zone=public --add-port=/tcp --permanent
[root@linux ~]# firewall-cmd --reload //再次重新连接就没有问题了
四、总结
好了,写完了。有关Memcached的文章可能要晚一点再出来了,先要把MongoDB的文章写完,再来写有关Memcached的文章了,学习和使用也需要一个过程。还有一个原因,由于Redis的崛起和强大,已经完全可以替代Memcached了,而且很多的公司也不使用它了,所以由于原因种种,该系列就往后推了。