memcached简介
memcached是一套分布式的高速缓存系统,由LiveJournal的Brad Fitzpatrick开发,但被许多网站使用。这是一套开放源代码软件,以BSD license授权发布
特点
- 协议简单
- 基于libevent的事件处理
- 内置内存存储方式
- memcached不互相通信的分布式
缺点
- 缺乏认证以及安全管制
- 不支持持久化
实验部署memcached
1、部署服务端
(1)安装环境包
[root@memcached ~]# yum install gcc gcc-c++ make -y
(2)安装事件库
[root@memcached memcached]# tar zxvf libevent-2.1.8-stable.tar.gz -C /opt
[root@memcached memcached]# cd /opt/libevent-2.1.8-stable/
[root@memcached libevent-2.1.8-stable]# ./configure --prefix=/usr/local/libevent
[root@memcached libevent-2.1.8-stable]# make && make install
(3)安装mencached
[root@memcached memcached]# tar zxvf memcached-1.5.6.tar.gz -C /opt
[root@memcached memcached]# cd /opt/memcached-1.5.6/
[root@memcached memcached-1.5.6]# ./configure \
> --prefix=/usr/local/memcached \
> --with-libevent=/usr/local/libevent
[root@memcached memcached-1.5.6]# make && make install
[root@memcached ~]# ln -s /usr/local/memcached/bin/* /usr/local/bin/
[root@90memcached ~]# memcached -d -m 32m -p 11211 -u root
(4)登陆测试
[root@memcached ~]# yum install telnet -y
[root@memcached ~]# telnet 127.0.0.1 11211
Trying 127.0.0.1...
Connected to 127.0.0.1.
Escape character is '^]'.
#创建
add username 0 0 7
#add--创建;username--键值名称;0 0 7:0-不设置序列号 0-无时间要求 7-输入字节长度
1234567
STORED
#查看
get username
VALUE username 0 7
1234567
END
#删除
delete username
DELETED
get username
END
#
2、安装客户端,使用LAMP架构
[root@lamp memcached]# tar zxvf memcache-2.2.7.tgz -C /opt
在解压目录下无配置脚本,需要手动生成
#增加为PHP的模块后再对memcache进行配置
[root@lamp memcache-2.2.7]# /usr/local/php5/bin/phpize
[root@lamp memcache-2.2.7]# ./configure \
> --enable-memcache \
> --with-php-config=/usr/local/php5/bin/php-config
[root@lamp memcache-2.2.7]# make && make install
[root@lamp memcache-2.2.7]# vim /usr/local/php5/php.ini
extension_dir="/usr/local/php5/lib/php/extensions/no-debug-zts-20131226/"
extension=memcache.so
3、用客户端去检测服务端是否可以连接
[root@lamp memcache-2.2.7]# vim /usr/local/httpd/htdocs/index.php
<?php
$memcache = new Memcache();
$memcache->connect('192.168.7.128',11211);
$memcache->set('key','Memcache test Successfull!',0,60);
$result = $memcache->get('key');
unset($memcache);
echo $result;
?>