@yanglt7
2018-10-21T15:58:12.000000Z
字数 9556
阅读 686
Web集群实战
协议简单。采用的是基于文本行的协议,能通过 telnet/nc 等命令直接操作 Memcached 服务存取数据。
支持 epoll/kqueue 异步 I/O 模型,使用 libevent 作为时间处理通知机制。
采用 key/value 键值数据类型。
全内存缓存,效率高。
可支持分布式集群
[root@cache001 ~]# cat /etc/redhat-release
CentOS Linux release 7.4.1708 (Core)
[root@cache001 ~]# uname -m
x86_64
[root@cache001 ~]# uname -r
3.10.0-693.el7.x86_64
[root@cache001 ~]# yum install -y libevent libevent-devel nc
[root@cache001 ~]# rpm -qa libevent libevent-devel nc
libevent-devel-2.0.21-4.el7.x86_64
libevent-2.0.21-4.el7.x86_64
[root@cache001 ~]# yum install -y memcached
[root@cache001 ~]# rpm -qa memcached
memcached-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 :11211
COMMAND PID USER FD TYPE DEVICE SIZE/OFF NODE NAME
memcached 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 *:memcache
memcached 945 memcached 29u IPv6 18593 0t0 UDP *:memcache
[root@cache001 ~]# ps -ef|grep memcached|grep -v grep
memcach+ 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 20181012
memcached -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 11211
STORED
[root@cache001 ~]# printf "get key1\r\n"|nc 127.0.0.1 11211
VALUE key1 0 3
ylt
END
[root@cache001 ~]# printf "delete key1\r\n"|nc 127.0.0.1 11211
DELETED
[root@cache001 ~]# yum install telnet -y
[root@cache001 ~]# rpm -qa telnet
telnet-0.17-64.el7.x86_64
[root@cache001 ~]# telnet 127.0.0.1 11211
Trying 127.0.0.1...
Connected to 127.0.0.1.
Escape character is '^]'.
set user01 0 0 4
ylt1
STORED
get user01
VALUE user01 0 4
ylt1
END
delete user01
DELETED
quit
Connection closed by foreign host.
[root@cache001 ~]# ps -ef|grep memcache|grep -v grep
memcach+ 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 grep
root 1781 1 0 20:58 ? 00:00:00 memcached -m -16m -p 11211 -d -u root -c 8192 -P /var/run/11211.pid
root 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:11212
COMMAND PID USER FD TYPE DEVICE SIZE/OFF NODE NAME
memcached 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 *:11212
memcached 1788 root 29u IPv6 23327 0t0 UDP *:11212
ps -ef|grep memcache|grep -v grep|awk '{print $2}'|xargs kill
kill `cat /var/run/11211.pid`
killall memcached
pkill 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/phpize
Configuring for:
PHP Api Version: 20160303
Zend Module Api No: 20160303
Zend 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 install
Installing 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.ini
extension_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 grep
root 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 www
nginx 1817 1815 0 21:00 ? 00:00:00 php-fpm: pool www
[root@cache001 blog]# cat -n op_mem.php
1 <?php
2 $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.php
ylt book
[root@cache001 ~]# cat -n mon_mc.sh
1 #!bin/sh
2 export MemcachedIp=$1
3 export MemcachedPort=$2
4 export NcCmd="nc $MemcachedIp $MemcachedPort"
5 export MD5=3fe396c01f03425cb5e2da8186eb090d
6 USAGE(){
7 echo "$0 MemcachedIp MemcachePort"
8 exit 3
9 }
10 [ $# -ne 2 ] && USAGE
11 printf "set $MD5 0 0 6\r\noldboy\r\n"|$NcCmd >/dev/null 2>&1
12 if [ $? -eq 0 ];then
13 if [ `printf "get $MD5\r\n"|$NcCmd|grep oldboy|wc -l` -eq 1 ];then
14 echo "Memcached status is ok"
15 printf "delete $MD5\r\n"|$NcCmd >/dev/null 2>&1
16 exit 0
17 else
18 echo "Memcached status is error1"
19 exit 2
20 fi
21 else
22 echo "Could not connect Mc server"
23 exit 2
24 fi
[root@cache001 ~]# sh mon_mc.sh 127.0.0.1 11211
Memcached status is ok
[root@cache001 ~]# pkill memcached
[root@cache001 ~]# lsof -i:11211
[root@cache001 ~]# sh mon_mc.sh 127.0.0.1 11211
Could 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 11211
Memcached status is ok
[root@cache001 ~]# printf "stats\r\n"|nc 127.0.0.1 11211
STAT pid 11025
STAT uptime 110
STAT time 1539360955
STAT version 1.4.15
STAT libevent 2.0.21-stable
STAT pointer_size 64
STAT rusage_user 0.004133
STAT rusage_system 0.008266
STAT curr_connections 10
STAT total_connections 15
STAT connection_structures 11
STAT reserved_fds 20
STAT cmd_get 1
STAT cmd_set 1
STAT cmd_flush 0
STAT cmd_touch 0
STAT get_hits 1
STAT get_misses 0
STAT delete_misses 0
STAT delete_hits 1
STAT incr_misses 0
STAT incr_hits 0
STAT decr_misses 0
STAT decr_hits 0
STAT cas_misses 0
STAT cas_hits 0
STAT cas_badval 0
STAT touch_hits 0
STAT touch_misses 0
STAT auth_cmds 0
STAT auth_errors 0
STAT bytes_read 146
STAT bytes_written 81
STAT limit_maxbytes 18446744073692774400
STAT accepting_conns 1
STAT listen_disabled_num 0
STAT threads 4
STAT conn_yields 0
STAT hash_power_level 16
STAT hash_bytes 524288
STAT hash_is_expanding 0
STAT bytes 0
STAT curr_items 0
STAT total_items 1
STAT expired_unfetched 0
STAT evicted_unfetched 0
STAT evictions 0
STAT reclaimed 0
END