验证Redis高可用的实现
基本架构:Web(LAMP)+memcache+mysql
实验配置:
主机功能 | IP |
---|---|
Web(主机A) | 192.168.195.139 |
memcache(主机B) | 192.168.195.137 |
mysql(主机C) | 192.168.195.136 |
基础环境搭建
1.三台主机时间同步
[root@A ~]yum -y install ntp ntpdate
[root@A ~]ntpdate cn.pool.ntp.org
[root@A ~]hwclock --systohc
2.关闭三台主机防火墙
[root@A ~]systemctl stop firewalld
[root@A ~]systemctl disable firewalld
3.关闭selinux
[root@A ~]sed -i 's/SELINUX=enforcing/SELINUX=disabled/' /etc/selinux/config
[root@A ~]setenforce 0
Web服务端配置(LAMP架构)
1.安装hpptd,mysql,php
[root@A ~]wget https://downloads.mysql.com/archives/get/file/mysql-community-libs-compat8.0.16-2.el7.x86_64.rpm
[root@A ~]yum localinstall mysql-community-client-8.0.16-2.el7.x86_64.rpm mysql-communityserver-8.0.16-2.el7.x86_64.rpm mysql-community-libs-8.0.16-2.el7.x86_64.rpm mysql-community-common-8.0.16-2.el7.x86_64.rpm mysql-community-libs-compat8.0.16-2.el7.x86_64.rpm
[root@A ~]yum install http php php-gb php-mysql php-memcache
[root@A ~]systemctl restart httpd
[root@A ~]systemctl enable httpd
[root@A ~]systemctl restart mysql
[root@A ~]systemctl enable mysql
2.在mysql主机上创建新用户
[root@C ~]# mysql -uroot -pABC123.com
mysql> create user 'superadmin'@'%' identified by 'ABC123.com';
mysql> ALTER USER 'superadmin'@'%' IDENTIFIED WITH mysql_native_password BY 'ABC123.com';
mysql> flush privileges;
3.测试httpd功能
[root@A ~]# vim /var/www/html/index.html
hello,word
[root@A ~]# cat /var/www/html/index.html this is a test form 187..
hello,word
客户端测试结果:
4.测试php连接功能
[root@A ~]vim /var/www/html/index.php
[root@A ~]cat /var/www/html/index.php
<?php
phpinfo();
?>
客户端测试结果
5.测试mysql
[root@A ~]vim /var/www/html/mysql.php
<?php
$link=mysql_connect('192.168.195.136','superadmin','ABC123.com');
if($link) echo "<h1>Success!!</h1>";
else echo "Fail!!";
mysql_close();
?>
memcache服务配置
1.安装memcache和编译环境
[root@localB ~]# wget http://monkey.org/~provos/libevent-1.4.14b-stable.tar.gz
[root@localB ~]# tar zxvf libevent-1.4.14b-stable.tar.gz
[root@localB ~]# yum install make gcc gcc-c++
[root@localB ~]#cd libevent-1.4.14b-stable
[root@localB ~]#./configure --prefix=/usr/local/libevent/
[root@localB ~]#make
[root@localB ~]#make install
[root@localB ~]#wget http://www.memcached.org/files/memcached-1.5.16.tar.gz
[root@localB ~]# tar -zvxf memcached-1.5.16.tar.gz
[root@localB ~]#./configure --prefix=/usr/local/memcached --with-libevent=/usr/local/libevent/
[root@localB ~]#make
[root@localB ~]#make install
2.启动memcache监听客户端所有地址
[root@localB ~]# /usr/local/memcached/bin/memcached -d -l 0.0.0.0 -p 11211 -u root -m 64 -c 1024 -P /var/run/memcached.pid
[root@localhost ~]# ps -ef | grep memcache
root 40333 1 0 16:16 ? 00:00:02 /usr/local/memcached/bin/memcached -d -l 0.0.0.0 -p 11211 -u root -m 64 -c 1024 -P /var/run/memcached.pid
root 45641 6985 0 18:50 pts/0 00:00:00 grep --color=auto memcache
3.代码测试(web端)
[root@localA ~]#vim /var/www/html/mem.php
<?php
$memcache = new Memcache;
$memcache->connect('192.168.42.186', 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);
?>
4.修改web端的配置文件
[root@A ~]#vim /etc/php.ini #直接添加在配置文件中
session.save_handler = memcache
session.save_path = "tcp://192.168.195.137:11211? persistent=1&weight=1&timeout=1&retry_interval=15"
session.save_handler:设置session的储存方式为memcache。默认以文件方式存取session数据,如 果想要使用自定义的处理来存取session数据,比如memcache方式则修为session.save_handler = memcache
session.save_path:设置session储存的位置,多台memcache用逗号隔开
5.测试memcache(在Web端)
[root@localA ~]#vim /var/www/html/mem1.php
<?php
session_start();
if (!isset($_SESSION['session_time']))
{
$_SESSION['session_time'] = time();
}
echo "session_time:".$_SESSION['session_time']."<br />";
echo "now_time:".time()."<br />";
echo "session_id:".session_id()."<br />";
?>
6.在mysql 端创建测试数据库
[root@C ~]# mysql -uroot -pABC123.com
mysql>create database testab1;
mysql>use testab1;
mysql>create table test1(id int not null auto_increment,name varchar(20) default null,primary key(id)) engine=innodb auto_increment=1 default charset=utf8;
mysql>insert into test1(name) values ('tom1'),('tom2'),('tom3'),('tom4'),('tom5'); mysql>select * from test1;
mysql>grant all privileges on *.* to 'superadmin'@'%' with grant option;
7.测试memcache是否缓存数据成功
[root@localA ~]#vim /var/www/html/memca.php
<?php
$memcachehost = '192.168.195.137';
$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.195.136","supeadmin","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>";
}
?>
注意此文件中的
$memcachehost = ‘192.168.195.137’; 此处IP为Memcache主机的IP
$conn=mysql_connect(“192.168.195.136”,“supeadmin”,“ABC123.com”); 此处IP为mysql 主机的ip ,用户为前面设置的登录Mysql用户,任何主机都可以登录
mysql_select_db(testab1); 此处括号内的内容为Mysql主机中数据库中的库名,若书写错误,验证时数据将不会回显,仅显示下图的钱行内容
第一次数据是存在mysql数据库中,进行访问后数据存在了Memcache中,过一段时间后memcache中的数据被清除,查看时又需要从mysql数据库中取数据。