一、引言

写有关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环境下的使用详解的更多相关文章

  1. Memcached在Linux环境下的使用详解http://blog.51cto.com/soysauce93/1737161

    大纲 一.什么是memcached 二.memcached特性 三.memcached存储方式 四.memcached安装与配置 五.memcached结合php 六.Nginx整合memcached ...

  2. [r]Ubuntu Linux系统下apt-get命令详解

    Ubuntu Linux系统下apt-get命令详解(via|via) 常用的APT命令参数: apt-cache search package 搜索包 apt-cache show package ...

  3. 【转】postgresql 9.4 在linux环境的安装步骤详解

    本文章来为各位介绍一篇关于postgresql 9.4 在linux环境的安装步骤详解,希望文章能够对各位新手朋友带来帮助的哦. 环境说明系统:centos 6.4 64位软件:postgresql ...

  4. Ubuntu Linux系统下apt-get命令详解

    整理了Ubuntu Linux操作系统下apt-get命令的详细说明,分享给大家.常用的APT命令参数: apt-cache search package 搜索包 apt-cache show pac ...

  5. linux环境vnc部署过程详解

    vnc服务端机器地址:10.165.38.68 vnc客户端机器地址:本机(windows机器) vnc客户端包:vnc_82537_82537.rar (百度云盘下载地址:http://pan.ba ...

  6. linux环境变量 export命令详解

    由host $ export DVSDK="${HOME}/ti-dvsdk_dm368-evm_xx_xx_xx_xx"引发的问题 1.${HOME}:首先, HOME 是个变量 ...

  7. Linux系统下Nginx安装详解

    该随笔为个人原创,后期会根据项目实践实时更新,如若转载,请注明出处,方便大家获得最新博文! 注:安装Nginx需要Linux系统已经安装   openssl-fips-2.0.2.tar.gz zli ...

  8. 在linux系统下安装mysql详解,以及远程调用连接不上mysql的解决方法。

    步骤: 1)查看CentOS自带的mysql 输入 rpm -qa | grep mysql 2)将自带的mysql卸载 3)上传Mysql的安装包到linux 4)安装mysql的依赖(不是必须) ...

  9. linux环境下/etc/hosts文件详解

    linux环境下/etc/hosts文件详解 就没一个昵称能用关注 0.0632017.09.12 17:04:28字数 623阅读 27,096 介绍 hosts文件是linux系统中负责ip地址与 ...

随机推荐

  1. 【52ABP实战教程】0.1-- Devops如何用VSTS持续集成到Github仓库!

    工欲善其事,必先利其器.在开始正式的教程之前我们先来聊聊准备工作. 管理工具会VSTS. 代码管理会用GITHUB. 服务器会用Azure. 所有的东西都是利用现有服务.不会说自己从虚拟机开始玩.我们 ...

  2. Let's Encrypt,免费好用的 HTTPS 证书

    很早之前我就在关注 Let's Encrypt 这个免费.自动化.开放的证书签发服务.它由 ISRG(Internet Security Research Group,互联网安全研究小组)提供服务,而 ...

  3. python爬虫requests 下载图片

    import requests # 这是一个图片的url url = 'http://yun.itheima.com/Upload/Images/20170614/594106ee6ace5.jpg' ...

  4. mysql基础练习题

    一.表关系 请创建如下表,并创建相关约束 二.操作表 1.自行创建测试数据 /* Navicat MySQL Data Transfer Source Server : mysql5.7.1 Sour ...

  5. Hibernate(三): org.hibernate.HibernateException: No CurrentSessionContext configured!

    Hibernate版本5.2.9 获取Session的方式是sessionFactory.getCurrentSession(); 比较老一些的版本使用的是sessionFactory.openSes ...

  6. 使用multiprocessing模块创建多进程

    # 使用multiprocessing模块创建多进程 # multiprcessing模块提供了一个Process类来描述一个进程对象. # 创建子进程时,只需要传入一个执行函数和函数的参数,即可完成 ...

  7. hasattr(obj,attr) 判断前面是否有后面的属性

    hasattr(obj,attr) 判断前面是否有后面的属性

  8. 微信小程序:wx.request之post请求后端无法获取数据的问题

    前言:小程序的开发中总是踩到各种坑,看文档也不知所云: 例如当我们在写微信小程序接口时,method请求方式有POST和GET两种,为了数据安全,我们会偏向于使用POST请求方式访问服务器端: 问题: ...

  9. Python 破解带密码保护的Zip文件

    今天发生了个有趣的事情,有个朋友发了一个带密码保护的Zip文件给我,却不给我密码,我就琢磨这怎么可以'猜'到密码呢? 经过一系列的尝试,最终使用python把这个密码给'猜'出来了.要想写出破解密码的 ...

  10. if else if,switch case二者的联系与区别

    前段时间在学习中听到了一个关于条件判断语句的问题,分析if else if语句和switch case语句这两者之间的联系和区别,从而使用其中最有效率的一种方法. 一.if...else if if. ...