tyxiayu 发表于 2018-12-26 07:20:09

菜鸟学Linux 第098篇笔记 memcached-Winthcloud

  菜鸟学Linux 第098篇笔记 memcached
  内容总览
  memcached 缓存服务器
  memcached 安装与配置
  配置php调用memcached
  配置php将会话保存至memcached中
  图形化的memcached监控和管理工具
  memcached 缓存服务器,但本身无法决定缓存任何数据
  一半依赖于客户端,一半依赖于服务器
  LRU 最近最少使用
  内存缓存服务器 最小不能少于48bytes 最大1mb
  bubby system 伙伴系统   避免内存外碎片
  slab allocator (slab分配器)避免内存内碎片
  memcached: 不通信分布式缓存服务器
  port 11211
  memcached客户端库程序
  perl module
  cache::memcached
  php
  memcache
  memcached
  c/c++
  libmemcached
  memadmin (图形化来管理和查看)
  memcached 安装与配置
  准备工作 下载memcached libevent 安装memcached时有依赖libevent
  1.安装libevent 和 memcached
  安装libevent
  # tar -xf libevent-2.1.8-stable.tar.gz
  # cd libevent-2.1.8-stable
  # ./configure --prefix=/usr/local/libevent
  # make && make install
  安装memcached
  # tar xf memcached-1.4.34.tar.gz
  # cd memcached-1.4.34
  # ./configure --prefix=/usr/local/memcacled --with-libevent=/usr/local/libevent/
  (到此安装完成)
  2. 配置memcached
  memcached命令
  -p       TCP port number to listen on (default: 11211)
  -U       UDP port number to listen on (default: 11211, 0 is off)
  -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

  -uassume>  -m       max memory to use for items in megabytes (default: 64 MB)

  -f    chunk>  -n   minimum space allocated for key+value+flags (default: 48)
  # /usr/local/memcacled/bin/memcached -m 128 -n 20 -f 1.2 -vv -u nobody -d
  3. 测试memcached
  add 命令
  add keyname flag timeout datasize
  如
  add firstkey 0 20 5
  hello
  get 命令
  get firstkey
  # telnet localhost 11211
  add firstkey 0 20 5
  hello
  get firstkey
  (此时memcached可以正常工作了)
  4. 添加服务脚本为memcached
  #!/bin/bash
  #
  # Init file for memcached
  #
  # chkconfig: - 86 14
  # description: Distributed memory caching daemon
  #
  # processname: memcached
  # config: /etc/sysconfig/memcached
  . /etc/rc.d/init.d/functions
  ## Default variables
  PORT="11211"
  USER="nobody"
  MAXCONN="1024"
  CACHESIZE="64"
  OPTIONS=""
  [ -f /etc/sysconf/memcached ] && . /etc/sysconfig/memcached
  RETVAL=0
  prog="/usr/local/memcached/bin/memcached"
  desc="Distributed memory caching"
  lockfile="/var/lock/subsys/memcached"
  start() {
  echo -n $"Starting $desc (memcached): "
  daemon $prog -d -p $PORT -u $USER -c $MAXCONN -m $CACHESIZE "$OPTIONS"
  RETVAL=$?
  echo
  [ $RETVAL -eq 0 ] && touch $lockfile
  return $RETVAL
  }
  stop() {
  echo -n $"Shutting down $desc (memcached): "
  killproc $prog
  RETVAL=$?
  echo
  [ $RETVAL -eq 0 ] && rm -f $lockfile
  return $RETVAL
  }
  restart() {
  stop
  start
  }
  reload() {
  echo -n $"Reloading $desc ($prog): "
  killproc $prog -HUP
  RETVAL=$?
  echo
  return $RETVAL
  }
  case "$1" in
  start)
  start
  ;;
  stop)
  stop
  ;;
  restart)
  restart
  ;;
  condrestart)
  [ -e $lockfile ] && restart
  RETVAL=$?
  ;;
  reload)
  reload
  ;;
  status)
  status $prog
  RETVAL=$?
  ;;
  *)
  echo $"Usage: $0 {start|stop|restart|condrestart|status}"
  RETVAL=1
  esac
  exit $RETVAL
  添加可执行
  配置php调用memcached
  1. 安装php
  所依赖的安装包
  libmcrypt-2.5.8-4.el5.centos.i386.rpm
  libmcrypt-devel-2.5.8-4.el5.centos.i386.rpm
  mhash-0.9.9-1.el5.centos.i386.rpm
  mhash-devel-0.9.9-1.el5.centos.i386.rpm
  mcrypt-2.6.8-1.el5.i386.rpm
  libevent
  openssl-devel
  bzip2-devel
  libcurl-devel
  查看此些包可以到网站rpmfind.net
  下载完成后安装
  安装php
  # tar -xf php-5.4.13.tar.bz2
  # cd php-5.4.13
  # ./configure --prefix=/usr/local/php --with-mysql=/usr/local/mysql \
  --with-openssl --enable-fpm --enable-sockets --enable-sysvshm\
  --with-mysqli=/usr/local/mysql/bin/mysql_config --enable-mbstring \
  --with-freetype-dir --with-jpeg-dir --with-png-dir --with-zlib-dir \
  --with-libxml-dir=/usr --enable-xml--with-mhash --with-mcrypt \
  --with-config-file-path=/etc --with-config-file-scan-dir=/etc/php.d \
  --with-bz2 --with-curl --with-libevent-dir=/usr/local/libevent/
  (此为一行命令)
  # make && make install
  # cd /usr/local/php/etc
  # cp php-fpm.conf.default php-fpm.conf
  # vim php-fpm.conf (修改值)
  pm.max_children = 150
  pm.start_servers = 5
  pm.min_spare_servers = 5
  pm.max_spare_servers = 10
  # cd /root/mysql-compile/php-5.4.13
  # cp sapi/fpm/init.d.php-fpm /etc/init.d/php-fpm
  # chmod +x /etc/init.d/php-fpm
  # chkconfig --add php-fpm
  # service php-fpm start
  # netstat -tunlp(查看9000端口是否开启)
  2. 配置nginx调用php
  # vim /etc/nginx/nginx.conf
  将如下的location前边#号去掉
  location ~ \.php$ {
  root         html;
  fastcgi_pass   127.0.0.1:9000;
  fastcgi_indexindex.php;
  fastcgi_paramSCRIPT_FILENAME/scripts$fastcgi_script_name;
  include      fastcgi_params;
  }
  # vim /etc/nginx/fastcgi_params
  删除里边内容替换为如下选项
  fastcgi_paramGATEWAY_INTERFACECGI/1.1;
  fastcgi_paramSERVER_SOFTWARE    nginx;
  fastcgi_paramQUERY_STRING       $query_string;
  fastcgi_paramREQUEST_METHOD   $request_method;
  fastcgi_paramCONTENT_TYPE       $content_type;
  fastcgi_paramCONTENT_LENGTH   $content_length;
  fastcgi_paramSCRIPT_FILENAME    $document_root$fastcgi_script_name;
  fastcgi_paramSCRIPT_NAME      $fastcgi_script_name;
  fastcgi_paramREQUEST_URI      $request_uri;
  fastcgi_paramDOCUMENT_URI       $document_uri;
  fastcgi_paramDOCUMENT_ROOT      $document_root;
  fastcgi_paramSERVER_PROTOCOL    $server_protocol;
  fastcgi_paramREMOTE_ADDR      $remote_addr;
  fastcgi_paramREMOTE_PORT      $remote_port;
  fastcgi_paramSERVER_ADDR      $server_addr;
  fastcgi_paramSERVER_PORT      $server_port;
  fastcgi_paramSERVER_NAME      $server_name;
  3. 配置php使用memcache
  需要到官方站点 pecl PECL is a repository for PHP Extensions
  pecl.php.net下载memcache
  编译和安装memcache
  # tar -xf memcache-2.2.7.tgz
  # cd memcache-2.2.7
  # /usr/local/php/bin/phpize
  # ./configure --with-php-config=/usr/local/php/bin/php-config --enable-memcache
  # make && make install
  安装完成会有如下一条信息,将其复制
  /usr/local/php/lib/php/extensions/no-debug-non-zts-20100525/
  # mkdir /etc/php.d
  # vim /etc/php.d/memcache.ini
  extension=/usr/local/php/lib/php/extensions/no-debug-non-zts-20100525/memcache.so
  上边此行的目录就是刚刚复制的那个目录,后边加了一个memcache.so
  # service php-fpm restart
  此时php便可使用memcache内存缓存
  4. 测试memcache是否被php调用
  vim /web/html/test.php
  
页: [1]
查看完整版本: 菜鸟学Linux 第098篇笔记 memcached-Winthcloud