@yanglt7
2018-10-21T15:58:12.000000Z
字数 9556
阅读 759
Web集群实战
协议简单。采用的是基于文本行的协议,能通过 telnet/nc 等命令直接操作 Memcached 服务存取数据。
支持 epoll/kqueue 异步 I/O 模型,使用 libevent 作为时间处理通知机制。
采用 key/value 键值数据类型。
全内存缓存,效率高。
可支持分布式集群
[root@cache001 ~]# cat /etc/redhat-releaseCentOS Linux release 7.4.1708 (Core)[root@cache001 ~]# uname -mx86_64[root@cache001 ~]# uname -r3.10.0-693.el7.x86_64
[root@cache001 ~]# yum install -y libevent libevent-devel nc[root@cache001 ~]# rpm -qa libevent libevent-devel nclibevent-devel-2.0.21-4.el7.x86_64libevent-2.0.21-4.el7.x86_64
[root@cache001 ~]# yum install -y memcached[root@cache001 ~]# rpm -qa memcachedmemcached-1.4.15-10.el7_3.1.x86_64
[root@cache001 ~]# which memcached/bin/memcached[root@cache001 ~]# memcached -m -16m -p 11211 -d -u root -c 8192
[root@cache001 ~]# lsof -i :11211COMMAND PID USER FD TYPE DEVICE SIZE/OFF NODE NAMEmemcached 945 memcached 26u IPv4 18588 0t0 TCP *:memcache (LISTEN)memcached 945 memcached 27u IPv6 18589 0t0 TCP *:memcache (LISTEN)memcached 945 memcached 28u IPv4 18592 0t0 UDP *:memcachememcached 945 memcached 29u IPv6 18593 0t0 UDP *:memcache[root@cache001 ~]# ps -ef|grep memcached|grep -v grepmemcach+ 945 1 0 20:34 ? 00:00:00 /usr/bin/memcached -u memcached -p 11211 -m 64 -c 1024
[root@cache001 ~]# vim /etc/rc.local[root@cache001 ~]# tail -2 /etc/rc.local#start up memcached by ylt at 20181012memcached -m -16m -p 11211 -d -u root -c 8192
[root@cache001 ~]# printf "set key1 0 0 3\r\nylt\r\n"|nc 127.0.0.1 11211STORED
[root@cache001 ~]# printf "get key1\r\n"|nc 127.0.0.1 11211VALUE key1 0 3yltEND
[root@cache001 ~]# printf "delete key1\r\n"|nc 127.0.0.1 11211DELETED
[root@cache001 ~]# yum install telnet -y[root@cache001 ~]# rpm -qa telnettelnet-0.17-64.el7.x86_64
[root@cache001 ~]# telnet 127.0.0.1 11211Trying 127.0.0.1...Connected to 127.0.0.1.Escape character is '^]'.set user01 0 0 4ylt1STOREDget user01VALUE user01 0 4ylt1ENDdelete user01DELETEDquitConnection closed by foreign host.
[root@cache001 ~]# ps -ef|grep memcache|grep -v grepmemcach+ 945 1 0 20:34 ? 00:00:00 /usr/bin/memcached -u memcached -p 11211 -m 64 -c 1024[root@cache001 ~]# killall memcached 或 pkill memcached
[root@cache001 ~]# memcached -m -16m -p 11211 -d -u root -c 8192 -P /var/run/11211.pid[root@cache001 ~]# memcached -m -16m -p 11212 -d -u root -c 8192 -P /var/run/11212.pid[root@cache001 ~]# ps -ef|grep memcache|grep -v greproot 1781 1 0 20:58 ? 00:00:00 memcached -m -16m -p 11211 -d -u root -c 8192 -P /var/run/11211.pidroot 1788 1 0 20:58 ? 00:00:00 memcached -m -16m -p 11212 -d -u root -c 8192 -P /var/run/11212.pid
[root@cache001 ~]# kill `cat /var/run/11211.pid`[root@cache001 ~]# lsof -i:11211[root@cache001 ~]# lsof -i:11212COMMAND PID USER FD TYPE DEVICE SIZE/OFF NODE NAMEmemcached 1788 root 26u IPv4 23322 0t0 TCP *:11212 (LISTEN)memcached 1788 root 27u IPv6 23323 0t0 TCP *:11212 (LISTEN)memcached 1788 root 28u IPv4 23326 0t0 UDP *:11212memcached 1788 root 29u IPv6 23327 0t0 UDP *:11212
ps -ef|grep memcache|grep -v grep|awk '{print $2}'|xargs killkill `cat /var/run/11211.pid`killall memcachedpkill memcached
[root@cache001 tools]# git clone https://github.com/websupport-sk/pecl-memcache memcache[root@cache001 tools]# cd memcache/[root@cache001 memcache]# /application/php/bin/phpizeConfiguring for:PHP Api Version: 20160303Zend Module Api No: 20160303Zend Extension Api No: 320160303[root@cache001 memcache]# ./configure --enable-memcache --with-php-config=/application/php/bin/php-config[root@cache001 memcache]# make[root@cache001 memcache]# make installInstalling shared extensions: /application/php-7.1.22/lib/php/extensions/no-debug-non-zts-20160303/[root@cache001 tools]# ll /application/php/lib/php/extensions/no-debug-non-zts-20160303/-rwxr-xr-x 1 root root 508496 Oct 12 22:54 memcache.so
[root@cache001 tools]# cd /application/php/lib[root@cache001 lib]# vim php.ini[root@cache001 lib]# tail -2 php.iniextension_dir = "/application/php/lib/php/extensions/no-debug-non-zts-20160303/"extension=memcache.so
[root@cache001 ~]# /application/php/sbin/php-fpm -t[12-Oct-2018 21:00:48] NOTICE: configuration file /application/php-7.1.22/etc/php-fpm.conf test is successful
[root@cache001 ~]# pkill php-fpm[root@cache001 ~]# ps -ef|grep php-fpm|grep -v grep[root@cache001 ~]# /application/php/sbin/php-fpm[root@cache001 ~]# ps -ef|grep php-fpm|grep -v greproot 1815 1 0 21:00 ? 00:00:00 php-fpm: master process (/application/php-7.1.22/etc/php-fpm.conf)nginx 1816 1815 0 21:00 ? 00:00:00 php-fpm: pool wwwnginx 1817 1815 0 21:00 ? 00:00:00 php-fpm: pool www

[root@cache001 blog]# cat -n op_mem.php1 <?php2 $memcache = new Memcache;3 $memcache->connect('192.168.2.135', 11211) or die ("Could not connect Mc server");4 $memcache->set('key', 'ylt book');5 $get= $memcache->get('key');6 echo $get;7 ?>
[root@cache001 blog]# /application/php/bin/php op_mem.phpylt book
[root@cache001 ~]# cat -n mon_mc.sh1 #!bin/sh2 export MemcachedIp=$13 export MemcachedPort=$24 export NcCmd="nc $MemcachedIp $MemcachedPort"5 export MD5=3fe396c01f03425cb5e2da8186eb090d6 USAGE(){7 echo "$0 MemcachedIp MemcachePort"8 exit 39 }10 [ $# -ne 2 ] && USAGE11 printf "set $MD5 0 0 6\r\noldboy\r\n"|$NcCmd >/dev/null 2>&112 if [ $? -eq 0 ];then13 if [ `printf "get $MD5\r\n"|$NcCmd|grep oldboy|wc -l` -eq 1 ];then14 echo "Memcached status is ok"15 printf "delete $MD5\r\n"|$NcCmd >/dev/null 2>&116 exit 017 else18 echo "Memcached status is error1"19 exit 220 fi21 else22 echo "Could not connect Mc server"23 exit 224 fi
[root@cache001 ~]# sh mon_mc.sh 127.0.0.1 11211Memcached status is ok
[root@cache001 ~]# pkill memcached[root@cache001 ~]# lsof -i:11211[root@cache001 ~]# sh mon_mc.sh 127.0.0.1 11211Could not connect Mc server
[root@cache001 ~]# memcached -m -16m -p 11211 -d -u root -c 8192 -P /var/run/11211.pid[root@cache001 ~]# sh mon_mc.sh 127.0.0.1 11211Memcached status is ok
[root@cache001 ~]# printf "stats\r\n"|nc 127.0.0.1 11211STAT pid 11025STAT uptime 110STAT time 1539360955STAT version 1.4.15STAT libevent 2.0.21-stableSTAT pointer_size 64STAT rusage_user 0.004133STAT rusage_system 0.008266STAT curr_connections 10STAT total_connections 15STAT connection_structures 11STAT reserved_fds 20STAT cmd_get 1STAT cmd_set 1STAT cmd_flush 0STAT cmd_touch 0STAT get_hits 1STAT get_misses 0STAT delete_misses 0STAT delete_hits 1STAT incr_misses 0STAT incr_hits 0STAT decr_misses 0STAT decr_hits 0STAT cas_misses 0STAT cas_hits 0STAT cas_badval 0STAT touch_hits 0STAT touch_misses 0STAT auth_cmds 0STAT auth_errors 0STAT bytes_read 146STAT bytes_written 81STAT limit_maxbytes 18446744073692774400STAT accepting_conns 1STAT listen_disabled_num 0STAT threads 4STAT conn_yields 0STAT hash_power_level 16STAT hash_bytes 524288STAT hash_is_expanding 0STAT bytes 0STAT curr_items 0STAT total_items 1STAT expired_unfetched 0STAT evicted_unfetched 0STAT evictions 0STAT reclaimed 0END