设为首页 收藏本站
查看: 839|回复: 0

[经验分享] Memcached的安装和应用

[复制链接]

尚未签到

发表于 2018-12-25 08:51:59 | 显示全部楼层 |阅读模式
  一,Memcached的安装
  1.安装libevent
  libevent是一个事件触发的网络库,适用于windows、linux、bsd等多种平台,内部使用select、epoll、kqueue等系统调用管理事件机制。memcached也是libevent based,而且libevent在使用上可以做到跨平台,而且根据libevent官方网站上公布的数据统计,似乎也有着非凡的性能。
  The libevent API provides a mechanism to execute a callback function when a specific event occurs on a file descriptor or after a timeout has been reached. Furthermore, libevent also support callbacks due to signals or regular timeouts.
  libevent is meant to replace the event loop found in event driven network servers. An application just needs to call event_dispatch() and then add or remove events dynamically without having to change the event loop.
  安装Memcached前需要先安装libevent,下载地址:
  http://libevent.org/
  [root@localhost home]# tar zxvf libevent-2.0.21-stable.tar.gz
  [root@localhost home]# cd libevent-2.0.21-stable
  [root@localhost libevent-2.0.21-stable]# ./configure
  [root@localhost libevent-2.0.21-stable]#make && make install
  2.安装Memcached
  下载地址:
  http://memcached.org/
  [root@bogon home]# tar zxvf memcached-1.4.15.tar.gz
  [root@bogon home]# cd memcached-1.4.15
  [root@bogon memcached-1.4.15]# ./configure
  [root@bogon memcached-1.4.15]# make && make install
  安装完成后,Memcached的默认安装目录是/usr/local/bin/memcached
  3.启动Memcached
  [root@bogon run]# /usr/local/bin/memcached -m 32m -p 11211 -d -u root -P /var/run/memcached.pid -c 256
  启动时可能出现如下错误:
  [root@bogon run]# /usr/local/bin/memcached -m 32m -p 11211 -d -u root -P /var/run/memcached.pid -c 256
  /usr/local/bin/memcached: error while loading shared libraries: libevent-2.0.so.5: cannot open shared object file: No such file or directory
  提示找不到libevent-2.0.so.5文件,解决办法是把/usr/local/lib
  加入到/etc/ld.so.conf中,/etc/ld.so.conf里保存的是想要读入高速缓存当中的动态函数库所在的目录,过程如下:
  [root@bogon run]# echo "/usr/local/lib">>/etc/ld.so.conf
  [root@bogon run]# ldconfig    #利用ldconfig这个可执行文件将/etc/ld.so.conf的数据读入到高速缓存中,
  [root@bogon script]# ldconfig -p|grep libevent-2.0.so.5    #ldconfig -p 查看libevent-2.0.so.5库已经被读入到高速缓存中了。
  libevent-2.0.so.5 (libc6) => /usr/local/lib/libevent-2.0.so.5
  启动过程参数说明:
  -p:使用TCP端口。默认是11211.
  -m:最大内存的大小。默认是64M。
  -vv:以very vrebose模式启动,将调试信息和错误输出到控制台。
  -d:做为守护进程在后台运行。
  -c:最大运行的并发连接数,默认是1024,按照服务器的负载量来设定。
  -P :设置保存Memcached的pid文件。
  -l:监听的服务器IP地址,如果有多个地址。
  -u:运行Memcached的用户,默认不能由root用户启动,所以当前用户为root时需要利用-u参数来指定。
  通过/usr/local/bin/memcached -h命令可以显示所有可选项:
  [root@bogon run]# /usr/local/bin/memcached -h
  memcached 1.4.15
  -p       TCP port number to listen on (default: 11211)
  -U       UDP port number to listen on (default: 11211, 0 is off)
  -s      UNIX socket path to listen on (disables network support)
  -a      access mask for UNIX socket, in octal (default: 0700)
  -l      interface to listen on (default: INADDR_ANY, all addresses)
   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  assume>  -m       max memory to use for items in megabytes (default: 64 MB)
  -M            return error on memory exhausted (rather than removing items)
  -c       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  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      save PID in , only used with -d option

  -f    chunk>  -n     minimum space allocated for key+value+flags (default: 48)
  -L            Try to use large memory pages (if available). Increasing

  the memory page>  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      Use  as the delimiter between key prefixes and>  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       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>  (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.
  4.关闭Memcached
  关闭Memcached的命令如下:
  [root@bogon script]# kill `cat /var/run/memcached.pid`  #注意是返单引号。
  5.安装Memcache的PHP扩展
  下载地址:http://pecl.php.net/package/memcache 我下载的是memcache-2.2.7.tgz稳定版
  [root@bogon home]# tar zxvf memcache-2.2.7.tgz
  [root@bogon home]# cd memcache-2.2.7
  执行phpize扩展安装程序,假设phpzie的路径为/usr/local/php/bin/phpize,具体的路径得根据自己的环境修改。
  [root@bogon memcache-2.2.7]# /usr/bin/phpize
  Configuring for:
  PHP Api Version:         20041225
  Zend Module Api No:      20050922
  Zend Extension Api No:   220051025
  注意,一定要在memcache-2.2.7目录下执行,否则会提示错误:
  Cannot find config.m4.
  Make sure that you run '/usr/bin/phpize' in the top level source directory of the module
  [root@bogon memcache-2.2.7]# ./configure --enable-memcache --with-php-config=/usr/bin/php-config
  如果提示如下错误提示:
  configure: error: memcache support requires ZLIB. Use --with-zlib-dir= to specify prefix where ZLIB include and library are located
  ERROR: `/var/cache/php-pear/memcache-2.2.5/configure --enable-memcache-session=yes' failed
  make sure you have zlib-devel installed
  这样解决:
  yum install zlib-devel
  [root@bogon memcache-2.2.7]# make && make install
  完成上述安装后会有类似以下提示:
  Installing shared extensions:     /usr/lib/php/modules/
  6.修改php.ini中extension_dir为:
  extension_dir = "/usr/lib/php/modules"
  增加如下一行来载入memcache.so扩展:
  extension=memcache.so
  7.测试Memcache的PHP扩展是否安装成功:
  运行下面PHP代码,如果输出“Hello world!“,就表示环境搭建成功。
  
  7.memcache的监控
  利用memcache.php对单台Memcached进行监控
  memcache.php下载地址:
  http://livebookmark.net/journal/2008/05/21/memcachephp-stats-like-apcphp/
  使用memcache.php需先修改用于访问的用户名和密码:
  vi memcache.php
  define('ADMIN_USERNAME','memcache');    // Admin Username
  define('ADMIN_PASSWORD','password');    // Admin Password
  …………
  $MEMCACHE_SERVERS[] = '192.168.56.2:11211'; // add more as an array #定义要查看的IP和端口
  $MEMCACHE_SERVERS[] = 'mymemcache-server2:11211'; // add more as an array #可定义多个
  然后可以通过URL访问memcache.php,输入用户名和密码后的状态图,如下:

  如果出现Cant connect to:192.168.56.2:11211错误:

  可通过关闭防火墙或开启防火墙端口11211的访问策略后重试,如果还不行就设置selinux为宽容模式,或者设置memcache.php文件正确的selinux策略。
  关闭iptables:
  [root@bogon html]# service iptables stop
  设置selinux为宽容模式:
  [root@bogon html]# setenforce 0


运维网声明 1、欢迎大家加入本站运维交流群:群②:261659950 群⑤:202807635 群⑦870801961 群⑧679858003
2、本站所有主题由该帖子作者发表,该帖子作者与运维网享有帖子相关版权
3、所有作品的著作权均归原作者享有,请您和我们一样尊重他人的著作权等合法权益。如果您对作品感到满意,请购买正版
4、禁止制作、复制、发布和传播具有反动、淫秽、色情、暴力、凶杀等内容的信息,一经发现立即删除。若您因此触犯法律,一切后果自负,我们对此不承担任何责任
5、所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其内容的准确性、可靠性、正当性、安全性、合法性等负责,亦不承担任何法律责任
6、所有作品仅供您个人学习、研究或欣赏,不得用于商业或者其他用途,否则,一切后果均由您自己承担,我们对此不承担任何法律责任
7、如涉及侵犯版权等问题,请您及时通知我们,我们将立即采取措施予以解决
8、联系人Email:admin@iyunv.com 网址:www.yunweiku.com

所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其承担任何法律责任,如涉及侵犯版权等问题,请您及时通知我们,我们将立即处理,联系人Email:kefu@iyunv.com,QQ:1061981298 本贴地址:https://www.yunweiku.com/thread-655504-1-1.html 上篇帖子: Ubuntu下安装Memcached 下篇帖子: python使用memcached
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

扫码加入运维网微信交流群X

扫码加入运维网微信交流群

扫描二维码加入运维网微信交流群,最新一手资源尽在官方微信交流群!快快加入我们吧...

扫描微信二维码查看详情

客服E-mail:kefu@iyunv.com 客服QQ:1061981298


QQ群⑦:运维网交流群⑦ QQ群⑧:运维网交流群⑧ k8s群:运维网kubernetes交流群


提醒:禁止发布任何违反国家法律、法规的言论与图片等内容;本站内容均来自个人观点与网络等信息,非本站认同之观点.


本站大部分资源是网友从网上搜集分享而来,其版权均归原作者及其网站所有,我们尊重他人的合法权益,如有内容侵犯您的合法权益,请及时与我们联系进行核实删除!



合作伙伴: 青云cloud

快速回复 返回顶部 返回列表