Memcached在Linux环境下的使用详解
一、引言 ?????????? Memcached是一种基于内存的key-value存储,用来存储小块的任意数据(字符串、对象)。这些数据可以是数据库调用、API调用或者是页面渲染的结果。Memcached简洁而强大。它的简洁设计便于快速开发,减轻开发难度,解决了大数据量缓存的很多问题。它的API兼容大部分流行的开发语言。 本质上,它是一个简洁的key-value存储系统。和Redis是有些类似的,但是也有很大的不同了。 [root@linux memcached]# pwd /root/software/download/memcached [root@linux memcached]# rpm -qa |grep libevent [root@linux memcached]# //无值说明libevent没有安装
[root@linux bin]# rpm -ql libevent
未安装软件包 libevent
[root@linux bin]#
[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.8-stable.tar.gz [root@linux libevent]#
[root@linux libevent]# pwd /root/software/download/libevent [root@linux libevent]# tar zxvf libevent-stable.tar.gz [root@linux libevent]# ls libevent-8-stable libevent-8-stable.tar.gz
[root@linux libevent]# cd libevent-stable [root@linux libevent-stable]# pwd /root/software/download/libevent/libevent-stable [root@linux libevent-8-stable]# ./configure -h/-help 在根目录下面查看一下配置文件,方便以后使用
[root@linux libevent-8-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 style of include used by make... GNU checking gcc... gcc ...... ...... checking size of pthread_t... 8 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 unchanged config.status: executing depfiles commands config.status: executing libtool commands [root@linux libevent-8-stable]#
[root@linux libevent-8-stable]# make && make install
[root@linux memcached]# pwd /root/software/download/memcached [root@linux memcached]# wget memcached 的地址 [root@linux memcached]# ls memcached-1.4.15.tar.gz [root@linux memcached]#
memcached [root@linux memcached]# tar zxvf memcached.tar.gz memcached-15/ memcached-Makefile.am memcached-trace.h memcached-config.guess memcached-cache.c memcached-util.c memcached-assoc.c .... .... memcached-15/doc/xml2rfc/rfc2629-other.ent memcached-15/doc/xml2rfc/reference.RFC.0768.xml memcached-xhtml.ent memcached-noinc.xsl memcached-refchk.xsl memcached-15/doc/xml2rfc/rfc2629.dtd memcached-15/doc/protocol.txt memcached-Makefile memcached-compile [root@linux memcached]#
[root@linux memcached]# ls memcached-15 memcached-.tar.gz [root@linux memcached]# cd memcached- [root@linux memcached-]# pwd /root/software/download/memcached/memcached-15
[root@linux memcached- [root@linux memcached]# ./configure -h/-help 可以查看memcached 配置文件,方便使用 15]# ./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 c checking whether build environment sane... yes checking p .... .... checking 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-15]#
[root@linux memcached-15]# make && make install
启动程序必须在安装Memcached服务的目录的Bin目录下执行操作 [root@linux bin]# pwd /root/application/program/memcached/memcachedfile/bin [root@linux bin]# ./memcached -m 64 -p 11211 -u nobody -vvv 前端 slab class 1: chunk size 96 perslab 10922 slab 2: chunk size 120 perslab 87383: chunk size 152 perslab 68984: chunk size 192 perslab 54615: chunk size 240 perslab 43696: chunk size 304 perslab 3449 .... .... <29 server listening (udp) <30 server listening (udp) 当前处于阻塞状态,说明Memcached启动成功,但是该窗口不能关闭,否则服务也会关闭 [root@linux bin]# ./memcached -m d 后台启动 [root@linux bin]# 查看memcache是否开启: [root@linux ~]# netstat -ntpl|grep memcache
[root@linux ~]# kill PID(Memcached服务的进程号) [root@linux ~]# kill 12177
1 [root@linux bin]# pwd 2 /root/application/program/memcached/memcachedfile/bin 3 [root@linux bin]# ./memcached -h/-help 4 memcached 15 5 -p <num> TCP port number to listen on (default: 11211) 6 -U <num> UDP port number to listen on (11211,0 off) 7 -s <file> UNIX socket path to listen on (disables network support) 8 -a <mask> access mask for UNIX socket,in octal (0700 9 -l <addr> interface to listen on (default: INADDR_ANY,all addresses) 10 <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 11 -d run as a daemon 12 -r maximize core file limit 13 -u <username> assume identity of <username> (only when run root) 14 -m <num> max memory to use for items in megabytes (64 MB) 15 -M return error on memory exhausted (rather than removing items) 16 -c <num> max simultaneous connections (102417 -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'). 18 -v verbose (print errors/warnings while in event loop) 19 -vv very verbose (also print client commands/reponses) 20 -vvv extremely verbose (also print internal state transitions) 21 -h print this help and exit 22 -i print memcached and libevent license 23 -P <file> save PID in <file>,only used with -d option 24 -f <factor> chunk size growth factor (1.2525 -n <bytes> minimum space allocated for key+value+flags (4826 -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. 27 -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. 28 -t <num> number of threads to use (429 -R Maximum number of requests per event,limits the number of requests process for a given connection to prevent starvation (2030 -C Disable use of CAS 31 -b Set the backlog queue limit (32 -B Binding protocol - one of ascii,binary,or auto (33 -I Override the size of each slab page. Adjusts max item size (: 1mb,min: 1k,max: 128m) 34 -o Comma separated list of extended or experimental options 35 - (EXPERIMENTAL) maxconns_fast: immediately close new connections if over maxconns limit 36 - 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. 37 [root@linux bin]#
1 C:UsersAdministrator>telnet192.168.127.130 11211 2 3 当前处于黑屏状态,等待输入命令,直接输入stats,直接回车。 4 stats 5 STAT pid 12177 6 STAT uptime 619 7 STAT time 1522221744 8 STAT version 9 STAT libevent stable 10 STAT pointer_size 64 11 STAT rusage_user 0.005197 12 STAT rusage_system 0.067566 13 STAT curr_connections 10 14 STAT total_connections 12 15 STAT connection_structures 11 16 STAT reserved_fds 20 17 STAT cmd_get 0 18 STAT cmd_set 19 STAT cmd_flush 20 STAT cmd_touch 21 STAT get_hits 22 STAT get_misses 23 STAT delete_misses 24 STAT delete_hits 25 STAT incr_misses 26 STAT incr_hits 27 STAT decr_misses 28 STAT decr_hits 29 STAT cas_misses 30 STAT cas_hits 31 STAT cas_badval 32 STAT touch_hits 33 STAT touch_misses 34 STAT auth_cmds 35 STAT auth_errors 36 STAT bytes_read 37 37 STAT bytes_written 21 38 STAT limit_maxbytes 67108864 39 STAT accepting_conns 1 40 STAT listen_disabled_num 41 STAT threads 4 42 STAT conn_yields 43 STAT hash_power_level 16 44 STAT hash_bytes 524288 45 STAT hash_is_expanding 46 STAT bytes 47 STAT curr_items 48 STAT total_items 49 STAT expired_unfetched 50 STAT evicted_unfetched 51 STAT evictions 52 STAT reclaimed 53 END 54 55 能连接上,说明memcache成功启用,可使用stats命令查看当前状态
1 [root@linux bin]# telnet 192.168.127.130 2 Trying 127.130... 3 Connected to . 4 Escape character ^] 5 当前窗口处于等待状态,可以直接输入命令执行 6 stats 直接运行该命令 7 STAT pid 8 STAT uptime 9 STAT time 10 STAT version 11 STAT libevent 12 STAT pointer_size 13 STAT rusage_user 14 STAT rusage_system 15 STAT curr_connections 16 STAT total_connections 17 STAT connection_structures 18 STAT reserved_fds 19 STAT cmd_get 20 STAT cmd_set 21 STAT cmd_flush 22 STAT cmd_touch 23 STAT get_hits 24 STAT get_misses 25 STAT delete_misses 26 STAT delete_hits 27 STAT incr_misses 28 STAT incr_hits 29 STAT decr_misses 30 STAT decr_hits 31 STAT cas_misses 32 STAT cas_hits 33 STAT cas_badval 34 STAT touch_hits 35 STAT touch_misses 36 STAT auth_cmds 37 STAT auth_errors 38 STAT bytes_read 39 STAT bytes_written 40 STAT limit_maxbytes 41 STAT accepting_conns 42 STAT listen_disabled_num 43 STAT threads 44 STAT conn_yields 45 STAT hash_power_level 46 STAT hash_bytes 47 STAT hash_is_expanding 48 STAT bytes 49 STAT curr_items 50 STAT total_items 51 STAT expired_unfetched 52 STAT evicted_unfetched 53 STAT evictions 54 STAT reclaimed 55 56 57 能连接上,说明memcache成功启用,可使用stats命令查看当前状态
[root@linux ~]# ps aux|grep memcached nobody 12177 0.0 0.0 323584 1052 ? Ssl 15:12 0:00 ./memcached -m d root 12292 116860 1012 pts/0 S+ 33 00 grep --color=auto memcached [root@linux ~]# ps -ef|12177 1 0 12 ? 00:12295 11965 33 pts/0 00 grep --color=auto memcached
在使用window的cmd连接Linux环境上的Memcached的时候,必须先把11211这个端口增加到防火墙,否则无法连接,执行命令如下: [root@linux ~]# firewall-cmd --zone=public --add-port=11211/tcp --permanent [root@linux ~]# firewall-cmd --reload 再次重新连接就没有问题了
(编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |