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

本文记录了一次使用KVM搭建web集群的实验,重点是配置和使用memcached缓存服务。首先,通过克隆虚拟机并设置为memcache服务器,IP为192.168.122.33。接着,使用yum安装memcached。然后,在nginx虚拟机(192.168.122.32)上安装php并配置以访问memcached,修改php.ini文件并添加相关设置。最后,通过重启php和创建测试脚本来验证配置成功。
摘要由CSDN通过智能技术生成
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;
?>

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值