登录
首页 >  数据库 >  MySQL

MySQL+Redis缓存+Gearman共同构建数据库缓存的方法

来源:脚本之家

时间:2023-01-21 11:34:46 495浏览 收藏

来到golang学习网的大家,相信都是编程学习爱好者,希望在这里学习数据库相关编程知识。下面本篇文章就来带大家聊聊《MySQL+Redis缓存+Gearman共同构建数据库缓存的方法》,介绍一下缓存、mysqlredis、gearman,希望对大家的知识积累有所帮助,助力实战开发!

yum install unzip openssl-devel pcre-devel gcc make -y 
tar -zxf nginx-1.22.0.tar.gz
./configure --with-http_stub_status_module --with-http_ssl_module --with-file-aio --add-module=/root/nginx-goodies-nginx-sticky-module-ng-08a395c66e42/
make -j 2 && make install
ln -s /usr/local/nginx/sbin/nginx /usr/local/bin/nginx
vim nginx.conf
          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;
          }

2、php部署安装以及模块安装

php安装同时部署相应的兼容模块方式

yum install php php-fpm -y 
yum install php-pecl-gearman-1.1.2-1.el7.x86_64.rpm php-pecl-igbinary-1.2.1-1.el7.x86_64.rpm php-pecl-redis-2.2.8-1.el7.x86_64.rpm php-fpm-5.4.16-46.el7.x86_64.rpm -y
systemctl start php-fpm.serrvice

将编写好的php放置到Nginx的默认发布目录位置

vim test.php
connect('192.168.63.102',6379) or die ("could net connect redis server");
  #      $query = "select * from test limit 9";
        $query = "select * from test";
        for ($key = 1; $key get($key))
                {
                        $connect = mysql_connect('192.168.63.109','redis','dockerps-A1');
                        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 $key"; echo "
"; echo "name is $data[$key]"; echo "
"; } ?>

二、后端部署

1.MySQL部署

1.建立用户设定远程登录

yum install mariadb-server -y
systemctl start mariadb.service

部署后在MySQL端进行创建一个用户给与远程登录权限,使得Redis作为缓存可以用来同步数据使用
建立test表保证有可以查看的数据

MariaDB [test]> CREATE USER redis@'%' IDENTIFIED BY 'dockerps-A1' ;
MariaDB [test]> CREATE TABLE `test` (`id` int(7) NOT NULL AUTO_INCREMENT, `name` char(8) DEFAULT NULL, PRIMARY KEY (`id`)) ENGINE=InnoDB DEFAULT CHARSET=utf8;
MariaDB [test]> INSERT INTO `test` VALUES (1,'test1'),(2,'test2'),(3,'test3'),(4,'test4'),(5,'test5'),(6,'test6'),(7,'test7'),(8,'test8'),(9,'test9');
MariaDB [test]> grant all on test.* to redis@'%';

2.注册UDF函数

yum install mariadb-devel -y			#下载对应的开发包
unzip lib_mysqludf_json-master.zip
gcc $(mysql_config --cflags) -shared -fPIC -o lib_mysqludf_json.so lib_mysqludf_json.c
										#利用udf函数使得其生成UDF函数
MariaDB [(none)]> show global variables like 'plugin_dir';
										#查看函数的存储目录
cp lib_mysqludf_json.so /usr/lib64/mysql/plugin/
MariaDB [(none)]> CREATE FUNCTION json_object RETURNS STRING SONAME 'lib_mysqludf_json.so';	

2.Redis作为缓存的部署以及安装

 make -j 2 && make install 
 ./utils/install_server.sh

三、germand分布式缓存位置部署

1、gearman原理

这里C端为MySQL的UDF函数节点通过已经注册的UDF函数和触发器结合使数据发生改变后将数据的更改通知到前端gearmand节点使其从Redis节点从新同步发生改变的数据
S端为前端worker节点即前端gearmand,同样需要在php环境中兼容这个插件的模块

2、C端部署

./configure --with-mysql=/usr/bin/mysql_config --libdir=/usr/lib64/mysql/plugin/
make -j 2 && make install 
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';
									#注册Gearman的UDF函数使其可以正常调用数据库的内容
MariaDB [(none)]> SELECT gman_servers_set('192.168.63.101:4730');
									#IP为S端IP以及端口为gearmand的运行端口
vim trigger.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 ;

3、S端部署

yum install gearmand-1.1.12-18.el7.x86_64.rpm libgearman-1.1.12-18.el7.x86_64.rpm -y
systemctl start gearmand.service

编写worker文件

vim worker.php

addServer();
$worker->addFunction('syncToRedis', 'syncToRedis');

$redis = new Redis();
$redis->connect('192.168.63.102', 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);
}
?>

总结

今天关于《MySQL+Redis缓存+Gearman共同构建数据库缓存的方法》的内容就介绍到这里了,是不是学起来一目了然!想要了解更多关于mysql的内容请关注golang学习网公众号!

声明:本文转载于:脚本之家 如有侵犯,请联系study_golang@163.com删除
相关阅读
更多>
最新阅读
更多>
课程推荐
更多>
评论列表