数据库
首页 > 数据库> > Gearman实现Mysql到Redis的数据同步

Gearman实现Mysql到Redis的数据同步

作者:互联网

redis与数据库结合,作为数据库数据的缓存提供给前端
一.实现数据库,php,http和redis的架构

server1提供http服务,使用php语言;
server2提供redis缓存服务;
server3提供后端数据库服务…

大概流程 :客户端通过nginx和php访问后端数据库时,先在redis这个数据库缓存中查找,看是否含有想要的数据,如果没有就去后端数据库查找,将查找到数据返回给客户端一份,另外在redis中缓存一份….下次如果访问相同的数据就直接去redis,缩短时间同时也减轻数据库的查找压力

1.server1安装http,php,php-mysql

yum install -y httpd php php-mysql

2.获取测试页面test.php,修改后放到httpd发布目录

vim test.php
mv test.php /var/www/html/
systemctl start httpd

<?php
        $redis = new Redis();
        $redis->connect('172.25.40.2',6379) or die ("could net connect redis server");
         #redis主机IP
  #      $query = "select * from test limit 9";
        $query = "select * from test";
        for ($key = 1; $key < 10; $key++)
        {
                if (!$redis->get($key))
                {
                        $connect = mysql_connect('172.25.40.3','redis','redhat'); # 连接数据库,ip,用户,密码
                        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>";
        }
?>

3.php添加redis模块

php -m | grep mysql
php -m | grep redis

在这里插入图片描述
源码编译php与redis的连接软件—phpredis-master.zip

unzip phpredis-master.zip 
cd phpredis-master/
phpize  # 创建预编译环境

报错:

Can't find PHP headers in /usr/include/php
The php-devel package is required for use of this command.

网上下载安装同版本php-devel

yum install -y php-devel-5.4.16-42.el7.x86_64.rpm 
cd phpredis-master/
phpize   # 创建预编译环境
./configure --help
 ./configure --enable-redis
make && make install   # 源码编译安装

进入目录/usr/lib64/php/modules/下,查看编译后存在的模块文件

在这里插入图片描述

cd /etc/php.d/
cp mysql.ini redis.ini
vim redis.ini 
extension=redis.so

4.在php的配置文件中修改时区

vim /etc/php.ini 
date.timezone = Asia/Shanghai

重启httpd查看已经加载的模块
systemctl restart httpd
php -m | grep redis

在这里插入图片描述

server2安装redis

vim /etc/redis/6379.conf
systemctl start redis_6379
ps ax
server3
systemctl stop redis_6379
67 chkconfig redis_6379 off
68 yum install -y mariadb-server
69 systemctl start mariadb
70 mysql
71 ls
72 mysql < test.sql
73 mysql
select * from test.test;
MariaDB [(none)]> grant all on test.* to redis@’%’ identified by ‘redhat’;

update test set name=‘redhat’ where id=1;

更新后无法自动同步

标签:Gearman,Redis,数据库,Mysql,redis,key,mysql,test,php
来源: https://blog.csdn.net/qq_37382077/article/details/99686715