RHEL6.5x64 下memcached安装和使用
从网上下载libevent-2.0.21-stable.tar.gz 和memcached-1.4.5.tar.gz,然后运行
第一步:安装gcc编译器
[root@memcached ~]#yum -y isnatll gcc
第二步:安装libevent和memcached
[root@memcached srv]# tar zxvf libevent-2.0.21-stable.tar.gz
[root@memcachedsrv]# cd libevent-2.0.21-stable
[root@memcachedlibevent-2.0.21-stable]#./configure --prefix=/usr/local/libevent
[root@memcachedlibevent-2.0.21-stable]#make&& make install
[root@memcachedlibevent-2.0.21-stable]# cd /srv
[root@memcachedsrv]#tar zxvf memcached-1.4.5.tar.gz
[root@memcachedsrv]#cd memcached-1.4.5
[root@memcached memcached-1.4.5]#./configure--prefix=/usr/local/memcache --with-libevent=/usr/local/libevent
[root@memcachedmemcached-1.4.5]#make &&make install
第三步:检查memcache的库文件
[root@memcachedmemcached-1.4.5]# cd /usr/local/memcache/bin/
[root@memcachedbin]# ./memcached --help
若显示./memcached: error while loading shared libraries:libevent-2.0.so.5: cannot open shared object file: No such file or directory
解决这个问题需要把只需要做个软链接就可以了:
[root@memcached bin]# ln -s/usr/local/libevent/lib/* /usr/lib64/
[root@memcachedbin]# ./memcached -help 可以查看memche的帮助
第四步:启动 memcache
[root@memcached~]# ln -s /usr/local/memcache/bin/* /usr/local/bin/
[root@memcached~]# memcached -d -m 1000 -u root -p12000 -c 1024 -l 10.0.0.2 -P/tmp/memcached.pid
-d :表示启动一个守护进程
-m 是分配给 memcached 使用的内存
-u 运行 memcached 的用户
-l 是 memcached 监听的 ip
-p 是 memcached 监听的端口
-c memcache 运行的最大并发连接数
-P 是设置 memcache 的 pid 文件
配置启动脚本
[root@memcached bin]# vi /root/restartmemcached.sh
# 加入下面行
killall -9 memcached
sleep 1
/usr/local/bin/memcached -d -u root -m 4096-p 10001 -c 30000 -P /opt/memcached/pid/m10001.pid -v
/usr/local/bin/memcached -d -u root -m 4096-p 10002 -c 30000 -P /opt/memcached/pid/m10002.pid-v
/usr/local/bin/memcached -d -u root -m 4096-p 10003 -c 30000 -P /opt/memcached/pid/m10003.pid -v
/usr/local/bin/memcached -d -u root -m 4096-p 10004 -c 30000 -P -f 2.0 -L /opt/memcached/pid/m10004.pid -v
/usr/local/bin/memcached -d -u root -m 4096-p 10005 -c 30000 -P /opt/memcached/pid/m10005.pid -v
/usr/local/bin/memcached -d -u root -m 4096-p 10006 -c 30000 -P /opt/memcached/pid/m10006.pid -v
/usr/local/bin/memcached -d -u root -m 4096-p 10007 -c 30000 -P /opt/memcached/pid/m10007.pid-v
/usr/local/bin/memcached -d -u root -m 4096-p 10008 -c 30000 -P -f 2.0 -L /opt/memcached/pid/m10008.pid -v
/usr/local/bin/memcached -d -u root -m 4096-p 10009 -c 30000 -P /opt/memcached/pid/m10009.pid -v
/usr/local/bin/memcached -d -u root -m 4096-p 10101 -c 30000 -P /opt/memcached/pid/m10101.pid -v
/usr/local/bin/memcached -d -u root -m 4096-p 10102 -c 30000 -P /opt/memcached/pid/m10102.pid -v
/usr/local/bin/memcached -d -u root -m 4096-p 10103 -c 30000 -P -L /opt/memcached/pid/m10103.pid-v
/usr/local/bin/memcached -d -u root -m 4096-p 10104 -c 30000 -P /opt/memcached/pid/m10104.pid –v
运行脚本
. restartmemcached.sh
检查是否有启动
netstat -lntp
把脚本加入到随机启动:
#echo /root/ restartmemcached.sh>>/etc/rc.local
第五部: memcache 的分布式部署和应用
(本部分涉及到编程,本人不会,参考别人文档)
基于 PHP 扩展的 Memcache 客户端实际上早已经实现,而且非常稳定。先解释一些名词, Memcache 是 danga.com 的一个开源项目,可以类比于 MySQL 这样的服务,
而 PHP 扩展的 Memcache 实际上是连接 Memcache 的方式。
其次,进行 PHP 扩展的安装,官方地址是 http://pecl.php.net/package/memcache
最后,启动 Memcache 服务,比如这样:
PHP 的 PECL 扩展中的 memcache 实际上在 2.0.0 的版本中就已经实现多服务器支持,现在都已经 2.2.5 了。请看如下代码
$memcache = newMemcache;
$memcache->addServer('localhost',11213);
$memcache->addServer('localhost',11214);
$memcache->addServer('localhost',11215);
$memStats =$memcache->getExtendedStats();
print_r($memStats);
通过上例就已经实现 Memcache 的分布式部署,是不是非常简单。
分布式系统的良性运行
在 Memcache 的实际使用中,遇到的最严重的问题,就是在增减服务器的时候,会导致大范围的缓存丢失,从而可能会引导数据库的性能瓶颈,为了避免出现这种情况,
请先看 Consistent hashing 算法,中文的介绍可以参考这里,通过存取时选定服务器算法的改变,来实现。
修改 PHP 的 Memcache 扩展 memcache.c 的源代码中的
修改 PHP 的 Memcache 扩展 memcache.c 的源代码中的
"memcache.hash_strategy"= standard
为
"memcache.hash_strategy"= consistent
重新编译,这时候就是使用 Consistent hashing 算法来寻找服务器存取数据了。
有效测试数据表明,使用 Consistent hashing 可以极大的改善增删 Memcache 时缓存大范围丢失的情况。
NonConsistentHash:92% of lookups changed after adding a target to the existing 10
NonConsistentHash:90% of lookups changed after removing 1 of 10 targets
ConsistentHash: 6%of lookups changed after adding a target to the existing 10
ConsistentHash: 9%of lookups changed after removing 1 of 10 target
运维网声明
1、欢迎大家加入本站运维交流群:群②:261659950 群⑤:202807635 群⑦870801961 群⑧679858003
2、本站所有主题由该帖子作者发表,该帖子作者与运维网 享有帖子相关版权
3、所有作品的著作权均归原作者享有,请您和我们一样尊重他人的著作权等合法权益。如果您对作品感到满意,请购买正版
4、禁止制作、复制、发布和传播具有反动、淫秽、色情、暴力、凶杀等内容的信息,一经发现立即删除。若您因此触犯法律,一切后果自负,我们对此不承担任何责任
5、所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其内容的准确性、可靠性、正当性、安全性、合法性等负责,亦不承担任何法律责任
6、所有作品仅供您个人学习、研究或欣赏,不得用于商业或者其他用途,否则,一切后果均由您自己承担,我们对此不承担任何法律责任
7、如涉及侵犯版权等问题,请您及时通知我们,我们将立即采取措施予以解决
8、联系人Email:admin@iyunv.com 网址:www.yunweiku.com