一、memcache的作用和原理
最终实现的拓扑图:
1、概述
是一套开源的高性能分布式内存对象缓存系统
所有的数据都存储在内存中
支持任意存储类型的数据
提高网站的访问速度
2、作用
分布式内存缓存工具
可以对网站的数据或者数据库的数据进行缓存
3、原理
客户端向web服务器发去请求
如果memcached中有用户请求的缓存,就直接从缓存中响应用户
Memcached中没有缓存用户请求的数据,转向数据库响应
二、memcached缓存数据的类型和支持的语言
1、缓存数据的类型
图像、视频、文件、数据库查询的结果
2、支持的语言
C C++ C# perl php python ruby java
3、memcached的数据算法(hash)
通过hash算法将用户查询的数据是生成随机值进行缓存
随机值不会重复
用户通过编号进行查询
4、Memcached路由算法(详细)
-
求余数hash算法
先用key做hash运算得到一个整数,再去做hash算法,根据余数进行路由,这种算法适合大多数据需求,但是不适合用在动态变化的环境中
-
一致性hash算法
按照hash算法把对应的key通过一定的hash算法处理后映射形成一个首尾相接闭合循环,然后通过使用与对象存储一样的hash算法将机器也映射到环中,顺时针方向计算将所有对象存储到里自己最近的机器中 -
适合在动态变化的环境中使用
三、安装单点的memcached
1、安装依赖程序
[root@centos1 libevent-1.4.9-stable]# ./configure --prefix=/usr/local/libevent
[root@centos1 libevent-1.4.9-stable]# make && make install
2、安装memcached
[root@centos1 memcached-1.2.6]# ./configure --prefix=/usr/local/memcached --with-libevent=/usr/local/libevent
[root@centos1 memcached-1.2.6]# make && make install
3、优化命令
[root@centos1 /]# vim /etc/profile.d/memcached.sh
PATH=$PATH:/usr/local/memcached/bin/
[root@centos1 /]# source /etc/profile.d/memcached.sh
4、加载libevent
[root@centos1 /]# vim /etc/ld.so.conf
include ld.so.conf.d/*.conf
/usr/local/libevent/lib/
[root@centos1 /]# ldconfig
5、启动服务(-d:后台运行、-m:运行内存大小、-p:指定端口、-u:指定用户)
[root@centos1 /]# memcached -d -m 300 -p 11211 -uroot
6、使用telnet登录并管理
[root@centos1 /]# yum -y install telnet
[root@centos1 /]# telnet 192.168.200.10 11211
四、管理和操作memcached
- start中命令显示的各项解释:
STAT curr_items 0 有效key的数量
STAT total_items 2 总共设置过key的数量
STAT curr_connections 2 当前的连接次数
STAT total_connections 5 总的连接次数
STAT cmd_get 5 获取key的次数
STAT cmd_set 14 设置key的次数
STAT get_hits 2 命中率,成功查询次数
STAT get_misses 3 获取key失败的次数
STAT bytes_read 520 读取数据的大小
STAT bytes_written 1112 写入数据的大小
- 对memcached的使用:
set benet 0 0 4 创建键值0:标识、0:永不超时、4:值的大小,只能为4字节
accp
get benet 查看创建的值
delete benet 删除键
五、安装manget(在两台中都要安装)
1、安装依赖程序(需要使用网络源)
[root@mem01 /]# yum -y install libevent-devel
2、安装manget
[root@mem01 /]# mkdir /usr/local/magent
[root@mem01 /]# tar xzvf /root/memcached/magent-0.5.tar.gz -C /usr/local/magent/
[root@mem01 /]# vim /usr/local/magent/Makefile (添加-lm)
LIBS = -levent -lm
[root@mem01 ~]# vim /usr/local/magent/ketama.h (添加这几项)
#ifndef SSIZE_MAX
#define SSIZE_MAX 32767
#endif
[root@mem01 ~]# cd /usr/local/magent/
[root@mem01 magent]# make
3、优化命令
[root@mem01 magent]# cp ./magent /usr/local/bin/
六、配置keepalived
1、安装keepalived(两台都要安装)
[root@mem01 /]# yum -y install keepalived
2、配置主配置文件(注意:备份keepalived修改router_id、state、priority)
[root@mem01 /]# vim /etc/keepalived/keepalived.conf
! Configuration File for keepalived
global_defs {
router_id LVS_master
}
vrrp_script magent {
script "/opt/magent.sh"
intercal 2
}
vrrp_instance VI_1 {
state MASTER
interface ens32
virtual_router_id 51
priority 100
advert_int 1
authentication {
auth_type PASS
auth_pass 1111
}
virtual_ipaddress {
192.168.200.15
}
track_script {
magent
}
}
3、配置监控脚本(两台中都需要配置)
[root@mem01 /]# vim /opt/magent.sh
#!/bin/bash
KEEPALIVED=`ip addr |grep 192.168.200.15 |grep -v grep |wc -l`
if [ $KEEPALIVED -gt 0 ];then
magent -u root -n 51200 -l 192.168.200.15 -p 12000 -s 192.168.200.10:11211 -b 192.168.200.20:11211
else
pkill -9 magent
fi
[root@mem01 /]# chmod +x /opt/magent.sh
[root@mem01 /]# systemctl start keepalived
[root@mem01 /]# systemctl enable keepalived
4、将服务器中的memcached服务开启
[root@mem01 /]# memcached -d -m 300 -p 11211 -uroot
[root@mem02 /]# memcached -d -m 300 -p 11211 -uroot
5、客户端验证(写入的测试数据会分别存入这两台服务器中)
[root@centos3 ~]# telnet 192.168.200.15 12000
set benet 0 0 4
accp
STORED
get benet
VALUE benet 0 4
accp
END
七、.事先准备LAMP环境。
八、安装phpredis扩展工具
1、安装
[root@server /]# cd /usr/src/memcache-2.2.7/
[root@server memcache-2.2.7]# phpize 生成configure等文件
[root@server memcache-2.2.7]# ./configure --with-php-config=/usr/bin/php-config --enable-memcache
[root@server memcache-2.2.7]# make
[root@server memcache-2.2.7]# make test 或 make check
[root@server memcache-2.2.7]# make install
2、配置memcache
注意:在安装完成后会显示 memcache.so的位置
1)配置php主配置文件。
[root@server /]# vim /usr/local/php/php.ini
extension_dir="/usr/local/php/lib/php/extensions/no-debug-non-zts-20090626/"
memcache_extension=memcache.so
2)重新启动httpd服务
[root@server /]# systemctl restart apached.service
3)登录网站查看
3、编辑测试脚本
[root@server ~]# vim /var/www/centos7/memcache.php
<?php
$memcache = new Memcache();
$memcache->connect(‘192.168.200.15’,12000);
$memcache->set(‘key’,‘Memcache test success!’,0,60);
$result =
m
e
m
c
a
c
h
e
−
>
g
e
t
(
′
k
e
y
′
)
;
u
n
s
e
t
(
memcache->get('key'); unset (
memcache−>get(′key′);unset(memcache);
echo $result;
?>
4、客户端验证