Memcached笔记——(一)安装&常规错误&监控
08年的时候接触过Memcached,当时还对它的客户端产品嗤之以鼻,毕竟手工代码没有各种ORM原生XML配置方便。尽管如此,Memcached现在已经成了服务器架构里不可或缺的一部分!
相关链接:
Memcached笔记——(一)安装&常规错误&监控
Memcached笔记——(二)XMemcached&Spring集成
Memcached笔记——(三)Memcached使用总结
Memcached笔记——(四)应对高并发攻击
一、下载
1.Libevent
简单的说就是一个事件触发的网络库,Memcached离不开它。
- wget http://cloud.github.com/downloads/libevent/libevent/libevent-2.0.17-stable.tar.gz
wget http://cloud.github.com/downloads/libevent/libevent/libevent-2.0.17-stable.tar.gz
2.Memcached
今天的主角
- wget http://memcached.googlecode.com/files/memcached-1.4.13.tar.gz
wget http://memcached.googlecode.com/files/memcached-1.4.13.tar.gz
二、安装
1.Libevent
解压缩
- tar zxvf libevent-2.0.17-stable.tar.gz
tar zxvf libevent-2.0.17-stable.tar.gz
编译、安装
- ./configure --prefix=/usr && make && make install
./configure --prefix=/usr && make && make install
这里一定要注意指定--prefix,后面配置memcached的时候就有必要用到。
2.Memcached
解压
- tar zxvf memcached-1.4.13.tar.gz
tar zxvf memcached-1.4.13.tar.gz
编译、安装
- ./configure --with-libevent=/usr/lib && make && make install
./configure --with-libevent=/usr/lib && make && make install
这里一定要指定libevent的路径,否则启动的时候就有找不到libevent的so文件的错误!
启动
- memcached -d -m 512 -p
11211 -u root -c
256 -P /var/run/memcached.pid
memcached -d -m 512 -p 11211 -u root -c 256 -P /var/run/memcached.pid
参数
-p <num> TCP port number to listen on (default: 11211)
-U <num> UDP port number to listen on (default: 11211, 0 is off)
-l <addr> interface to listen on (default: INADDR_ANY, all addresses)
<addr> may be specified as host:port. If you don't specify
a port number, the value you specified with -p or -U is
used. You may specify multiple addresses separated by comma
or by using -l multiple times
-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)
-M return error on memory exhausted (rather than removing items)
-c <num> max simultaneous connections (default: 1024)
-v verbose (print errors/warnings while in event loop)
-P <file> save PID in <file>, only used with -d option
要关掉memcached
- kill -9 `cat /var/run/memcached.pid`
kill -9 `cat /var/run/memcached.pid`
是否正常?Telnet上去看看
- telnet xxx.xxx.xxx.xxx 11211
telnet xxx.xxx.xxx.xxx 11211
然后输入
- stats
stats
接着就能看到:
STAT pid 3021
STAT uptime 3621
STAT time 1331261509
STAT version 1.4.13
STAT libevent 2.0.17-stable
STAT pointer_size 64
STAT rusage_user 0.000000
STAT rusage_system 0.000999
STAT curr_connections 6
STAT total_connections 7
STAT connection_structures 7
STAT reserved_fds 20
STAT cmd_get 0
STAT cmd_set 0
STAT cmd_flush 0
STAT cmd_touch 0
STAT get_hits 0
STAT get_misses 0
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 touch_hits 0
STAT touch_misses 0
STAT auth_cmds 0
STAT auth_errors 0
STAT bytes_read 72
STAT bytes_written 1038
STAT limit_maxbytes 52428800
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 expired_unfetched 0
STAT evicted_unfetched 0
STAT bytes 0
STAT curr_items 0
STAT total_items 0
STAT evictions 0
STAT reclaimed 0
END
上面状况说明Memcached服务正常。
还可以试试get、set、delete、replace
bar (数据)
STORED (结果)
get foo (取得命令)
VALUE foo 0 3 (数据)
bar (数据)
输入
- quit
quit
退出。
三、系统服务
参照Nginx的系统服务,自己写了一个Memcached的系统服务脚本。
先构建/etc/init.d/memcahed这个文件,然后赋予其可执行权限:
- touch /etc/init.d/memcached
- chmod +x /etc/init.d/memcached
touch /etc/init.d/memcached
chmod +x /etc/init.d/memcached
memcached脚本如下:
- #!/bin/bash
- # v.0.0.1
- # create by snowolf at 2012.5.25
- #
- # memcached - This shell script takes care of starting and stopping memcached.
- #
- # chkconfig: - 90
10 - # description: Memcache provides fast memory based storage.
- # processname: memcached
- memcached_path="/usr/local/bin/memcached"
- memcached_pid="/var/run/memcached.pid"
- memcached_memory="1024"
- # Source function library.
- . /etc/rc.d/init.d/functions
- [ -x $memcached_path ] || exit 0
- RETVAL=0
- prog="memcached"
- # Start daemons.
- start() {
- if [ -e $memcached_pid -a ! -z $memcached_pid ];then
- echo $prog" already running...."
- exit 1
- fi
- echo -n $"Starting $prog "
- # Single instance for all caches
- $memcached_path -m $memcached_memory -l 0.0.0.0 -p
11211 -u root -d -P $memcached_pid - RETVAL=$?
- [ $RETVAL -eq 0 ] && {
- touch /var/lock/subsys/$prog
- success $"$prog"
- }
- echo
- return $RETVAL
- }
- # Stop daemons.
- stop() {
- echo -n $"Stopping $prog "
- killproc -d 10 $memcached_path
- echo
- [ $RETVAL = 0 ] && rm -f $memcached_pid /var/lock/subsys/$prog
- RETVAL=$?
- return $RETVAL
- }
- # See how we were called.
- case "$1" in
- start)
- start
- ;;
- stop)
- stop
- ;;
- status)
- status $prog
- RETVAL=$?
- ;;
- restart)
- stop
- start
- ;;
- *)
- echo $"Usage: $0 {start|stop|status|restart}"
- exit 1
- esac
- exit $RETVAL
#!/bin/bash
# v.0.0.1
# create by snowolf at 2012.5.25
#
# memcached - This shell script takes care of starting and stopping memcached.
#
# chkconfig: - 90 10
# description: Memcache provides fast memory based storage.
# processname: memcached memcached_path="/usr/local/bin/memcached"
memcached_pid="/var/run/memcached.pid"
memcached_memory="1024" # Source function library.
. /etc/rc.d/init.d/functions [ -x $memcached_path ] || exit 0 RETVAL=0
prog="memcached" # Start daemons.
start() {
if [ -e $memcached_pid -a ! -z $memcached_pid ];then
echo $prog" already running...."
exit 1
fi echo -n $"Starting $prog "
# Single instance for all caches
$memcached_path -m $memcached_memory -l 0.0.0.0 -p 11211 -u root -d -P $memcached_pid
RETVAL=$?
[ $RETVAL -eq 0 ] && {
touch /var/lock/subsys/$prog
success $"$prog"
}
echo
return $RETVAL
} # Stop daemons.
stop() {
echo -n $"Stopping $prog "
killproc -d 10 $memcached_path
echo
[ $RETVAL = 0 ] && rm -f $memcached_pid /var/lock/subsys/$prog RETVAL=$?
return $RETVAL
} # See how we were called.
case "$1" in
start)
start
;;
stop)
stop
;;
status)
status $prog
RETVAL=$?
;;
restart)
stop
start
;;
*)
echo $"Usage: $0 {start|stop|status|restart}"
exit 1
esac
exit $RETVAL
注意这几行配置,请根据实际情况配置memcached执行文件路径,以及Memcached使用内存大小:
memcached_memory="1024"
追加为系统服务:
- chkconfig --add memcached
- chkconfig memcached on
chkconfig --add memcached
chkconfig memcached on
然后就可以通过service memcached start|stop|status|restart控制memcached了!
四、常规错误
一开始没有指定libevent路径安装memcached的时候,启动memcached就报这个错误:
其实这个文件就在/usr/lib下。错就错在Linux是64bit系统,如果没有指定libevent路径,memcached就会去/usr/lib64下去找。
找到这个文件
- whereis libevent-2.0.so.5
whereis libevent-2.0.so.5
- ldd /usr/local/bin/memcached
ldd /usr/local/bin/memcached
提示找不到libevent-2.0.so.5
libevent-2.0.so.5 => not found
librt.so.1 => /lib64/librt.so.1 (0x0000003c94a00000)
libpthread.so.0 => /lib64/libpthread.so.0 (0x0000003c93a00000)
libc.so.6 => /lib64/libc.so.6 (0x0000003c92e00000)
/lib64/ld-linux-x86-64.so.2 (0x0000003c92a00000)
定位
- LD_DEBUG=libs /usr/local/bin/memcached -v
LD_DEBUG=libs /usr/local/bin/memcached -v
19905: search path=/usr/lib/lib/tls/x86_64:/usr/lib/lib/tls:/usr/lib/lib/x86_64:/usr/lib/lib (RPATH from file /usr/local/bin/memcached)
19905: trying file=/usr/lib/lib/tls/x86_64/libevent-2.0.so.5
19905: trying file=/usr/lib/lib/tls/libevent-2.0.so.5
19905: trying file=/usr/lib/lib/x86_64/libevent-2.0.so.5
19905: trying file=/usr/lib/lib/libevent-2.0.so.5
19905: search cache=/etc/ld.so.cache
19905: search path=/lib64/tls/x86_64:/lib64/tls:/lib64/x86_64:/lib64:/usr/lib64/tls/x86_64:/usr/lib64/tls:/usr/lib64/x86_64:/usr/lib64 (system search path)
19905: trying file=/lib64/tls/x86_64/libevent-2.0.so.5
19905: trying file=/lib64/tls/libevent-2.0.so.5
19905: trying file=/lib64/x86_64/libevent-2.0.so.5
19905: trying file=/lib64/libevent-2.0.so.5
19905: trying file=/usr/lib64/tls/x86_64/libevent-2.0.so.5
19905: trying file=/usr/lib64/tls/libevent-2.0.so.5
19905: trying file=/usr/lib64/x86_64/libevent-2.0.so.5
19905: trying file=/usr/lib64/libevent-2.0.so.5
19905:
/usr/local/bin/memcached: error while loading shared libraries: libevent-2.0.so.5: cannot open shared object file: No such file or directory
注意这句:
做个软连接
- ln -s /usr/lib/libevent-2.0.so.5 /usr/lib64/libevent-2.0.so.5
ln -s /usr/lib/libevent-2.0.so.5 /usr/lib64/libevent-2.0.so.5
再试试:
- ldd /usr/local/bin/memcached
ldd /usr/local/bin/memcached
这回找到了!
libevent-2.0.so.5 => /usr/lib64/libevent-2.0.so.5 (0x00002b5608a26000)
librt.so.1 => /lib64/librt.so.1 (0x0000003c94a00000)
libpthread.so.0 => /lib64/libpthread.so.0 (0x0000003c93a00000)
libc.so.6 => /lib64/libc.so.6 (0x0000003c92e00000)
/lib64/ld-linux-x86-64.so.2 (0x0000003c92a00000)
五、监控
可以在服务器上配置一个PHP页面来监测Memcached的情况,
下载MemcachePHP
配置也比较简单,主要包括账户配置,以及Memcached Server地址配置。
- define('ADMIN_USERNAME','memcache');
// Admin Username - define('ADMIN_PASSWORD','password');
// Admin Password - define('DATE_FORMAT','Y/m/d H:i:s');
- define('GRAPH_SIZE',200);
- define('MAX_ITEM_DUMP',50);
- $MEMCACHE_SERVERS[] = 'mymemcache-server1:11211';
// add more as an array - $MEMCACHE_SERVERS[] = 'mymemcache-server2:11211';
// add more as an array
define('ADMIN_USERNAME','memcache'); // Admin Username
define('ADMIN_PASSWORD','password'); // Admin Password
define('DATE_FORMAT','Y/m/d H:i:s');
define('GRAPH_SIZE',200);
define('MAX_ITEM_DUMP',50); $MEMCACHE_SERVERS[] = 'mymemcache-server1:11211'; // add more as an array
$MEMCACHE_SERVERS[] = 'mymemcache-server2:11211'; // add more as an array
常见错误:
this warning, you most likely misspelled the timezone identifier. We selected the timezone 'UTC' for now, but please set date.timezone to select your timezone. in /home/usr/local/apache.../memcache.php on line 726
在memcache.php顶端加上“date_default_timezone_set('Asia/Hong_Kong');”即可,具体地域设置参考:http://www.php.net/manual/zh/datetime.configuration.php#ini.date.timezone
如果不方便搭建PHP服务,可以使用Perl脚本memcache-top
修改@default_instances或使用--instances参数:
- perl memcache-top-v0.6 --instances
10.11.155.26
10.11.155.41
perl memcache-top-v0.6 --instances 10.11.155.26 10.11.155.41
先到这里,后续做压力测试,Java客户端开发,Spring系统集成等。
相关链接:
Memcached笔记——(一)安装&常规错误&监控
Memcached笔记——(二)XMemcached&Spring集成
Memcached笔记——(三)Memcached使用总结
Memcached笔记——(四)应对高并发攻击
Memcached笔记——(一)安装&常规错误&监控的更多相关文章
- Memcached理解笔记1---安装&常规错误&监控
一.下载 1.Libevent 简单的说就是一个事件触发的网络库,Memcached离不开它. wget http://cloud.github.com/downloads/libevent/libe ...
- Memcached笔记——(四)应对高并发攻击【转】
http://snowolf.iteye.com/blog/1677495 近半个月过得很痛苦,主要是产品上线后,引来无数机器用户恶意攻击,不停的刷新产品各个服务入口,制造垃圾数据,消耗资源.他们的最 ...
- Memcached笔记——(四)应对高并发攻击
近半个月过得很痛苦,主要是产品上线后,引来无数机器用户恶意攻击,不停的刷新产品各个服务入口,制造垃圾数据,消耗资源.他们的最好成绩,1秒钟可以并发6次,赶在Database入库前,Cache进行Mis ...
- Memcached笔记——(三)Memcached使用总结
为了将N个前端数据同步,通过Memcached完成数据打通,但带来了一些新问题: 使用iBatis整合了Memcached,iBatis针对每台server生成了唯一标识,导致同一份数据sql会产生不 ...
- Memcached笔记——(二)XMemcached&Spring集成
今天研究Memcached的Java的Client,使用XMemcached 1.3.5,做个简单的测试,并介绍如何与Spring集成. 相关链接: Memcached笔记--(一)安装&常规 ...
- Windows Server 2008安装Memcached笔记
分布式缓存系统Memcached简介与实践 缘起: 在数据驱动的web开发中,经常要重复从数据库中取出相同的数据,这种重复极大的增加了数据库负载.缓存是解决这个问题的好办法.但是ASP.NET中的虽然 ...
- LNMP笔记:安装 Xcache 缓存扩展,降低服务器负载
LNMP笔记:安装 Xcache 缓存扩展,降低服务器负载 2014/11/27 教程笔记 4,743 14 WordPress 精品主机推荐:恒创主机 | 阿里云(本站目前所用云主机) 倡萌 ...
- Memcached和Memcache安装(64位win7)
一.Memcached和Memcache的区别: 网上关于Memcached和Memcache的区别的理解众说纷纭,我个人的理解是: Memcached是一个内存缓存系统,而Memcache是php的 ...
- memcache与memcached介绍及安装配置
也许大家一看到Memcache和Memcached会有点晕,这两者有什么关系又有什么区别呢,下面先给大家说下Memcached,Memcached是一个高性能的分布式内存对象缓存系统,用于动态Web应 ...
随机推荐
- nginx中rewrite flag
rewrite 正则表达式 新URI [flag]; [flag] 选项用于调控重写的行为,它的取值可能是: last:重写完成后,会停止继续处理当前区块所有属于ngx_http_rewrite ...
- nginx反代及后端web配置
一.反代配置,proxy_pass指向的upstream需要在反代的nginx.conf中配置 server {access_log /home/nginx/front_access.log;erro ...
- 支付宝H5支付demo
支付宝H5支付 首先我们必须注册一个支付宝应用(本案例就直接用支付宝的沙箱环境,这个沙箱也就是支付宝提供给开发者的一个测试环境) 登录地址:https://open.alipay.com/platfo ...
- Flask pythn Web 框架总结
Flask pythn Web 框架总结 一, Flask 介绍 Flask 是一个基于Python 实现的web 开发的'小型轻框架' 1. flask介绍 Flask是一个基于Python实现的w ...
- ANT下载和配置 IDEA
1.下载地址大全: http://archive.apache.org/dist/ant/binaries/ jdk与ant版本有对应关系,目前知道: jdk1.7与ant1.10不兼容,1.7必须用 ...
- postgreSQL命令大全(更新中)
1.PostgreSQL索引的建立https://blog.csdn.net/jubaoquan/article/details/78850899: 2.PostgreSQL9中索引的原理和效率查询h ...
- Linux 运维命令及知识
1.查找当前目录下所有以.tar结尾的文件然后移动到指定目录: find . -name “*.tar” -exec mv {}./backup/ ; 注解:find –name 主要用于查找某个文件 ...
- 给创业公司CEO的临别赠言
一别两宽,各生欢喜 2018年2月8日,我在这个公司的最后一天,三年半. 还记得2014年,在三里屯SOHO的某个咖啡厅中,你自信飞扬的脸和眼睛里暗夜星辰般的闪亮的希冀让我久久无法平静.终于在某一个耀 ...
- EXAM-2018-8-3
EXAM-2018-8-3 D H 喜闻乐见的水题 J lower_bound + upper_bound 一个可以查找第一个大于,另一个可查找第一个不小于. F 找规律+奇偶分析 偶数好找,就是奇数 ...
- spring-mvc基于注解的配置
将配置文件修改为: <?xml version="1.0" encoding="UTF-8"?> <beans xmlns="htt ...