`

mysql到redis的复制

阅读更多

系统开发中时常会需要缓存来提升并发读的能力,这时可以通过mysql的UDF和hiredis来进行同步 

 

A UDF(user defined functions) plugin for MySQL, which can be used for pushing data to Redis

https://github.com/jackeylu/mysql2redis

 

This is used to move the mysql data to redis or from redis to mysql.

https://github.com/zhangjg/mysql2redis

 

MySQL Syncer is a project which parse mysql binlog and sync to other datases, such as redis, mongodb and any other databases..

https://github.com/Terry-Mao/MySQL-Syncer

 

前题:安装了mysql5.5和client 

1、安装mysql2redis 

Shell代码   收藏代码
  1. git clone https://github.com/jackeylu/mysql2redis.git  
  2. cd mysql2redis  
  3. ./install.sh  



2、安装hiredis 

Shell代码   收藏代码
  1. git clone http://github.com/redis/hiredis  
  2. cd hiredis  
  3. make && make install  



3、安装mysql json udf 

Shell代码   收藏代码
  1. git clone https://github.com/mysqludf/lib_mysqludf_json.git  
  2. cd lib_mysqludf_json  
  3. gcc $(/usr/local/mariamysql/bin/mysql_config --cflags) -shared -fPIC -o lib_mysqludf_json.so lib_mysqludf_json.c  


然后将lib_mysqludf_json.so拷贝到mysql的plugin 
在mysql里执行lib_mysqludf_json.sql 

4、在mysql里创建table,trigger 

Sql代码   收藏代码
  1. CREATE  TABLE IF NOT EXISTS `test`.`t_users` (  
  2.   `user_name` VARCHAR(50) NOT NULL ,  
  3.   `nick_name` VARCHAR(100) NOT NULL ,  
  4.   `passwordVARCHAR(32) NOT NULL ,  
  5.   `age` INT NULL ,  
  6.   `create_time` TIMESTAMP NULL DEFAULT CURRENT_TIMESTAMP ,  
  7.   PRIMARY KEY (`user_name`) ,  
  8.   UNIQUE INDEX `un_t_users_nick_name` (`nick_name` ASC) )  
  9. ENGINE = InnoDB;  

 

Sql代码   收藏代码
  1. DELIMITER |  
  2. create trigger tri_users_redis_json BEFORE INSERT on t_users  
  3.   For EACH ROW   
  4. BEGIN  
  5. DECLARE done INT DEFAULT 999;  
  6.   
  7. set done = redis_command("127.0.0.1",6379,concat("set user::",cast(NEW.user_name as CHAR)," ",json_object(NEW.user_name as userName,NEW.nick_name as nickName,NEW.age as age)));  
  8.   
  9. if done>0 then  
  10.   SIGNAL sqlstate '45001' set message_text = "Redis error! ";  
  11. end if ;  
  12.   
  13. END;  
  14. |  
  15. DELIMITER ;  

 

Sql代码   收藏代码
  1. insert into t_users(user_name,nick_name,password,age,create_time) values('Sally','雪莉','123456',25,CURRENT_TIMESTAMP)  



mysql的 


  


redis的 


 

使用mysql的udf和trigger可以保证mysql和redis的数据一致性,SIGNAL sqlstate '45001'会在redis失败时回滚事物。 

问题:hiredis里递交带空格数据需要这样使用 

C代码   收藏代码
  1. reply = redisCommand(context, "SET key:%s %s", myid, value);  


那么对于mysql2redis的redis_command是无法工作的,这部分可以为不同命令单写函数。如redis_command_set。 

为了得到更好的灵活性,现在在使用gearman的UDF client,php的worker联合工作。

  • 大小: 22.3 KB
  • 大小: 21.1 KB
分享到:
评论
1 楼 JavaStart 2014-03-07  
运行mysql2redis 的install.sh 文件为何提示
ERROR 1126 (HY000) at line 20: Can't open shared library 'lib_mysqludf_redis.so' (errno: 11 libhiredis.so.0.10: cannot open shared object file: No such file or directory)
明明mysql 的plugin里面有这个文件啊!求解!

相关推荐

Global site tag (gtag.js) - Google Analytics