分布式缓存之mamcached

一,基础知识

1,mamcached使用目的

通过缓存数据库查询结果,减少数据库访问次数,以提高动态web应用的速度,提高可扩展性。

2,服务器端缓存实现工作原理
(1)memcached特性

mamcached使用了BSD许可的服务器端缓存实现,与其他服务器端缓存实现不同的是,其主要有两部分组成:独立运行的memcached服务器实例,以访问这些服务器实例的客户端。因此相比较于普通服务器端缓存实现各个缓存都运行在服务器实例之上的情况,memcached服务器实例则是在服务器实例之外独立运行的。

普通缓存memcached缓存
特性缓存与特定的应用实例绑定,每个应用实例只能访问特定缓存实例独立于各个服务器实例运行,每个应用实例可访问任意缓存
特点整个应用所能访问的缓存容量变小;缓存中存在数据冗余;缓存系统整体效率降低整个应用所能访问的缓存容量变大;缓存中无数据冗余;缓存系统整体效率较高

(2)数据的存取过程
在这里插入图片描述
3,内存管理
(1)内存管理模型

由于内存的碎片化,memcached使用了一种叫Slab的结构,在该分配算法中,内存按照1MB的大小划分为页,而该页内存会被继续分割为一系列具有相同大小的内存块。

(2)数据的存储
在这里插入图片描述

二,配置

1,环境规划

192.168.3.20:web
192.168.3.88:memcached
192.168.3.25:mysql

2,同步时间

yum -y install ntp ntpdate
ntpdate cn.pool.ntp.org
hwclock -s

3,关闭防火墙和selinux防火墙

systemctl stop firewald
systemctl disable firewalld
setenforce 0

4,web服务器配置(192.168.3.20)

yum localinstall mysql-community-client-8.0.16-2.el7.x86_64.rpm mysql-community-common-8.0.16-2.el7.x86_64.rpm mysql-community-libs-8.0.16-2.el7.x86_64.rpm mysql-community-libs-compat-8.0.16-2.el7.x86_64.rpm mysql-community-server-8.0.16-2.el7.x86_64.rpm
yum install http php php-gb php-mysql php-memcache
yum install httpd
systemctl restart httpd
systemctl enabled httpd

5,mysql服务器(192.168.3.25)

systemctl restart mysqld
systemctl enable mysqld

为服务器端创建用户:

mysql> create user 'memcache'@'%' identified by 'ABC123@com';
mysql> alter user 'memcache'@'%' identified with mysql_native_password by 'ABC123@com';
mysql> flush privileges;

注:php支持的插件认证方式为mysql_native_password,而mysql8的为caching_sha2_password
6,测试httpd功能(在web服务器上)

[root@localhost ~]# vim /var/www/html/index.html
[root@localhost ~]# cat /var/www/html/index.html
this is a test

在这里插入图片描述
7,测试php链接功能(在web服务器上)

[root@localhost ~]# cat /var/www/html/index.php
<?php
phpinfo();
?>

在这里插入图片描述
8,测试mysql(在web服务器上)

[root@localhost ~]# cat /var/www/html/mysql.php
<?php
$link=mysql_connect('192.168.3.20','memcache','ABC123@com');
if($link) echo "<h1>success!!</h1>";
else echo "Fail";
mysql_close();
?>

在这里插入图片描述
9,配置memcached(192.168.3.88)
(1)安装依赖包

[root@localhost ~]# wget http://monkey.org/~provos/libevent-1.4.14b-stable.tar.gz
[root@localhost ~]# tar xzvf libevent-1.4.14b-stable.tar.gz
[root@localhost ~]# yum install gcc gcc-c++
[root@localhost ~]# cd libevent-1.4.14b-stable 
[root@localhost libevent-1.4.14b-stable]#
 ./configure --prefix=/usr/local/libevent
[root@localhost libevent-1.4.14b-stable]#
 make
[root@localhost libevent-1.4.14b-stable]#
 make install

(2)安装memcached

[root@localhost ~]# https://memcached.org/files/memcached-1.5.19.tar.gz
[root@localhost ~]# tar xzvf memcached-1.5.16
[root@localhost ~]# cd memcached-1.5.16
[root@localhost memcached-1.5.16]# ./configure --prefix=/usr/local/memcached --with-libevent=/usr/locsl/libevent/
[root@localhost memcached-1.5.16]# make
[root@localhost memcached-1.5.16]# make install

(3)启动

[root@localhost ~]# /usr/local/memxached/bin/memcached -d -l 0.0.0.0 -p 11211 -u root -m 64 -c 1024 -P /usr/local/memxached/memcached.pid
 ps -ef |grep memcached
root       7142      1  0 18:04 ?        00:00:00 /usr/local/memxached/bin/memcached -d -l 0.0.0.0 -p 11211 -u root -m 64 -c 1024 -P /usr/local/memxached/memcached.pid
root       7155   7093  0 18:05 pts/0    00:00:00 grep --color=auto memcached
参数说明
-d启动一个守护进程
-l监听的服务器IP地址
-p设置memcached的tcp监听端口
-u运行memcached的用户
-m分配memcached使用的内存数量
-c最大运行的并发连接数量默认1024
-P设置保存memcached的pid文件,

10,测试memcached(在web服务器上)

[root@localhost ~]# cat /var/www/html/memcache.php
<?php
$memcache=new Memcache;
$memcache->connect('192.168.3.88',11211) or die ("could not connect");
$version=$memcache->getVersion();
echo "Server's version:".$version."<br/>";
$tmp_object=new stdClass;
$tmp_object->str_attr='test';
$tmp_object->int_attr=123;
$memcache->set('key',$tmp_object,false,10)or die ("failed to save data at the server");
echo "store data in the cache (data will expire in 10 seconds)<br/>";
$get_result=$memcache->get('key');
echo "data from the cache:<br/>";
var_dump($get_result);
?>

在这里插入图片描述
11,配置session(在web服务器上)

vim /etc/php.ini
session.save_handler=memcache
session.save_path="tcp://192.168.3.88:11211?persistent=1&weight=1&timeout=1&retry_interval=15"

12,测试memcachd的可用性(在web服务器上)

[root@localhost html]# cat /var/www/html/memcache.php
<?php
$memcache=new Memcache;
$memcache->connect('192.168.3.88',11211) or die ("could not connect");
$version=$memcache->getVersion();
echo "Server's version:".$version."<br/>";
$tmp_object=new stdClass;
$tmp_object->str_attr='test';
$tmp_object->int_attr=123;
$memcache->set('key',$tmp_object,false,10)or die ("failed to save data at the server");
echo "store data in the cache (data will expire in 10 seconds)<br/>";
$get_result=$memcache->get('key');
echo "data from the cache:<br/>";
var_dump($get_result);
?>

在这里插入图片描述
13,在mysql中创建数据库,测试memcached

mysql> create databases testab1;
mysql> use testab1;
mysql> create table(id int not null auto_increment,name varchar(20) default null,primary key(id)) enginee=innodb auto_increment=1 default charset=utf8;
mysql> insert into test1(name) ('tom1'),('tom2');
mysql>grant select on testab1.* to memcache@'%';

14,测试memcache是否缓存数据库

[root@localhost html]# cat /var/www/html/memcache1.php
<?php
$memcachehost = '192.168.3.88';
$memcacheport = 11211;
$memcachelife = 60;
$memcache = new Memcache;
$memcache->connect($memcachehost,$memcacheport) or die ("could not connect");
$query="select * from test1 limit 10";
$key=md5($query);
if(!$memcache->get($key))
{
    $conn=mysql_connect("192.168.3.25","memcache","ABC123@com");
    mysql_select_db(testab1);
    $result=mysql_query($query);
    while($row=mysql_fetch_assoc($result))
    {
        $arr[]=$row;
    }
    $f='mysql';
    $memcache->add($key,serialize($arr),0,30);
    $data=$arr;
}
else{
    $f='memcache';
    $data_mem=$memcache->get($key);
    $data=unserialize($data_mem);
}
echo $f;
echo "<br>";
echo "$key";
echo "<br>";
//print_r($data);
foreach($data as $a)
{
    echo "number is <b><font color=#FF0000>$a[id]</font></b>";
    echo "<br>";
    echo "name is <b><font color=#FF0000>$a[name]</font></b>";
    echo "<br>";
}
?>

在这里插入图片描述
在这里插入图片描述
注:第一次会出现mysql,再刷新页面,又会出现memcached,过了存活期,又是mysql

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值