阿里云-云小站(无限量代金券发放中)
【腾讯云】云服务器、云数据库、COS、CDN、短信等热卖云产品特惠抢购

memcached服务详解

157次阅读
没有评论

共计 5671 个字符,预计需要花费 15 分钟才能阅读完成。

介绍

它是一套数据缓存系统或软件

用于动态应用系统中缓存数据库的数据,减少数据库的访问压力,达到提升性能的效果,实际应用环境中多用于数据库的 cache 的应用。它是通过预分配指定的内存空间来存储数据

定义

它是一个开源的、高性能的,具有分布式内存对象的缓存系统,它一般用来存储经常读取的对象或数据,如同 web 服务器会将一些内容缓存到客户端本地一样

MySQL 已经有 cache 了,为啥还要在它前面加一层 memcached?

memcached 是一个 key/value 系统,系统相对于 MySQL 简单很多,虽然 MySQL 也有 cache,但是数据库的 SQL 解析会耗费性能,查询慢于 memcached,另外 MySQL 的 cache 设计得更加复杂,因为要考虑事务,日志,存储引擎等模块,它的性能也没有 memcached 好

memcached 只做一件事情,简单高效,在 cache 上比 MySQL 强,这应该容易理解

memcached 的应用场景

数据库的前端缓存应用:让它来分担数据的并发压力,当数据更新时,可以使程序通知缓存进行更新

session 会话共享的共享存储

memcached 应用中的工作流程

它是一种内存缓存,可通过 API 的方式读取内存中缓存的这些数据,当用户需要读取数据时,会首先访问 memcached 内存缓存,如果缓存中有数据就直接返回给前端的应用程序,如果没有,再转发给后台端的服务器,这时服务器除了返回数据给用户,还会将数据更新给 memcached 缓存。

如果实际生产环境中,缓存服务器需要重启(或者断电),那么缓存中的数据将会丢失,那么这时后端的服务器
并发压力会增大,可能会导致后端的服务器也跟着宕机,无法提供服务,
那么这时我们的处理流程是这样的:
首先从负载均衡中将 WEB 应用停掉 —–> 让负载均衡不再转发数据给 WEB—-> 接着启动缓存服务器 ——–>
通过程序把数据库的内容初始化到缓存服务器中 ——-> 然后将 web 应用启用 ——-> 重启数据库服务器

memcached 服务详解

1、设置缓存(expires)和 deflate 压缩,可以将一些内容直接缓存在用户端的本地,下次访问直接调用本地
2、CDN 缓存静态内容(html、图片等),当用户请求这些内容时直接调用 CDN 的内容,不再请求后端服务器了
3、Apache 和 Nginx 静态服务器提供静态内容(通过异步消息队列生成静态内容)
4、PHP 和 Java 动态内容
5、数据库的 memcached 缓存服务器
6、数据库服务器(MYSQL)
7、数据库的存储服务器

特性

协议简单:协议使用比较简单,使用基于文本行的协议

基于 libevent 的事件处理

memcached 软件的工作原理

它是一套 C / S 模式架构的软件,在服务器端启动服务守护进程,可以为 memcached 服务器指定监听的 IP 地址、端口号、并发访问连接数以及分配多少内存来处理客户的请求参数

需要被缓存的数据以 key/value 键值对的形式在服务器端预分配的内存区中,每个被缓存的数据都有唯一的标识 key

安装与配置

[root@linuxidc ~]# cat /etc/RedHat-release
CentOS release 6.8 (Final)
[root@linuxidc ~]# uname -r
2.6.32-696.6.3.el6.x86_64

libevent 安装

[root@linuxidc software]# tar xf libevent-2.1.8-stable.tar.gz
[root@linuxidc software]# cd libevent-2.1.8-stable
[root@linuxidc libevent-2.1.8-stable]# ./configure
[root@linuxidc libevent-2.1.8-stable]# make
[root@linuxidc libevent-2.1.8-stable]# make install

安装 memcached

[root@linuxidc libevent-2.1.8-stable]# yum install memcached
[root@linuxidc libevent-2.1.8-stable]# find / -name memcached
/etc/sysconfig/memcached
/etc/rc.d/init.d/memcached
/usr/bin/memcached
/var/run/memcached

配置 ld.so.conf 路径防止启动时报错

[root@linuxidc lib]# echo “/usr/local/lib”>>/etc/ld.so.conf
[root@linuxidc lib]# ldconfig
[root@linuxidc lib]# which memcached
/usr/bin/memcached

memcached 参数介绍

[root@linuxidc lib]# /usr/bin/memcached -h
memcached 1.4.4
-p <num>      TCP port number to listen on (default: 11211) #指定监听端口
-U <num>      UDP port number to listen on (default: 11211, 0 is off)
-s <file>    UNIX socket path to listen on (disables network support)
-a <mask>    access mask for UNIX socket, in octal (default: 0700)
-l <ip_addr>  interface to listen on (default: INADDR_ANY, all addresses) #指定监听地址
-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: 64 MB) #分配内存区大小
-M            return error on memory exhausted (rather than removing items)
-c <num>      max simultaneous connections (default: 1024) #最大并发数
-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) #以 very verbose 模式启动
-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 #设置保存 memcached 的 PID
-f <factor>  chunk size growth factor (default: 1.25)
-n <bytes>    minimum space allocated for key+value+flags (default: 48)
-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: 4)
-R            Maximum number of requests per event, limits the number of
              requests process for a given connection to prevent
              starvation (default: 20)
-C            Disable use of CAS
-b            Set the backlog queue limit (default: 1024)
-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)

配置 memcached

[root@linuxidc lib]# memcached -p 11111 -u root -c 1024 -d
[root@linuxidc lib]# lsof -i:11111
COMMAND    PID USER  FD  TYPE DEVICE SIZE/OFF NODE NAME
memcached 17416 root  26u  IPv4  90768      0t0  TCP *:vce (LISTEN)
memcached 17416 root  27u  IPv4  90785      0t0  UDP *:vce
[root@linuxidc lib]# netstat -lntup|grep mem
tcp        0      0 0.0.0.0:11111              0.0.0.0:*                  LISTEN      17416/memcached   
udp        0      0 0.0.0.0:11111              0.0.0.0:*                              17416/memcached

 写入数据

[root@linuxidc ~]# printf “set key001 0 0 10\r\ntest123456\r\n”|nc 127.0.0.1 11111 #写入数据
STORED
[root@linuxidc ~]# printf “get key001\r\n”|nc 127.0.0.1 11111 #查询数据
VALUE key001 0 10
test123456
END
[root@linuxidc ~]# printf “delete key001\r\n”|nc 127.0.0.1 11111 #删除数据
DELETED
[root@linuxidc ~]# printf “get key001\r\n”|nc 127.0.0.1 11111
END
 
正确关闭 memcached 服务
一般启动时最好指定下 PID
[root@LVS-2 ~]# memcached -p 11213 -u root -m 16m -c 1024 -d -P /var/run/11213.pid #指定进程文件
[root@LVS-2 ~]# kill `cat /var/run/11213.pid`
提示:实际生产环境中,要布署 memcached 服务,做为数据缓存,内存的指定大小是根据业务来确定,是不是需要负载均衡?最好同开发一起讨论确定好,还有它还有分客户端与服务端
配置 session 会话共享存储
vi /applicaton/php/lib/php.ini
session.save_path = “/tmp” 改成 memcached 服务器地址 tcp ://10.10.10.1:11211
session.save_handler = files(改成 memcached)
将所有的 WEB 服务器 (WEB 服务器需要安装客户端) 里的 PHP 配置文件改成上述配置,即可完成会话共享存储

memcached 监控软件介绍

[root@LB01 vhost]# cd /web/www/
[root@LB01 www]# wget http://www.junopen.com/memadmin/memadmin-1.0.12.tar.gz
[root@LB01 www]# tar zxf memadmin-1.0.12.tar.gz
无需安装,解压即可

memcached 服务详解

memcached 服务详解

本文永久更新链接地址:http://www.linuxidc.com/Linux/2017-08/146597.htm

正文完
星哥说事-微信公众号
post-qrcode
 0
星锅
版权声明:本站原创文章,由 星锅 于2022-01-21发表,共计5671字。
转载说明:除特殊说明外本站文章皆由CC-4.0协议发布,转载请注明出处。
【腾讯云】推广者专属福利,新客户无门槛领取总价值高达2860元代金券,每种代金券限量500张,先到先得。
阿里云-最新活动爆款每日限量供应
评论(没有评论)
验证码
【腾讯云】云服务器、云数据库、COS、CDN、短信等云产品特惠热卖中