MySQL+Redis缓存+Gearman共同构建数据库缓存的方法
吾爱主题
阅读:159
2022-11-25 16:11:00
评论:0
前言
一、前端搭建
1、Nginx部署
?安装部署Nginx
1 2 3 4 5 | 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 |
1 2 3 4 5 6 7 8 | 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安装同时部署相应的兼容模块方式
1 2 3 | 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的默认发布目录位置
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 | vim test.php <?php $redis = new Redis(); $redis ->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 < 10; $key ++) { if (! $redis ->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 "<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>" ; } ?> |
二、后端部署
1.MySQL部署
1.建立用户设定远程登录
?1 2 | yum install mariadb-server -y systemctl start mariadb.service |
?部署后在MySQL端进行创建一个用户给与远程登录权限,使得Redis作为缓存可以用来同步数据使用
建立test表保证有可以查看的数据
1 2 3 4 | 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函数
?1 2 3 4 5 6 7 8 | 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作为缓存的部署以及安装
?1 2 | make -j 2 && make install . /utils/install_server .sh |
三、germand分布式缓存位置部署
1、gearman原理
这里C端为MySQL的UDF函数节点通过已经注册的UDF函数和触发器结合使数据发生改变后将数据的更改通知到前端gearmand节点使其从Redis节点从新同步发生改变的数据
S端为前端worker节点即前端gearmand,同样需要在php环境中兼容这个插件的模块
2、C端部署
?1 2 3 4 5 6 7 | . /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的运行端口 |
1 2 3 4 5 6 7 | 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端部署
?1 2 | 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文件
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 | vim worker.php <?php $worker = new GearmanWorker(); $worker ->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共同构建数据库缓存的文章就介绍到这了,更多相关Redis Gearman数据库缓存内容请搜索服务器之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持服务器之家!
原文链接:https://blog.csdn.net/b_______/article/details/126900340
声明
1.本站遵循行业规范,任何转载的稿件都会明确标注作者和来源;2.本站的原创文章,请转载时务必注明文章作者和来源,不尊重原创的行为我们将追究责任;3.作者投稿可能会经我们编辑修改或补充。