Memcached在Linux环境下的使用详解
一、引言
写有关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了,而且很多的公司也不使用它了,所以由于原因种种,该系列就往后推了。
Memcached在Linux环境下的使用详解的更多相关文章
- Memcached在Linux环境下的使用详解http://blog.51cto.com/soysauce93/1737161
大纲 一.什么是memcached 二.memcached特性 三.memcached存储方式 四.memcached安装与配置 五.memcached结合php 六.Nginx整合memcached ...
- [r]Ubuntu Linux系统下apt-get命令详解
Ubuntu Linux系统下apt-get命令详解(via|via) 常用的APT命令参数: apt-cache search package 搜索包 apt-cache show package ...
- 【转】postgresql 9.4 在linux环境的安装步骤详解
本文章来为各位介绍一篇关于postgresql 9.4 在linux环境的安装步骤详解,希望文章能够对各位新手朋友带来帮助的哦. 环境说明系统:centos 6.4 64位软件:postgresql ...
- Ubuntu Linux系统下apt-get命令详解
整理了Ubuntu Linux操作系统下apt-get命令的详细说明,分享给大家.常用的APT命令参数: apt-cache search package 搜索包 apt-cache show pac ...
- linux环境vnc部署过程详解
vnc服务端机器地址:10.165.38.68 vnc客户端机器地址:本机(windows机器) vnc客户端包:vnc_82537_82537.rar (百度云盘下载地址:http://pan.ba ...
- linux环境变量 export命令详解
由host $ export DVSDK="${HOME}/ti-dvsdk_dm368-evm_xx_xx_xx_xx"引发的问题 1.${HOME}:首先, HOME 是个变量 ...
- Linux系统下Nginx安装详解
该随笔为个人原创,后期会根据项目实践实时更新,如若转载,请注明出处,方便大家获得最新博文! 注:安装Nginx需要Linux系统已经安装 openssl-fips-2.0.2.tar.gz zli ...
- 在linux系统下安装mysql详解,以及远程调用连接不上mysql的解决方法。
步骤: 1)查看CentOS自带的mysql 输入 rpm -qa | grep mysql 2)将自带的mysql卸载 3)上传Mysql的安装包到linux 4)安装mysql的依赖(不是必须) ...
- linux环境下/etc/hosts文件详解
linux环境下/etc/hosts文件详解 就没一个昵称能用关注 0.0632017.09.12 17:04:28字数 623阅读 27,096 介绍 hosts文件是linux系统中负责ip地址与 ...
随机推荐
- 阿里云API网关(8)开发指南-SDK下载
网关指南: https://help.aliyun.com/document_detail/29487.html?spm=5176.doc48835.6.550.23Oqbl 网关控制台: https ...
- Python入门之函数的介绍/定义/定义类型/函数调用/Return
本篇目录: 一. 函数的介绍 二. 函数的定义 三. 定义函数的三种类型 四. 函数调用的阶段 五. Return返回值 ======================================= ...
- IT 必备电脑快捷键
IT 必备电脑快捷键 键盘上除了有字母.数字之外,还有一些特殊的按键:ctrl.shift.alt.tab ● ctrl键是英语control“控制”的意思,这个按键,单独按没有任何作用,都要和其他的 ...
- POJ-1751 Highways---确定部分边的MST
题目链接: https://vjudge.net/problem/POJ-1751 题目大意: 有一个N个城市M条路的无向图,给你N个城市的坐标,然后现在该无向图已经有M条边了,问你还需要添加总长为多 ...
- CBC翻转攻击(实验吧_简单的登陆题)
题目链接 http://ctf5.shiyanbar.com/web/jiandan/index.php 有源码在test.php页面 分析代码过程 如果post id,将id转字符串,然后进入sql ...
- canvas图像模糊以及图像变形问题
问题:有时用canvas作图时发现图像会出现模糊不清晰的问题,甚至做出来的图呈现出的效果与我们给的数值所应该呈现出的效果不一致 原因:当你在支持html5 canvas的浏览器下查看页面的时候,can ...
- django 开发忘记密码通过邮箱找回功能
一.流程分析: 1.点击忘记密码====>forget.html页面,输入邮箱和验证码,发送验证链接网址的邮件====>发送成功,跳到send_success.html提示 2.到邮箱里找 ...
- linux实现文件的去重【转】
(1)两个文件的交集,并集 1. 取出两个文件的并集(重复的行只保留一份) cat file1 file2 | sort | uniq > file3 2. 取出两个文件的交集(只留下同时存在于 ...
- Hibernate注解用法
@NotNull 适用于基本类型,任何对象的value不能为null. @NotBlank 适用于String类型,只能用于字符串不为null,并且字符串trim()以后length要大于0 @Not ...
- maven引入本地jar 打jar包
没搭建私服的情况下引入本地的jar,并把本地jar打包进项目的run jar 以打包引入hadoop-common-2.7.5.jar为例 引用 复制jar包所在的路径 打开cmd命令提示符 切换路径 ...