redis实现mysql的数据缓存

环境设定
base2 172.25.78.12 nginx+php
base3 172.25.78.13 redis端
base4 172.25.78.14 mysql端
1.在base2(nginx+php)上
配置nginx和php(这里的nginx只是提供了负载均衡,所以版本要求不高)
[root@base2 ~]# killall redis-server
[root@base2 ~]# ls
gearmand-1.1.12-18.el7.x86_64.rpm
php-fpm-5.4.16-46.el7.x86_64.rpm
libevent-devel-2.0.21-4.el7.x86_64.rpm
php-mysql-5.4.16-46.el7.x86_64.rpm
libgearman-1.1.12-18.el7.x86_64.rpm
php-pdo-5.4.16-46.el7.x86_64.rpm
libgearman-devel-1.1.12-18.el7.x86_64.rpm
php-pecl-gearman-1.1.2-1.el7.x86_64.rpm
libzip-0.10.1-8.el7.x86_64.rpm
php-pecl-igbinary-1.2.1-1.el7.x86_64.rpm
openssl-1.0.2k-16.el7.x86_64.rpm
php-pecl-redis-2.2.8-1.el7.x86_64.rpm
openssl-libs-1.0.2k-16.el7.x86_64.rpm
php-process-5.4.16-46.el7.x86_64.rpm
php-cli-5.4.16-46.el7.x86_64.rpm
php-xml-5.4.16-46.el7.x86_64.rpm
php-common-5.4.16-46.el7.x86_64.rpm

[root@base2 ~]# yum install -y *.rpm # 安装php

注:当服务器上有openssl-devel开发包时,会有冲突,所以要先删除
[root@base2 ~]# tar zxf nginx-1.14.2.tar.gz
[root@base2 ~]# cd nginx-1.14.2
[root@base2 nginx-1.14.2]# vim auto/cc/gcc
171 # debug
172 #CFLAGS="$CFLAGS -g"
[root@base2 nginx-1.14.2]# vim src/core/nginx.h
14 #define NGINX_VER “nginx/”
[root@base2 nginx-1.14.2]# ./configure --prefix=/usr/local/nginx
[root@base2 nginx-1.14.2]# make && make install
[root@base2 ~]# cd /usr/local/nginx/conf/
[root@base2 conf]# vim nginx.conf
location / {
root html;
index index.php index.html index.htm;
}

location ~ \.php$ {
    root           html;
    fastcgi_pass   127.0.0.1:9000;
    fastcgi_index  index.php;
   #fastcgi_param  SCRIPT_FILENAME  /scripts$fastcgi_script_name;
    include        fastcgi.conf;
}

在这里插入图片描述
[root@base2 conf]# ln -s /usr/local/nginx/sbin/nginx /sbin
[root@base2 conf]# nginx -t
[root@base2 conf]# nginx # 开启nginx服务
[root@base2 conf]# vim /etc/php.ini
878 date.timezone =Asia/Shanghai
[root@base2 conf]# vim /etc/php-fpm.d/www.conf
39 user = nginx
41 group = nginx
[root@base2 conf]# systemctl start php-fpm
[root@base2 conf]# systemctl status php-fpm # 开启php服务
在这里插入图片描述
[root@base2 conf]# netstat -antlp # 查看php的端口
在这里插入图片描述
nginx服务搭建成功
在这里插入图片描述
[root@base2 conf]# cd /usr/local/nginx/html/
[root@base2 html]# vim index.php # 编写php网页,测试php

<?php phpinfo() ?>

php配置成功
在这里插入图片描述
[root@server1 html]# vim index.php # 重新编写php页面,把redis服务器和mysql服务器建立练习

<?php $redis = new Redis(); $redis->connect('172.25.78.13',6379) or die ("could net connect redis server"); # 这是redis服务器 # $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.78.14','redis','redhat'); # 这是数据库服务器,用户以及密码 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 "
"; for ($key = 1; $key < 10; $key++) { echo "number is $key"; echo "
"; echo "name is $data[$key]"; echo "
"; } ![在这里插入图片描述](https://img-blog.csdnimg.cn/20191014181914107.png) 2.redis服务端 [root@base3 ~]# tar zxf redis-5.0.3.tar.gz [root@base3 ~]# cd redis-5.0.3 [root@base3 redis-5.0.3]# yum install gcc -y [root@base3 redis-5.0.3]# make && make install [root@base3 redis-5.0.3]# cd utils/ [root@base3 utils]# ./install_server.sh ![在这里插入图片描述](https://img-blog.csdnimg.cn/20191014181938627.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L2NhaW5pdXhpYW5mZWk=,size_16,color_FFFFFF,t_70) [root@base3 utils]# vim /etc/redis/6379.conf 70 bind 0.0.0.0 [root@base3 ~]# systemctl restart redis_6379 [root@base3 ~]# redis-cli 127.0.0.1:6379> info replication ![在这里插入图片描述](https://img-blog.csdnimg.cn/20191014181956980.png) [root@base3 ~]# netstat -antlp ![在这里插入图片描述](https://img-blog.csdnimg.cn/20191014182016607.png) 3.mysql服务端 [root@base4 ~]# yum install -y mariadb-server [root@base4 ~]# systemctl start mariadb [root@base4 ~]# mysql_secure_installation ![在这里插入图片描述](https://img-blog.csdnimg.cn/20191014182047323.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L2NhaW5pdXhpYW5mZWk=,size_16,color_FFFFFF,t_70) ![在这里插入图片描述](https://img-blog.csdnimg.cn/20191014182058548.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L2NhaW5pdXhpYW5mZWk=,size_16,color_FFFFFF,t_70) [root@base4 ~]# mysql -p Enter password: MariaDB [(none)]> show databases; ![在这里插入图片描述](https://img-blog.csdnimg.cn/20191014182111168.png) MariaDB [(none)]> create database test; MariaDB [(none)]> grant all on test.* to redis@'%' identified by 'redhat'; MariaDB [(none)]> flush privileges; MariaDB [(none)]> quit Bye [root@base4 ~]# 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; # 在test数据库里创建表 INSERT INTO `test` VALUES (1,'test1'),(2,'test2'),(3,'test3'),(4,'test4'),(5,'test5'),(6,'test6'),(7,'test7'),(8,'test8'),(9,'test9'); # 给表里添加内容 [root@base4 ~]# mysql -predhat < test.sql [root@base4 ~]# mysql -p Enter password: MariaDB [(none)]> select * from test.test; ![在这里插入图片描述](https://img-blog.csdnimg.cn/20191014182126661.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L2NhaW5pdXhpYW5mZWk=,size_16,color_FFFFFF,t_70) 测试 这是mysql服务器通过php页面利用redis缓存来显示数据库中的内容 ![在这里插入图片描述](https://img-blog.csdnimg.cn/20191014182151914.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L2NhaW5pdXhpYW5mZWk=,size_16,color_FFFFFF,t_70) 但是有一个问题,mysql更新后,redis无法同步 MariaDB [(none)]> use test; MariaDB [test]> update test set name='lala' where id=1; MariaDB [test]> select * from test.test; ![在这里插入图片描述](https://img-blog.csdnimg.cn/20191014182213818.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L2NhaW5pdXhpYW5mZWk=,size_16,color_FFFFFF,t_70) [root@base3 ~]# redis-cli # 发现redis服务器并没有同步mysql上的数据 ![在这里插入图片描述](https://img-blog.csdnimg.cn/20191014182228392.png) 2.gearman 实现redis和mysql的数据同步 Gearman 是一个支持分布式的任务分发框架: Gearman Job Server: Gearman 核心程序,需要编译安装并以守护进程形式运行在后台。 Gearman Client:可以理解为任务的请求者。 Gearman Worker:任务的真正执行者,一般需要自己编写具体逻辑并通过守护进程方式 运行,Gearman Worker 接收到 Gearman Client 传递的任务内容后,会按顺序处理。 大致流程:下面要编写的 mysql 触发器,就相当于 Gearman 的客户端。修改表,插入表就相当于直接 下发任务。然后通过 lib_mysqludf_json UDF 库函数将关系数据映射为 JSON 格式,然后 在通过 gearman-mysql-udf 插件将任务加入到 Gearman 的任务队列中,最后通过 redis_worker.php,也就是 Gearman 的 worker 端来完成 redis 数据库的更新。 [root@base2 ~]# systemctl start gearmand [root@base2 ~]# netstat -antlp ![在这里插入图片描述](https://img-blog.csdnimg.cn/20191014182307770.png) 安装 php 的 gearman 扩展,安装 lib_mysqludf_json,lib_mysqludf_json UDF 库函数将关系数据映射为 JSON 格式。通常,数据库中的数据映射为 JSON 格式,是通过程序来转换的。 [root@base4 ~]# ls lib_mysqlud_json-master.zip [root@base4 ~]# yum install -y unzip [root@base4 ~]# unzip lib_mysqludf_json-master.zip [root@base4 ~]# cd lib_mysqludf_json-master [root@base4 lib_mysqludf_json-master]# yum install -y gcc [root@base4 lib_mysqludf_json-master]# yum list mariadb-devel ![在这里插入图片描述](https://img-blog.csdnimg.cn/20191014182337551.png) [root@base4 lib_mysqludf_json-master]# yum install -y mariadb-devel.x86_64 [root@base4 lib_mysqludf_json-master]# gcc $(mysql_config --cflags) -shared -fPIC -o lib_mysqludf_json.so lib_mysqludf_json.c [root@base4 ~]# cp lib_mysqludf_json-master/lib_mysqludf_json.so /usr/lib64/mysql/plugin/ [root@base4 lib_mysqludf_json-master]# cd /usr/lib64/mysql/ [root@base4 ~]# cd /usr/lib64/mysql/plugin/ [root@base4 plugin]# mysql -p Enter password: MariaDB [(none)]> show global variables like 'plugin_dir'; # 查看 mysql 的模块目录: ![在这里插入图片描述](https://img-blog.csdnimg.cn/2019101418235237.png) MariaDB [(none)]> CREATE FUNCTION json_object RETURNS STRING SONAME 'lib_mysqludf_json.so'; # 注册 UDF 函数 MariaDB [(none)]> select * from mysql.func; ![在这里插入图片描述](https://img-blog.csdnimg.cn/20191014182409672.png) MariaDB [(none)]> quit Bye 安装 gearman-mysql-udf,这个插件是用来管理调用 Gearman 的分布式的队列。 [root@base4 ~]# ls gearman-mysql-udf-0.6.tar.gz [root@base4 ~]# cd gearman-mysql-udf-0.6 [root@base4 gearman-mysql-udf-0.6]# ./configure --with-mysql=/usr/bin/mysql_config --libdir=/usr/lib64/mysql/plugin/ # 有报错,依赖性没解决完 ![在这里插入图片描述](https://img-blog.csdnimg.cn/20191014182445911.png) [root@base4 gearman-mysql-udf-0.6]# cd [root@base4 ~]# yum install -y libgearman-devel-1.1.12-18.el7.x86_64.rpm libevent-devel-2.0.21-4.el7.x86_64.rpm libgearman-1.1.12-18.el7.x86_64.rpm # 解决依赖性 [root@base4 ~]# cd gearman-mysql-udf-0.6 [root@base4 gearman-mysql-udf-0.6]# ./configure --with-mysql=/usr/bin/mysql_config --libdir=/usr/lib64/mysql/plugin/ [root@base4 gearman-mysql-udf-0.6]# make && make install [root@base4 gearman-mysql-udf-0.6]# mysql -p Enter password: 注册 UDF 函数 MariaDB [(none)]> CREATE FUNCTION gman_do_background RETURNS STRING SONAME 'libgearman_mysql_udf.so'; MariaDB [(none)]> CREATE FUNCTION gman_servers_set RETURNS STRING SONAME 'libgearman_mysql_udf.so'; MariaDB [(none)]> select * from mysql.func; # 查看函数 ![在这里插入图片描述](https://img-blog.csdnimg.cn/20191014182508823.png) MariaDB [(none)]> SELECT gman_servers_set('172.25.78.12:4730'); # 指定 gearman 的服务信息 ![在这里插入图片描述](https://img-blog.csdnimg.cn/20191014182525952.png) 编写 mysql 触发器(根据实际情况编写) [root@base4 gearman-mysql-udf-0.6]# cd [root@base4 ~]# vim test.sql use test; DELIMITER $$ CREATE TRIGGER datatoredis AFTER UPDATE ON test FOR EACH ROW BEGIN SET @RECV=gman_do_background('syncToRedis', json_object(NEW.id as `id`, NEW.name as `name`)); END$$ DELIMITER ; ![在这里插入图片描述](https://img-blog.csdnimg.cn/20191014182550810.png) [root@base4 ~]# mysql -predhat < test.sql [root@base4 ~]# mysql -p Enter password: MariaDB [(none)]> SHOW TRIGGERS FROM test\G; # 查看触发器 ![在这里插入图片描述](https://img-blog.csdnimg.cn/20191014182609349.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L2NhaW5pdXhpYW5mZWk=,size_16,color_FFFFFF,t_70) 在base2上,编写 gearman 的 worker 端 [root@base2 ~]# cd /usr/local/ [root@base2 local]# vim worker.php <?php $worker = new GearmanWorker(); $worker->addServer(); $worker->addFunction('syncToRedis', 'syncToRedis'); $redis = new Redis(); $redis->connect('172.25.78.13', 6379); while($worker->work()); function syncToRedis($job) { global $redis; $workString = $job->workload(); $work = json_decode($workString); if(!isset($work->id)){ return false; } $redis->set($work->id, $work->name); # 这条语句就是将 id 作 KEY 和name 作 VALUE 分开存储,需要和前面写的 php 测试代码的存取一致。 } ?>

在这里插入图片描述
[root@base2 ~]# nohup php /usr/local/worker.php &> /dev/null &

3.测试:
在mysql端(base4)更新 mysql 中的数据
MariaDB [test]> update test set name=‘lala’ where id=1;
MariaDB [test]> update test set name=‘lala’ where id=2;
MariaDB [test]> update test set name=‘lala’ where id=3;
MariaDB [test]> select * from test;
在这里插入图片描述
在redis端(base3)查看是否同步成功
在这里插入图片描述
刷新测试页面数据同步
在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值