[关闭]
@yanglt7 2018-10-21T15:58:12.000000Z 字数 9556 阅读 686

【Web 集群实战】21_Memcached 服务应用

Web集群实战


1. Memcached 介绍

1.1 Memcached 是什么?

1.2 Memcached 的作用

1.3 Memcached 常见用途工作流程

1.4 网站读取 Memcached 数据时工作流程

1.5 网站更新 Memcached 数据时的工作原理

1.6 Memcached 的特点

1.7 Memcached 内存管理机制

1.8 Memcached 的检测过期与删除机制

2. Memcached 服务安装

2.1 安装 libevent 及连接 Memcached 工具 nc

  1. [root@cache001 ~]# cat /etc/redhat-release
  2. CentOS Linux release 7.4.1708 (Core)
  3. [root@cache001 ~]# uname -m
  4. x86_64
  5. [root@cache001 ~]# uname -r
  6. 3.10.0-693.el7.x86_64
  1. [root@cache001 ~]# yum install -y libevent libevent-devel nc
  2. [root@cache001 ~]# rpm -qa libevent libevent-devel nc
  3. libevent-devel-2.0.21-4.el7.x86_64
  4. libevent-2.0.21-4.el7.x86_64

2.2 安装 Memcached

  1. [root@cache001 ~]# yum install -y memcached
  2. [root@cache001 ~]# rpm -qa memcached
  3. memcached-1.4.15-10.el7_3.1.x86_64

3. Memcached 服务的基本管理

3.1 启动 Memcached

  1. [root@cache001 ~]# which memcached
  2. /bin/memcached
  3. [root@cache001 ~]# memcached -m -16m -p 11211 -d -u root -c 8192
  1. [root@cache001 ~]# lsof -i :11211
  2. COMMAND PID USER FD TYPE DEVICE SIZE/OFF NODE NAME
  3. memcached 945 memcached 26u IPv4 18588 0t0 TCP *:memcache (LISTEN)
  4. memcached 945 memcached 27u IPv6 18589 0t0 TCP *:memcache (LISTEN)
  5. memcached 945 memcached 28u IPv4 18592 0t0 UDP *:memcache
  6. memcached 945 memcached 29u IPv6 18593 0t0 UDP *:memcache
  7. [root@cache001 ~]# ps -ef|grep memcached|grep -v grep
  8. memcach+ 945 1 0 20:34 ? 00:00:00 /usr/bin/memcached -u memcached -p 11211 -m 64 -c 1024
  1. [root@cache001 ~]# vim /etc/rc.local
  2. [root@cache001 ~]# tail -2 /etc/rc.local
  3. #start up memcached by ylt at 20181012
  4. memcached -m -16m -p 11211 -d -u root -c 8192

3.2 向 Memcached 中写入数据并检查

通过 printf

  1. [root@cache001 ~]# printf "set key1 0 0 3\r\nylt\r\n"|nc 127.0.0.1 11211
  2. STORED
  1. [root@cache001 ~]# printf "get key1\r\n"|nc 127.0.0.1 11211
  2. VALUE key1 0 3
  3. ylt
  4. END
  1. [root@cache001 ~]# printf "delete key1\r\n"|nc 127.0.0.1 11211
  2. DELETED

通过 Telnet

  1. [root@cache001 ~]# yum install telnet -y
  2. [root@cache001 ~]# rpm -qa telnet
  3. telnet-0.17-64.el7.x86_64
  1. [root@cache001 ~]# telnet 127.0.0.1 11211
  2. Trying 127.0.0.1...
  3. Connected to 127.0.0.1.
  4. Escape character is '^]'.
  5. set user01 0 0 4
  6. ylt1
  7. STORED
  8. get user01
  9. VALUE user01 0 4
  10. ylt1
  11. END
  12. delete user01
  13. DELETED
  14. quit
  15. Connection closed by foreign host.

3.3 关闭 Memcached

  1. [root@cache001 ~]# ps -ef|grep memcache|grep -v grep
  2. memcach+ 945 1 0 20:34 ? 00:00:00 /usr/bin/memcached -u memcached -p 11211 -m 64 -c 1024
  3. [root@cache001 ~]# killall memcached pkill memcached
  1. [root@cache001 ~]# memcached -m -16m -p 11211 -d -u root -c 8192 -P /var/run/11211.pid
  2. [root@cache001 ~]# memcached -m -16m -p 11212 -d -u root -c 8192 -P /var/run/11212.pid
  3. [root@cache001 ~]# ps -ef|grep memcache|grep -v grep
  4. root 1781 1 0 20:58 ? 00:00:00 memcached -m -16m -p 11211 -d -u root -c 8192 -P /var/run/11211.pid
  5. root 1788 1 0 20:58 ? 00:00:00 memcached -m -16m -p 11212 -d -u root -c 8192 -P /var/run/11212.pid
  1. [root@cache001 ~]# kill `cat /var/run/11211.pid`
  2. [root@cache001 ~]# lsof -i:11211
  3. [root@cache001 ~]# lsof -i:11212
  4. COMMAND PID USER FD TYPE DEVICE SIZE/OFF NODE NAME
  5. memcached 1788 root 26u IPv4 23322 0t0 TCP *:11212 (LISTEN)
  6. memcached 1788 root 27u IPv6 23323 0t0 TCP *:11212 (LISTEN)
  7. memcached 1788 root 28u IPv4 23326 0t0 UDP *:11212
  8. memcached 1788 root 29u IPv6 23327 0t0 UDP *:11212
  1. ps -ef|grep memcache|grep -v grep|awk '{print $2}'|xargs kill
  2. kill `cat /var/run/11211.pid`
  3. killall memcached
  4. pkill memcached

4. 安装 Memcached 客户端

4.1 Memcached 缓存 PHP 扩展插件安装

  1. [root@cache001 tools]# git clone https://github.com/websupport-sk/pecl-memcache memcache
  2. [root@cache001 tools]# cd memcache/
  3. [root@cache001 memcache]# /application/php/bin/phpize
  4. Configuring for:
  5. PHP Api Version: 20160303
  6. Zend Module Api No: 20160303
  7. Zend Extension Api No: 320160303
  8. [root@cache001 memcache]# ./configure --enable-memcache --with-php-config=/application/php/bin/php-config
  9. [root@cache001 memcache]# make
  10. [root@cache001 memcache]# make install
  11. Installing shared extensions: /application/php-7.1.22/lib/php/extensions/no-debug-non-zts-20160303/
  12. [root@cache001 tools]# ll /application/php/lib/php/extensions/no-debug-non-zts-20160303/
  13. -rwxr-xr-x 1 root root 508496 Oct 12 22:54 memcache.so

4.2 配置 Memcached 客户端,使其生效

  1. [root@cache001 tools]# cd /application/php/lib
  2. [root@cache001 lib]# vim php.ini
  3. [root@cache001 lib]# tail -2 php.ini
  4. extension_dir = "/application/php/lib/php/extensions/no-debug-non-zts-20160303/"
  5. extension=memcache.so

4.3 重启 php fpm 服务使 PHP 的配置修改生效

  1. [root@cache001 ~]# /application/php/sbin/php-fpm -t
  2. [12-Oct-2018 21:00:48] NOTICE: configuration file /application/php-7.1.22/etc/php-fpm.conf test is successful
  1. [root@cache001 ~]# pkill php-fpm
  2. [root@cache001 ~]# ps -ef|grep php-fpm|grep -v grep
  3. [root@cache001 ~]# /application/php/sbin/php-fpm
  4. [root@cache001 ~]# ps -ef|grep php-fpm|grep -v grep
  5. root 1815 1 0 21:00 ? 00:00:00 php-fpm: master process (/application/php-7.1.22/etc/php-fpm.conf)
  6. nginx 1816 1815 0 21:00 ? 00:00:00 php-fpm: pool www
  7. nginx 1817 1815 0 21:00 ? 00:00:00 php-fpm: pool www

Memcache

4.4 编写测试 Memcached 服务的 PHP 脚本

  1. [root@cache001 blog]# cat -n op_mem.php
  2. 1 <?php
  3. 2 $memcache = new Memcache;
  4. 3 $memcache->connect('192.168.2.135', 11211) or die ("Could not connect Mc server");
  5. 4 $memcache->set('key', 'ylt book');
  6. 5 $get= $memcache->get('key');
  7. 6 echo $get;
  8. 7 ?>
  1. [root@cache001 blog]# /application/php/bin/php op_mem.php
  2. ylt book

5 Memcached 应用管理

5.1 通过命令管理 Memcached

通过脚本模拟用户插入及删除数据来监控 Memcached 服务是否正常

  1. [root@cache001 ~]# cat -n mon_mc.sh
  2. 1 #!bin/sh
  3. 2 export MemcachedIp=$1
  4. 3 export MemcachedPort=$2
  5. 4 export NcCmd="nc $MemcachedIp $MemcachedPort"
  6. 5 export MD5=3fe396c01f03425cb5e2da8186eb090d
  7. 6 USAGE(){
  8. 7 echo "$0 MemcachedIp MemcachePort"
  9. 8 exit 3
  10. 9 }
  11. 10 [ $# -ne 2 ] && USAGE
  12. 11 printf "set $MD5 0 0 6\r\noldboy\r\n"|$NcCmd >/dev/null 2>&1
  13. 12 if [ $? -eq 0 ];then
  14. 13 if [ `printf "get $MD5\r\n"|$NcCmd|grep oldboy|wc -l` -eq 1 ];then
  15. 14 echo "Memcached status is ok"
  16. 15 printf "delete $MD5\r\n"|$NcCmd >/dev/null 2>&1
  17. 16 exit 0
  18. 17 else
  19. 18 echo "Memcached status is error1"
  20. 19 exit 2
  21. 20 fi
  22. 21 else
  23. 22 echo "Could not connect Mc server"
  24. 23 exit 2
  25. 24 fi
  1. [root@cache001 ~]# sh mon_mc.sh 127.0.0.1 11211
  2. Memcached status is ok
  1. [root@cache001 ~]# pkill memcached
  2. [root@cache001 ~]# lsof -i:11211
  3. [root@cache001 ~]# sh mon_mc.sh 127.0.0.1 11211
  4. Could not connect Mc server
  1. [root@cache001 ~]# memcached -m -16m -p 11211 -d -u root -c 8192 -P /var/run/11211.pid
  2. [root@cache001 ~]# sh mon_mc.sh 127.0.0.1 11211
  3. Memcached status is ok

通过 nc 命令查看 Memcached 服务的运行状态信息

  1. [root@cache001 ~]# printf "stats\r\n"|nc 127.0.0.1 11211
  2. STAT pid 11025
  3. STAT uptime 110
  4. STAT time 1539360955
  5. STAT version 1.4.15
  6. STAT libevent 2.0.21-stable
  7. STAT pointer_size 64
  8. STAT rusage_user 0.004133
  9. STAT rusage_system 0.008266
  10. STAT curr_connections 10
  11. STAT total_connections 15
  12. STAT connection_structures 11
  13. STAT reserved_fds 20
  14. STAT cmd_get 1
  15. STAT cmd_set 1
  16. STAT cmd_flush 0
  17. STAT cmd_touch 0
  18. STAT get_hits 1
  19. STAT get_misses 0
  20. STAT delete_misses 0
  21. STAT delete_hits 1
  22. STAT incr_misses 0
  23. STAT incr_hits 0
  24. STAT decr_misses 0
  25. STAT decr_hits 0
  26. STAT cas_misses 0
  27. STAT cas_hits 0
  28. STAT cas_badval 0
  29. STAT touch_hits 0
  30. STAT touch_misses 0
  31. STAT auth_cmds 0
  32. STAT auth_errors 0
  33. STAT bytes_read 146
  34. STAT bytes_written 81
  35. STAT limit_maxbytes 18446744073692774400
  36. STAT accepting_conns 1
  37. STAT listen_disabled_num 0
  38. STAT threads 4
  39. STAT conn_yields 0
  40. STAT hash_power_level 16
  41. STAT hash_bytes 524288
  42. STAT hash_is_expanding 0
  43. STAT bytes 0
  44. STAT curr_items 0
  45. STAT total_items 1
  46. STAT expired_unfetched 0
  47. STAT evicted_unfetched 0
  48. STAT evictions 0
  49. STAT reclaimed 0
  50. END
添加新批注
在作者公开此批注前,只有你和作者可见。
回复批注