lnmp----添加缓存(memcache)

MemCache是分布式的高速缓存系统,通过在内存里维护一个同一的巨大的hash表,它能够用来存储各种格式的数据,包括图像、视频、文件以及数据库检索的结果等。简单的说就是将数据调用到内存中,然后从内存中读取。从而提高读取速度。

实验前提:

在搭建好lnmp架构的基础上进行。
可查看mariadb的安装与配置
php的安装与配置
nginx的安装与配置


  • 解压memcache包:

      [root@server1 ~]# tar zxf memcache-2.2.5.tgz 
      [root@server1 ~]# cd memcache-2.2.5
    

  • 添加环境变量:

      [root@server1 memcache-2.2.5]# vim ~/.bash_profile
      	PATH=$PATH:$HOME/bin:/usr/local/lnmp/mysql/bin:/usr/local/lnmp/php/bin
      [root@server1 memcache-2.2.5]# source ~/.bash_profile 
      
      [root@server1 memcache-2.2.5]# phpize
      Configuring for:
      PHP Api Version:         20131106
      Zend Module Api No:      20131226
      Zend Extension Api No:   220131226
    

  • 编译、安装

      [root@server1 memcache-2.2.5]# ./configure
      [root@server1 memcache-2.2.5]# make && make install
    

  • 配置

      [root@server1 memcache-2.2.5]# cd /usr/local/lnmp/php/etc/
      [root@server1 etc]# vim php.ini 
       873  extension=memcache.so
      [root@server1 etc]# /etc/init.d/php-fpm reload
      [root@server1 etc]# php -m | grep memcache(查看php有模块memcache)
      memcache
      [root@server1 etc]# yum install -y memcached
      	
      [root@server1 etc]# vim /etc/sysconfig/memcached 
      PORT="11211"
      USER="memcached"
      MAXCONN="1024"
      CACHESIZE="64"
      OPTIONS=""(如果这里指定ip只监控指定ip的端口,没有是监控所有端口)
      [root@server1 etc]# /etc/init.d/memcached start(打开服务)
      
      [root@server1 etc]# netstat -tnlp(查看开启的服务的端口号)
      Active Internet connections (only servers)
      Proto Recv-Q Send-Q Local Address               Foreign Address             State       PID/Program name   
      tcp        0      0 0.0.0.0:11211               0.0.0.0:*                   LISTEN      28121/memcached     
    

  • 安装telnet(查看是否远程通信)

      [root@server1 ~]# yum install telnet
      [root@server1 etc]#  telnet localhost 11211
      Trying ::1...
      Connected to localhost.
      Escape character is '^]'.
      set name 0 0 6
      westos
      STORED
      wwwwwww
      ERROR
      get name
      VALUE name 0 6
      westos
      END
      set name 0 10 6         
      hello1
      STORED
      get name
      VALUE name 0 6
      hello1
      END
      get name       
      END
      stats(可查看版本信息)
      STAT pid 22465
      STAT uptime 13172
      STAT time 1550668310
      STAT version 1.4.4
      STAT pointer_size 64
      STAT rusage_user 0.006998
    

  • 编辑memcache文件

      [root@server1 memcache-2.2.5]# cp memcache.php example.php  /usr/local/lnmp/nginx/html/
      [root@server1 ~]# cd /usr/local/lnmp/nginx/html/
      [root@server1 html]# vim memcache.php 
      
      	 20 $VERSION='$Id: memcache.php,v 1.2 2008/09/11 19:21:06 mikl Exp $';
      	 21 
      	 22 define('ADMIN_USERNAME','memcache');    // Admin Username
      	 23 define('ADMIN_PASSWORD','westos');     (修改密码) // Admin Password
      	 24 define('DATE_FORMAT','Y/m/d H:i:s');
      	 25 define('GRAPH_SIZE',200);
      	 26 define('MAX_ITEM_DUMP',50);
      	 27 
      	 28 $MEMCACHE_SERVERS[] = '172.25.55.1:11211'; // add more as an array(指定ip)
      	 29 #$MEMCACHE_SERVERS[] = 'mymemcache-server2:11211'; // add more as an     array
    

  • 访问网页
    在这里插入图片描述
    http:/ /172.25.55.1/example.php(多访问几次)
    在这里插入图片描述
    刷新memcache网页可查看到数据大多来此缓存,只有小部分是直接访问服务端。
    在这里插入图片描述

  • 用ab命令模拟访问服务器的数据流量

未加memcache时:

[root@server1 ~]# ab -c 10 -n 5000 http:/ /172.25.55.1/index.php	
Time taken for tests:   10.235 seconds(用时10s)
Complete requests:      5000
Failed requests:        502(失败了502个)
   (Connect: 0, Receive: 0, Length: 502, Exceptions: 0)
Write errors:           0

添加memcache时:

[root@server1 ~]# ab -c 10 -n 5000 http://172.25.55.1/example.php
Time taken for tests:   3.418 seconds(用时3秒)
Complete requests:      5000
Failed requests:        0(没有失败)
Write errors:           0

猜你喜欢

转载自blog.csdn.net/weixin_43328213/article/details/87807911