前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >memcached服务

memcached服务

作者头像
用户1173509
发布2018-03-28 12:43:45
1.1K0
发布2018-03-28 12:43:45
举报
文章被收录于专栏:CaiRuiCaiRui

介绍

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

用于动态应用系统中缓存数据库的数据,减少数据库的访问压力,达到提升性能的效果,实际应用环境中多用于数据库的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 应用启用------->重启数据库服务器
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@cairui ~]# cat /etc/redhat-release 
CentOS release 6.8 (Final)
[root@cairui ~]# uname -r
2.6.32-696.6.3.el6.x86_64

libevent安装

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

安装memcached

[root@cairui libevent-2.1.8-stable]# yum install memcached
[root@cairui 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@cairui lib]# echo "/usr/local/lib">>/etc/ld.so.conf
[root@cairui lib]# ldconfig 
[root@cairui lib]# which memcached
/usr/bin/memcached

memcached参数介绍

[root@cairui 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@cairui lib]# memcached -p 11111 -u root -c 1024 -d
[root@cairui 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@cairui 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@cairui ~]# printf "set key001 0 0 10\r\ntest123456\r\n"|nc 127.0.0.1 11111  #写入数据
STORED
[root@cairui ~]# printf "get key001\r\n"|nc 127.0.0.1 11111  #查询数据
VALUE key001 0 10
test123456
END
[root@cairui ~]# printf "delete key001\r\n"|nc 127.0.0.1 11111  #删除数据
DELETED
[root@cairui ~]# 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
无需安装,解压即可
本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2018-03-23 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 介绍
  • 定义
  • mysql已经有cache了,为啥还要在它前面加一层memcached?
  • memcached的应用场景
  • memcached应用中的工作流程
  • 特性
    • memcached软件的工作原理
    • 安装与配置
      • memcached参数介绍
        •  配置memcached
          • memcached监控软件介绍
          相关产品与服务
          对象存储
          对象存储(Cloud Object Storage,COS)是由腾讯云推出的无目录层次结构、无数据格式限制,可容纳海量数据且支持 HTTP/HTTPS 协议访问的分布式存储服务。腾讯云 COS 的存储桶空间无容量上限,无需分区管理,适用于 CDN 数据分发、数据万象处理或大数据计算与分析的数据湖等多种场景。
          领券
          问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档