memcached部署

由于memcached利用libevent作为事件通知机制,所以安装memcached之前要先安装libevent,这里的libevent封装了epoll或kqueue的异步IO,同时memcached有是纯内存的方式存储,所以memcached速度很快。

又因为memcached支持简单协议,telnet或nc可以作为客户端连接memcached

安装memcached所需要的依赖包

yum  install libevent libevent-devel nc  telnet -y

安装memcached

yum install  memcached -y

查看memcached的参数:

[root@mysqlserver ~]# memcached -h
memcached 1.4.4
-p <num>      TCP port number to listen on (default: 11211)
-U <num>      UDP port number to listen on (default: 11211, 0 is off)
-s <file>     UNIX socket path to listen on (disables network support)
-a <mask>     access mask for UNIX socket, in octal (default: 0700)
-l <ip_addr>  interface to listen on (default: INADDR_ANY, all addresses)
-d            run as a daemon
-r            maximize core file limit
-u <username> assume identity of <username> (only when run as root)
-m <num>      max memory to use for items in megabytes (default: 64 MB)
-M            return error on memory exhausted (rather than removing items)
-c <num>      max simultaneous connections (default: 1024)
-k            lock down all paged memory.  Note that there is a
              limit on how much memory you may lock.  Trying to
              allocate more than that would fail, so be sure you
              set the limit correctly for the user you started
              the daemon with (not for -u <username> user;
              under sh this is done with 'ulimit -S -l NUM_KB').
-v            verbose (print errors/warnings while in event loop)
-vv           very verbose (also print client commands/reponses)
-vvv          extremely verbose (also print internal state transitions)
-h            print this help and exit
-i            print memcached and libevent license
-P <file>     save PID in <file>, only used with -d option
-f <factor>   chunk size growth factor (default: 1.25)
-n <bytes>    minimum space allocated for key+value+flags (default: 48)
-L            Try to use large memory pages (if available). Increasing
              the memory page size could reduce the number of TLB misses
              and improve the performance. In order to get large pages
              from the OS, memcached will allocate the total item-cache
              in one large chunk.
-D <char>     Use <char> as the delimiter between key prefixes and IDs.
              This is used for per-prefix stats reporting. The default is
              ":" (colon). If this option is specified, stats collection
              is turned on automatically; if not, then it may be turned on
              by sending the "stats detail on" command to the server.
-t <num>      number of threads to use (default: 4)
-R            Maximum number of requests per event, limits the number of
              requests process for a given connection to prevent 
              starvation (default: 20)
-C            Disable use of CAS
-b            Set the backlog queue limit (default: 1024)
-B            Binding protocol - one of ascii, binary, or auto (default)
-I            Override the size of each slab page. Adjusts max item size
              (default: 1mb, min: 1k, max: 128m)
memcached参数

启动memcached:  

[root@mysqlserver ~]# memcached -m 16m -p 11211 -d  -u root -c 8192

当然memcached也可以跟mysql一样启动多实例,只要改下端口就可以

[root@mysqlserver ~]# memcached -m 16m -p 11212 -d  -u root -c 8192

这时候查看memcached的进程就会有两个memcached进程,这就是开启的两个实例的效果

[root@mysqlserver ~]# ps  -ef |grep mem                            
root        584      2  0 07:06 ?        00:00:01 [vmmemctl]
root       4716      1  0 20:56 ?        00:00:00 memcached -m 16m -p 11211 -d -u root -c 8192
root       4740      1  0 21:02 ?        00:00:00 memcached -m 16m -p 11212 -d -u root -c 8192

telnet连接memcached:   telnet  127.0.0.1 11211

[root@mysqlserver ~]# telnet  127.0.0.1 11211
Trying 127.0.0.1...
Connected to 127.0.0.1.
Escape character is '^]'.
set user01 0 0 7
goser01
STORED
get  user01
VALUE user01 0 7
goser01
END

建议使用nc命令来操作memcached  比如:printf "set  user01 0 0 7\r\ngoser01"|nc 127.0.0.1 11211

可以通过stats来查看memcached的数据状态:

[root@mysqlserver ~]# printf "stats\r\n"|nc 127.0.0.1  11211
STAT pid 4894
STAT uptime 3265
STAT time 1506875210
STAT version 1.4.4
STAT pointer_size 64
STAT rusage_user 0.050992
STAT rusage_system 0.100984
STAT curr_connections 10
STAT total_connections 20
STAT connection_structures 11
STAT cmd_get 6
STAT cmd_set 1
STAT cmd_flush 0
STAT get_hits 5
STAT get_misses 1
STAT delete_misses 0
STAT delete_hits 0
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 auth_cmds 0
STAT auth_errors 0
STAT bytes_read 123
STAT bytes_written 173
STAT limit_maxbytes 67108864
STAT accepting_conns 1
STAT listen_disabled_num 0
STAT threads 4
STAT conn_yields 0
STAT bytes 78
STAT curr_items 1
STAT total_items 1
STAT evictions 0
END
memcached stats

关闭memcached的话,只能用kill杀掉,但如果是多实例的memcached的话,这样kill掉memcached的话就会将所有的实例都关掉了,这时候应该对某个实例的进程号kill掉,不影响其他的实例。比如:

[root@mysqlserver ~]# ps -ef|grep memcached                       
root       4894      1  0 Oct01 ?        00:00:00 memcached -m 64m -p 11211 -u root -c 8192 -d
root       4982      1  0 00:41 ?        00:00:00 memcached -m 16m -d -u root -p 11212 -c 8192

这时候用kill  4894那么4894进程号的实例被杀掉了,但4981的实例还在

[root@mysqlserver ~]# kill 4894
[root@mysqlserver ~]# ps -ef|grep memcached
root       4982      1  0 00:41 ?        00:00:00 memcached -m 16m -d -u root -p 11212 -c 8192

当然还可以在启memcached的实例的时候指定存放进程号的文件,在杀掉这个实例的时候就直接读取这个文件的进程号kill掉即可

[root@mysqlserver ~]# memcached -m 16m -d -u root -p 11213 -c 8192  -P /var/run/11213.pic
[root@mysqlserver ~]# ps -ef|grep memcached                                     root       4982      1  0 00:41 ?        00:00:00 memcached -m 16m -d -u root -p 11212 -c 8192
root       4993      1  0 00:45 ?        00:00:00 memcached -m 16m -d -u root -p 11213 -c 8192 -P /var/run/11213.pic

[root@mysqlserver ~]# kill `cat /var/run/11213.pic` 
[root@mysqlserver ~]# ps -ef|grep memcached        
root       4982      1  0 00:41 ?        00:00:00 memcached -m 16m -d -u root -p 11212 -c 8192

 

memcache客户端的安装及php连接memcached的配置

下载memcache客户端    wget http://pecl.php.net/get/memcache-2.2.7.tgz

[root@lnmp01 tools]# tar xf memcache-2.2.7.tgz 

[root@lnmp01 tools]# cd  memcache-2.2.7

[root@lnmp01 memcache-2.2.7]# /application/php/bin/phpize 

[root@lnmp01 memcache-2.2.7]# ./configure --enable-memcache --with-php-config=/application/php/bin/php-config 

[root@lnmp01 memcache-2.2.7]# make && make install

 接下来php连接memcached的配置,修改php全局配置文件php.ini,在php.ini文件中修改和添加如下内容:

[root@lnmp01 blog]# vim  /application/php/lib/php.ini 

extension_dir = "/application/php/lib/php/extensions/no-debug-non-zts-20090626/"
extension = memcache.so

重启php  

pkill  php-fpm

/application/php/sbin/php-fpm

这是用浏览器访问web的phpinfo.php的时候就会有memcache的模块了

 

然后写个php连接memcached的检测页面

vim  check_mc.php

<?php
	$memcache01 = new Memcache;
	$memcache02 = new Memcache;
	$memcache01->connect('192.168.1.105',11211) or die ('Could not connect memcache');
	$memcache02->connect('192.168.1.105',11212) or die ('Could not connect memcache');
	$memcache01->set('key01','goser001');
	$memcache02->set('key02','goser002');
	$get_value01 = $memcache01->get('key01');
	$get_value02 = $memcache02->get('key02');
	echo $get_value01."<br>";
	echo $get_value02;
?>

这时候再用浏览器访问这个检测页面http://blog.etiantian.org/check_mc.php,返回的结果为:

goser001
goser002

这个结果说明php已经能够成功连接memcached了。。。

 

对memcached做健康检查

用shell编程对memcached做健康检查  check_mc.sh

#!/bin/bash
export  MemcachedIp=$1
export  MemcachedPort=$2
export  NcCmd="nc $MemcachedIp $MemcachedPort"
export	MD5=3fe396c01f03425cb5e2da8186eb090d
USAGE(){
	echo "$0 MemcachedIp  MemcachedPort"
	exit  3
	
}
[ $# -ne 2 ] && USAGE
printf "set $MD5 0 0 7\r\ngoser01\r\n"|$NcCmd>/dev/null 2>&1
if [ $? -eq 0 ];then
	if [ `printf "get $MD5\r\n"|$NcCmd|grep goser01|wc -l` -eq 1 ];then
		echo "Memcached status is ok"
		printf "delete $MD5\r\n"|$NcCmd >/dev/null
		exit 0
	else
		echo  "Memcached status is errors"
		exit 2
	fi
else
	echo "Could  not connect Mc server"
	exit 2
fi

 

集群架构中如何保证session会话共享

客户端在登陆好web服务后,如何能保证用户在登陆的时候保持session不退出,通过一致性哈希虽然能够解决session共享的问题,但是在集群架构中导致节点web分配不均。

为了解决一致性哈希带来的问题,就要再web节点服务后面加上session共享服务,这里就要用到memcached做session共享服务,将客户端的session全部保在memcached的服务中,这样无论客户端通过负载均衡后访问到哪台web服务,它的session会话都会保存在memcached的服务中

处理的方法为在php.ini的全局配置文件中做修改如下:

[root@lnmp01 ~]# vim /application/php/lib/php.ini 

1461 session.save_handler = memcache

1490 session.save_path = "/tcp://192.168.182.144:11211"

然后重启php服务,这样php的session配置就生效了

killall  php-fpm

/application/php/sbin/php-fpm

 

 memcached管理工具memadmin

下载地址:http://www.junopen.com/memadmin/memadmin-1.0.12.tar.gz

这个工具也就是php页面,将其解压到站点目录中即可,然后通过浏览器访问就可以管理memcached了

比如浏览器访问:http://blog.etiantian.org/memadmin/index.php

显示的管理界面为:

 

企业中应用mencached主要做为mysql数据库的缓存和session会话缓存。企业一般会在mencached中预热一部分数据,这样客户端访问的时候直接找mencached,如果memcached里没有数据,那么程序会去访问mysql数据库并返回给客户端,同时程序会将此数据存入memcached缓存中。

这里有个疑问点:memcached如何与mysql同步的呢,客户端访问web为什么先访问mencached,memcached没有数据的时候再找数据库,同时将查找到的数据再存到memcached中的?

posted @ 2017-10-10 21:43  goser  阅读(189)  评论(0)    收藏  举报