首頁 > 軟體

MySQL+Redis快取+Gearman共同構建資料庫快取的方法

2022-10-20 14:03:51

前言

一、前端搭建

1、Nginx部署

安裝部署Nginx

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
<?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.建立使用者設定遠端登入

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

<?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資料庫快取內容請搜尋it145.com以前的文章或繼續瀏覽下面的相關文章希望大家以後多多支援it145.com!


IT145.com E-mail:sddin#qq.com