memcached启动
1)memcached常用参数:(/usr/local/bin/memcached -h)
-p <num> TCP port number to listen on (default: 11211)
-U <num> UDP port number to listen on (default: 11211, 0 is off)
-l <ip_addr> interface to listen on (default: INADDR_ANY, all addresses)
-d run as a daemon
-u <username> assume identity of <username> (only when run as root)
-m <num> max memory to use for items in megabytes (default: 64 MB)
-c <num> max simultaneous connections (default: 1024)
-P <file> save PID in <file>, only used with -d option
-f <factor> chunk size growth factor (default: 1.25)
2)以daemon的方式启动memcached
/usr/local/bin/memcached -u nobody -p 11211 -m 64 -c 128 -d
——————————————————————————————————————————
3.memcached结合mysql、perl的实践
cat memcached_mysql.pl
#!/usr/bin/perl
#Usage: test memcahed with mysql and perl
#Made by yunhaozou@gmail.com,2010/12/20
use Cache::Memcached;
use DBI;
use Data::Dumper;
use Digest::MD5 qw(md5_hex);
use strict;
my $host = “127.0.0.1″; #db相关信息
my $port = “3306″;
my $db = “zichan_db”;
my $user = “root”;
my $password = “”;
my $dsn = “dbi:mysql:database=$db:hostname=$host:port=$port”;
my $dbh = DBI->connect($dsn,$user,$password) or die “Couldn’t connect to database: ” . DBI->errstr; #连接db
my $memcached = Cache::Memcached->new({ servers=>["127.0.0.1:11211"],compress_threshold=>10_000}); #连接memcached
#$memcached = Cache::Memcached->new({ servers=>["127.0.0.1:11211","192.168.2.1:11211"],compress_threshold=>10_000}); #表示连接两台机的memcached,这样可以实现负载均衡,一个机器挂了,另一个可以继续用
my $zcsql = “select sn,model,cab_no,func,intra_ip,man_addr,contact from equipment where sn=’0917QAR012′;”;
my $result = query_result($zcsql);
print Dumper $result;
$dbh->disconnect();
sub query_result{
my $query_sql = shift;
my $query_key = md5_hex($query_sql); #把查询sql语句转换为MD5值的key
my $query_result = $memcached->get($query_key);
return $query_result if $query_result; #当在memcaed有缓存时直接返回
$query_result = $dbh->selectrow_hashref($query_sql);
$memcached->set(“$query_key”, $query_result); #当在memcached没有缓存时从数据查询,并缓存memcached
return $query_result;
——————————————————————————————————————————
4.日常运维
1)memcached是典型的是耗内存不耗CPU。而通常web进程(如Apache)是CPU敏感,内存不那么敏感的。因此可将memcached直接跑在前台web主机上,作为数据缓存(或数据库连接池)使用,也就是在web程序与数据库之间多加了一层。
2)尽量使用memcached分布式部署。但各个模块尽量做到memcached资源隔离(之前因为社区前端公用memcached,有一次广东综合通信上线了一个页面,因工程师写代码时失误,显式调用了flush memcache,从而每次访问都清空整个memcached,把整个网站社区前端memcached资源清空,而且过几秒又被清空,这样造成的后果有多严重就不说了,写那代码的哥们有多惨也不说了~~)
3)用telnet测试memcached
STAT total_connections 11 //表示从memcached服务启动到当前时间,系统打开过的连接的总数。
STAT connection_structures 11
STAT cmd_get 0 //查询缓存的次数,平均每秒缓存次数cmd_get/uptime
STAT cmd_set 0 //设置key=>value的次数
STAT cmd_flush 0
STAT get_hits 0 //缓存命中的次数,缓存命中率=get_hits/cmd_get*100%
STAT get_misses 0 //cmd_get-get_hits
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 7 //memcached服务器从网络读取的总的字节数
STAT bytes_written 0 //memcached服务器发送到网络的总的字节数。
STAT limit_maxbytes 67108864 //memcached服务缓存允许使用的最大字节数
STAT accepting_conns 1
STAT listen_disabled_num 0
STAT threads 4
STAT conn_yields 0
STAT bytes 0
STAT curr_items 0
STAT total_items 0
STAT evictions 0
STAT reclaimed 0
——————————————————————————————————————————
5.监控
1)nagios监控
a. check_memcached
先要安装check_memcached插件
wget http://search.cpan.org/CPAN/authors/id/Z/ZI/ZIGOROU/Nagios-Plugins-Memcached-0.02.tar.gz
tar xzvf Nagios-Plugins-Memcached-0.02.tar.gz
cd Nagios-Plugins-Memcached-0.02
perl Makefile.PL (可能需要其他perl的modules,依照提示通过cpan安装即可)
make && make install
check_memcached [-H host:port] [-w warnings] [-c critical] [--size-warnng size-warnng] [--size-critical size-critical] [--hit-warning hit-warning] [--hit-critical
hit-critical] [-t timeout]
b. /check_tcp -H host -p 11211 -t 5 -E -s ‘stats/r/nquit/r/n’ -e ‘uptime’ -M crit
2)cacti监控
一台服务器起多个memcached,多端口cacti部署监控。
a. 需要一个 Python memcached Client API
wget ftp://ftp.tummy.com/pub/python-memcached/python-memcached-1.45.tar.gz
tar xzvf python-memcached-1.45.tar.gz
cd python-memcached-1.45
python setup.py install
b. 加模板memcached multiport
wget http://tag1consulting.com/blog/cacti-memcache-multi-port-templates
cp memcached.py <cacti install目录>/scripts/memcachedmultiport.py
c. 增加监控时devices->des && ip->host template选择memcachedmultiport->Downed Device Detection选择none->SNMP Version选择not in use->create graphs->Port to query for memcached statistics填写memcached端口。
3)也可以自定义nagios、cacti监控,通过memcached命令stats取相关数据。
该贴由system转至本版2014-9-9 23:18:03