webserver ->(r/w) redisserver -> 钩子 ->input ->mysql
webserver -> (redisserver cache) <--> r w -> mysql -->update mysql ->trigger --> redisserver
主从:不实时,复制延迟,在slave上数据备份
1、环境
server5 :172.25.17.105 | web |
server6 :172.25.17.106 | redis-cache |
server7 :172.25.17.107 | mysql |
cgi:无
fastcgi:有后台程序
2、部署
server5:
yum install -y httpd php php-mysql
在php中添加redis模块:
yum install php-devel
php-redis-master.zip
unzip php-redis-master.zip
cd php-redis-master
phpize
./configure --enble-redis
make
make install 生成一个redis.so模块
cd /usr/lib64/php/modules/ redis.so模块在这里
vim /etc/php.ini
data.timezone = Asia/Shanghai
cd /etc/php.d/
cp mysql.ini redis.ini
vim redis.ini
extension=redis.so
php - m | grep redis 查看php中的模块
systemctl start httpd
测试脚本:
vim /var/www/html/test.php
<?php
$redis = new Redis();
#连接redis数据库
$redis->connect('server6',6379) or die ("could net connect redis server");
# $query = "select * from test limit 9";
$query = "select * from test";
for ($key = 1; $key < 10; $key++)
{
if (!$redis->get($key))
{ #连接数据库:server7
$connect = mysql_connect('server7','redis','westos');
mysql_select_db(test);
$result = mysql_query($query);
//如果没有找到$key,就将该查询sql的结果缓存到redis
while ($row = mysql_fetch_assoc($result))
{
$redis->set($row['id'],$row['name']);
}
$myserver = 'mysql';
break;
}
else
{
$myserver = "redis";
$data[$key] = $redis->get($key);
}
}
echo $myserver;
echo "<br>";
for ($key = 1; $key < 10; $key++)
{
echo "number is <b><font color=#FF0000>$key</font></b>";
echo "<br>";
echo "name is <b><font color=#FF0000>$data[$key]</font></b>";
echo "<br>";
}
?>
server6:
安装数据库:
yum install gcc
tar zxf redis-5.0.3.tar.gz
cd redis-5.0.3/
make && make install
cd utils/
./install_server.sh
配置数据库:
vim /etc/redis/6379.conf
bind 0.0.0.0
启动redis数据库:
/etc/init.d/redis_6379 start
server7:
yum install mariadb-server
用户授权:
mysql:
grant all on test.* to redis@'%' identified by 'westos';
初始化数据库:
vim test.sql
use test;
CREATE TABLE `test` (`id` int(7) NOT NULL AUTO_INCREMENT, `name` char(8) DEFAULT NULL, PRIMARY KEY (`id`)) ENGINE=InnoDB DEFAULT CHARSET=utf8;
INSERT INTO `test` VALUES (1,'test1'),(2,'test2'),(3,'test3'),(4,'test4'),(5,'test5'),(6,'test6'),(7,'test7'),(8,'test8'),(9,'test9');
mysql:
mysql < test.sql
3、测试
在server7上边修改数据,查看效果:
server6上边的redis数据没有同步过来,还是原来的数据
删掉redis中缓存的数据,重新访问,数据更新,而且redis也会缓存数据:
删除数据缓存之后,可以直接get的到数据,时间可能有延迟。