高性能内存对象缓存Memcached
一、Memcached简介
Memcached是一套开源的高性能分布式内存对象缓存系统,它将所有的数据都存储在内存中,因为在内存中会统一维护一张巨大的Hash表,所以支持任意存储类型的数据。
它是经典的C/S架构,因此需要安装Memcached服务端与MemcachedAPI客户端。
1、存储方式与数据过期方式
(1)存储方式:Slab Allocation
按组分配内存,每次先分配一个Slab,相当于一个大小为1MB的页,然后在1MB的空间里根据数据划分大小相同的chunk。该方法可有效解决内存碎片问题,但可能会对内存空间有所浪费
(2)数据过期方式:LRU、Laxzy Expiration
LRU是指追加的数据空间不足时,会根据LRU的情况淘汰最近最少使用的记录。
Laxz Expriation即惰性过期,是指使用get查看记录时间,从而检查记录是否已经过期
2、缓存机制
缓存是常驻在内存的数据,能够快速进行读取。而Memcached就是一款缓存软件,当程序写入缓存数据请求时,Memcached的API接口将Key输入路由算法模块路由到集群中一台服务器,之后由API接口与服务器进行通信,完成一次分布式缓存写入。
3、分布式
主要依赖于客户端来实现分布式,多个Memcached服务器是独立的。分布式数据如何存储是由路由算法决定。
4、路由算法
(1)求余数hash算法
先用key做hash运算得到一个整数,再去做hash算法,根据余数进行路由。这种算法适合大多数需求,但是不适合用在动态变化的环境中,比如有机器需要添加或删除时,会导致大量对象的存储位置失效
(2)一致性hash算法
适合在动态变化的环境中使用。原理是按照hash算法把对应的key通过一定的hash算法处理后,映射形成一个首尾相连的闭合循环,然后通过使用与对象存储一样的hash算法将机器也映射到环中,按顺时针方向将所有对象存储到离最近的机器中
二、安装Memcached
1、安装Memcached服务器
(1)安装libevent
./configure --prefix=/usr/local/libevent
(2)安装Memcached
./configure --prefix=/usr/local/memcached --with-libevent=/usr/local/libevent
(3)设置Memcached服务脚本
①启动:/usr/local/memcached/bin/memcached
②关闭:killall memchced
[Unit]
Description=service of memcached
[Service]
Type=simple
ExecStart=/usr/local/memcached/bin/memcached -d -m 128 -u root
ExecStop=/usr/bin/killall memcached
RemainAfterExit=yes
[Install]
WantedBy=multi-user.target
2、Memcached API客户端
(1)安装nginx1.12.0
①创建用户
useradd -M -s /sbin/nologin www
②安装依赖包
yum install pcre pcre-devel openssl openssl-devel zlib zlib-devel -y
③编译安装
./configure --prefix=/usr/local/nginx --without-http_memcached_module --user=www --group=www --with-http_stub_status_module --with-http_ssl_module --with-http_gzip_static_module
④权限修改及优化
chown -R www:www /usr/local/nginx
ln -s /usr/local/nginx/sbin/nginx /usr/local/sbin/
(2)安装php
①依赖包
yum install -y gcc gcc-c++ autoconf libjpeg libjpeg-devel libpng libpng-devel freetype freetype-devel libpng libpng-devel libxml2 libxml2-devel zlib zlib-devel xml2 xml2-devel openssl openssl-devel glibc glibc-devel glib2 glib2-devel bzip2 bzip2-devel ncurses ncurses-devel curl libcurl-devel gdbm-devel db4-devel libXpm libXpm-devel libX11 libX11-devel gd-devel gmp-devel readline-devel libxslt-devel expat-devel xmlrpc-c xmlrpc-c-devel
http://nchc.dl.sourceforge.net/project/mcrypt/Libmcrypt/2.5.8/libmcrypt-2.5.8.tar.gz
./configure
②php安装
./configure --prefix=/usr/local/php --with-config-file-path=/usr/local/php/etc --with-mysql --with-pdo-mysql --with-mysqli --with-openssl --with-freetype-dir --with-jpeg-dir --with-png-dir --with-zlib-dir --with-mhash --with-mcrypt --with-bz2 --enable-zip --with-curl --with-gettext --with-iconv --with-xmlrpc --enable-fpm --enable-sockets --enable-sysvshm --enable-mbstring --enable-pdo --enable-libxml --enable-xml --enable-soap --enable-session --enable-ctype --enable-ftp --enable-bcmath --enable-shmop --enable-inline-optimization --enable-opcache --enable-mbregex --enable-pcntl --enable-cgi --enable-wddx
③优化
cp /root/php-5.6.31/php.ini-production /usr/local/php/etc/php.ini
ln -s /usr/local/php/etc/php.ini /etc/php.ini
④php-fpm设置
#php-fpm.conf文件,取消”;pid = run/php-fpm.pid”的注释,同时修改运行账号通nginx服务的运行账号一致
cp /usr/local/php/etc/php-fpm.conf.default /usr/local/php/etc/php-fpm.conf
ln -s /usr/local/php/etc/php-fpm.conf /etc/php-fpm.conf
pid = run/php-fpm.pid
user = www
group = www
⑤niginx设置
location ~ \.php$ {
root html;
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
(3)安装libmemcached(API与Memcached对接)
./configure --prefix=/usr/local/libmemcached --with-memcached
(4)安装php的memcached扩展组件(memcached-2.2.0)
/usr/local/php/bin/phpize
./configure --enable-memcached --with-php-config=/usr/local/php/bin/php-config --with-libmemcached-dir=/usr/local/libmemcached --disable-memcached-sasl
(5)安装php的memcache2.2.7
/usr/local/php/bin/phpize
./configure --enable-memcache --with-php-config=/usr/local/php/bin/php-config
(6)安装intl-3.0.0
wget http://pecl.php.net/get/intl-3.0.0.tgz
yum -y install libicu-devel
./configure --enable-intl --with-php-config=/usr/local/php/bin/php-config
(7)在PHP配置文件中启动扩展库
[Intl]
extension = intl.so
[Memcached]
extension = memcached.so
[Memcache]
extension = memcache.so
(8)php测试页面
<?php
$memcache=new Memcache; #创建一个memcache对象
$memcache->connect('192.168.100.11',11211) or die("Could not connect");
$memcache->set('key','memcache-testaaaaaaaa');
$get_value=$memcache->get('key');
echo $get_value;
?>
三、Memcached数据库操作与管理
1、常见操作
(1)添加键值数据
add username 0 0 7
键值名:username
自定义信息:0
过期时间:0
字节数:7
(2)查询键值数据
get username
gets username
(3)更新键值数据
set username 0 0 10,之后输入值
replace username 0 0 7,之后输入值
(4)清除一条缓存数据
delete username
(5)检查后更新
gets username
cas username 0 0 7 1
(6)追加数据
append key:原键值后追加
prepend key:原键值前追加
(7)清除数据
flush_all
(8)查询统计
stats
Memcached stats 命令用于返回统计信息例如 PID(进程号)、版本号、连接数等
stats items:返回所有键值对的统计信息
stats cachedump 1 0:返回指定存储空间的键值对
stats slabs:显示各个slab的信息,包括chunk大小、数目、使用情况等
stats sizes:输出所有item的大小和个数
stats reset:清空统计数据
四、mysql与memcached对接
1、maridb或mysql安装
yum -y install maridb maridb-server maridb-devel
2、安装libmemcached(libmemcached-0.34与memcached_functions_mysql-1.1配对进行安装)
./configure --prefix=/usr/local/libmemcached --with-memcached
3、安装扩展函数库memcached_functions_mysql
./configure --with-mysql=/usr/local/mysql/bin/mysql_config --with-libmemcached=/usr/local/libmemcached/
4、往MySQL中添加memcache UDF函数
(1)在mysql里边执行
source /root/packages/memcatch/mysql/memcached_functions_mysql-1.1/sql/install_functions.sql
(2)在shell环境执行导入命令
mysql -uroot -p111111 < /root/packages/memcatch/mysql/memcached_functions_mysql-1.1/sql/install_functions.sql
(3)导入报错找不到libmemcached_functions_mysql.so
cp /root/packages/memcatch/mysql/memcached_functions_mysql-1.1/src/.libs/libmemcached_functions_mysql.so.0.0.0 /usr/local/mysql/lib/plugin/libmemcached_functions_mysql.so
5、测试
select memc_servers_set('192.168.11.31:11211');
select memc_server_count();
select memc_set('m','llppppp');
select memc_get('m');
6、mysql触发器
(1)新建数据库表
create database test123;
create table tab1(id int not null, name varchar(32) not null, primary key (id));
(2)建立触发器(插入)
delimiter $
create trigger tab1_insert_memc
>before insert on tab1
>for each row begin
>set @m=memc_set(NEW.id,NEW.value);
>end$
(3)更新及删除
create trigger tab1_update_memc
before update on tab1
create trigger tab1_delete_memc
before delete on tab1
(4)测试:测试前先连接memcache
五、主主复制和高可用架构
1、主主复制
(1)使用memcached复制架构,需安装支持复制功能的memcached安装包
memcached-1.2.8-repcached-2.2.tar.gz
(2)编译安装(先安装libevent)
./configure --prefix=/usr/local/memcached_replication --enable-replication --with-libevent=/usr/local/libevent
(3)libevent模块复制
ln -s /usr/local/libevent/lib/libevent-1.4.so.2 /usr/lib64/
(4)启动
/usr/local/memcached_replication/bin/memcached -d -u root -m 128 -x 192.168.11.31 -X 18000
/usr/local/memcached_replication/bin/memcached -d -u root -m 128 -x 192.168.11.34 -X 18000
2、keepalive高可用架构
(1)安装keepalived
yum keepalived
(2)配置主keepalived
global_defs {
router_id LVS_1 !本路由器(服务器)名称
}
vrrp_instance VI_1 { !定义热备实例
state MASTER !热备份状态,MASTER为主服务器
interface ens33 !承载VIP的物理接口
virtual_router_id 51 !虚拟路由器的ID号,每个热备组保持一致
priority 100 !优先级,越大优先级越高
advert_int 1 !通告间隔描述
nopreempt !不主动抢占资源,只在master或高优先级上设置
authentication { !认证信息,同组保持一致
auth_type PASS !认证类型
auth_pass 123456 !密码
}
virtual_ipaddress { !指定飘逸地址(VIP),可以有多个
192.168.11.210
}
}
virtual_server 192.168.11.210 11211 { !VIP及端口
delay_loop 6 !健康检查时间
persistence_timeout 50 !保持连接时间
protocol TCP !协议
sorry_server 192.168.11.34 11211 !对机
real_server 192.168.11.31 11211 { !本机
weight 1 !权重
notify_down /root/memcached.sh !宕机时停止keepalived服务
TCP_CHECK { !健康检查方式
connect_port 11211 !端口
connetc_timeout 3 !超时时间
nb_get_retry 3 !重试次数
delay_before_retry 3 !重试间隔
}
}
}
(3)配置从keepalived
global_defs {
router_id LVS_1 !本路由器(服务器)名称
}
vrrp_instance VI_1 { !定义热备实例
state MASTER !热备份状态,MASTER为主服务器
interface ens33 !承载VIP的物理接口
virtual_router_id 51 !虚拟路由器的ID号,每个热备组保持一致
priority 90 !优先级,越大优先级越高
advert_int 1 !通告间隔描述
!去掉nopreempt选项
authentication { !认证信息,同组保持一致
auth_type PASS !认证类型
auth_pass 123456 !密码
}
virtual_ipaddress { !指定飘逸地址(VIP),可以有多个
192.168.11.210
}
}
virtual_server 192.168.11.210 11211 { !VIP及端口
delay_loop 6 !健康检查时间
persistence_timeout 50 !保持连接时间
protocol TCP !协议
sorry_server 192.168.11.31 11211 !对机
real_server 192.168.11.34 11211 { !本机
weight 3 !权重
notify_down /root/memcached.sh !宕机时停止keepalived服务
TCP_CHECK { !健康检查方式
connect_port 11211 !端口
connetc_timeout 3 !超时时间
nb_get_retry 3 !重试次数
delay_before_retry 3 !重试间隔
}
}
}
(4)服务停止脚本memcached.sh
echo “/usr/bin/systemctl stop keepalived” >/root/memcached.sh
chmod +x /root/memcached.sh