您的位置:首页 > 数据库 > Memcache

用 KVM 搭建web集群实验笔记 - memcached

2016-09-27 23:49 465 查看
memcached是一个开源的、高并发的分布式内存缓存系统,常用于在动态web集群系统后端、数据库前端,作为临时缓存,减少WEB系统直接请求数据库的次数,减轻后端数据库的压力。数据在memcahed中以key-valued对的方式保存数据。memcached采用基于文本的协议,可用telnet/nc等命令直接操作。

网站查询数据时,先检查数据是否在memcahed缓存中,如果在,直接返回数据;如果不在缓存中,则从数据库请求数据,并把新取到的数据缓存一份到memcached。网站更新数据(增、删、改)时,程序先更新到数据库,同时更新到memcached。这些需要应用程序配合完成。
以下是memcached安装和配置的简单实验。

1. memcached安装

用前面的方法克隆一台虚拟机主机名改为memcache,ip地址设为192.168.122.33

直接用yum安装

yum install libevent libevent nc -y
yum install memcached -y
检查memcached是否安装好

[root@memcache ~]# which memcached
/usr/bin/memcached
[root@memcache ~]#
2.运行memcached服务

memcached -m 16m -p 11212 -d -u root -c 8192
检查memcached是否运行

[root@memcache ~]# ps -ef |grep memcached |grep -v grep
root      1178     1  0 16:15 ?        00:00:00 memcached -m 16m -p 11212 -d -u root -c 8192
用nc和telnet访问memcached
root@memcache ~]# printf "set key1 0 0 15\r\nhello memcached\r\n" |nc 127.0.0.1 11212
STORED
[root@memcache ~]# printf "get key1\r\n" |nc 127.0.0.1 11212
VALUE key1 0 15
hello memcached
END
[root@memcache ~]#
yum install telnet -y
[root@memcache ~]# telnet 127.0.0.1 11212
Trying 127.0.0.1...
Connected to 127.0.0.1.
Escape character is '^]'.
get key1
VALUE key1 0 15
hello memcached
END
set key2 0 0 7
mydream
STORED
get key2
VALUE key2 0 7
mydream
END
quit
Connection closed by foreign host.
[root@memcache ~]# 停止memcached服务
ps -ef |grep memcached |grep -v grep |awk '{print $2'} |xargs kill
3.配置php访问memcached

在虚拟机nginx上进行安装,ip为192.168.122.32

wget -q http://pecl.php.net/get/memcache-2.2.7.tgz tar -xvf memcache-2.2.7.tgz
cd memcache-2.2.7
/usr/local/php/bin/phpize
./configure --enable-memcache --with-php-config=/usr/local/php/bin/php-config
make

[root@nginx memcache-2.2.7]# make install
Installing shared extensions: /usr/local/php/lib/php/extensions/no-debug-non-zts-20090626/
[root@nginx memcache-2.2.7]#
进入/usr/local/php/lib目录编辑php.ini文件,在文件末尾增加
extension_dir="/usr/local/php/lib/php/extensions/no-debug-non-zts-20090626/"
extension=memcache.so
重启php

[root@nginx lib]# /usr/local/php/sbin/php-fpm -t
[26-Sep-2016 15:57:29] NOTICE: configuration file /usr/local/php/etc/php-fpm.conf test is successful

[root@nginx etc]# pkill php-fpm
[root@nginx etc]# ps -ef |grep php-fpm |grep -v grep

[root@nginx etc]# /usr/local/php/sbin/php-fpm
[root@nginx etc]#
编辑/usr/local/nginx/html/www/testing_mem.php文件进行验证

vi testing_mem.php
<?php
$memcache = new Memcache;
$memcache->connect('192.168.122.33',121212) or die("Could not connect Mc server");
$memcache->set("key", "hello memcache");
$get = $memcache->get("key");
echo $get;
?>
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: