码迷,mamicode.com
首页 > 数据库 > 详细

Mysql 与Redis的同步实践

时间:2015-05-31 20:12:48      阅读:253      评论:0      收藏:0      [点我收藏+]

标签:redis   数据库   php   mysql   

一、测试环境在Ubuntu kylin 14.04 64bit

已经安装Mysql、Redis、php、lib_mysqludf_json.so、Gearman。
点击这里查看测试数据库及表参考
本文也有些基本操作,在之前文章里有介绍。

1、安装

安装gearman-mysql-udf

apt-get install libgearman-dev
wget https://launchpad.net/gearman-mysql-udf/trunk/0.6/+download/gearman-mysql-udf-0.6.tar.gz
tar -xzf gearman-mysql-udf-0.6.tar.gz
cd gearman-mysql-udf-0.6
./configure --with-mysql=/usr/bin/mysql_config --libdir=/usr/lib/mysql/plugin/
make
sudo make install

注册UDF函数:

CREATE FUNCTION gman_do_background RETURNS STRING SONAME ‘libgearman_mysql_udf.so‘;
CREATE FUNCTION gman_servers_set RETURNS STRING SONAME ‘libgearman_mysql_udf.so‘;
CREATE FUNCTION gman_do RETURNS STRING SONAME "libgearman_mysql_udf.so";
CREATE FUNCTION gman_do_high RETURNS STRING SONAME "libgearman_mysql_udf.so"; 
CREATE FUNCTION gman_do_low RETURNS STRING SONAME "libgearman_mysql_udf.so";
CREATE FUNCTION gman_do_background RETURNS STRING SONAME "libgearman_mysql_udf.so";
CREATE FUNCTION gman_do_high_background RETURNS STRING SONAME "libgearman_mysql_udf.so";
CREATE FUNCTION gman_do_low_background RETURNS STRING SONAME "libgearman_mysql_udf.so";
CREATE AGGREGATE FUNCTION gman_sum RETURNS INTEGER SONAME "libgearman_mysql_udf.so";
CREATE FUNCTION gman_servers_set RETURNS STRING SONAME "libgearman_mysql_udf.so";

指定Gearman服务器的信息:

SELECT gman_servers_set(‘127.0.0.1:4730‘);

使用示例:

参照http://blog.csdn.net/xundh/article/details/46287681 建立一个reverse.php的worker

<?php
$worker= new GearmanWorker();
$worker->addServer();
$worker->addFunction("reverse", "my_reverse_function");
while ($worker->work());

function my_reverse_function($job){
  return strrev($job->workload());
}
?>

输入命令php reverse.php运行。

到mysql里,输入:

SELECT gman_do("reverse",‘abcdef‘) AS test FROM Users; ---FROM Users也可以不带。

技术分享

SELECT gman_do("reverse", password) AS test FROM Users;

可以看到输出结果,其中password列已经被reverse的worker处理,mysql这时充当client端:

技术分享

还可以输入以下命令测试:

SELECT gman_do_high("reverse", password) AS test FROM Users; --高优先权

技术分享

SELECT gman_do_background("reverse", password) AS test FROM Users; --后台低优先权,返回主机和作业号。

技术分享

创建syncToRedis作业

停止前面的reverse worker,建立一个syncToRedis.php

<?php
$worker = new GearmanWorker();
$worker->addServer();
$worker->addFunction(‘syncToRedis‘, ‘syncToRedis‘);

$redis = new Redis();
$redis->connect(‘127.0.0.1‘, 6379);
echo("begin:\n");

while($worker->work());
function syncToRedis($job)
{
        global $redis;
        $workString = $job->workload();
        $work = json_decode($workString);
    echo(‘get value:‘);
    echo($workString);
    echo("\n");
    echo(‘json_decode:‘);
    var_dump($work);
    echo("\n");
        if(!isset($work->user_id)){
                return false;
        }
        $redis->set($work->user_id, $workString);
}

在mysql里测试一下:

    SELECT gman_do("syncToRedis", json_object(user_id as user_id,password as password)) AS test FROM Users;

技术分享

如果redis监控是打开的,可以看到redis已经收到了数据:
技术分享

redis查询结果
技术分享

2、建立触发器

    DELIMITER $$
	CREATE TRIGGER datatoredis AFTER UPDATE ON Users
	  FOR EACH ROW BEGIN
	    SET @ret=gman_do_background(‘syncToRedis‘, json_object(NEW.user_id as `user_id`, NEW.email as `email`,NEW.display_name as `display_name`,NEW.password as `password`));  
	  END$$
DELIMITER ;

执行SQL语句测试:

insert into Users values(‘8‘,‘new‘,‘3‘,‘hello‘);
update Users set email=‘new8@qq.com‘ where user_id=8;

技术分享

正常使用时,可以把worker使用&设置为后台任务:
nohup php syncToReids.php &

Mysql 与Redis的同步实践

标签:redis   数据库   php   mysql   

原文地址:http://blog.csdn.net/xundh/article/details/46288547

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!