how to move data from mysql to redis

2019-01-28 11:22发布

I attempt to put some frequently used data into redis server from mysql . so redis just as a read server , i need to move data from mysql to redis. can anybody recommend a good approache ? i have read some topics and have some thinking share

1、through mysql trigger to record proper data , through timing app move data to redis 2、read mysql logs ,analysis it ,then put it to redis.

BTW: in my application data stored in redis don't need real-time, a little latency is ok.

标签: mysql redis
2条回答
再贱就再见
2楼-- · 2019-01-28 12:06

Use mass-insert mode of redis-cli.

Check this out - http://dcw.ca/blog/2013/01/02/mysql-to-redis-in-one-step/

查看更多
Fickle 薄情
3楼-- · 2019-01-28 12:10

I think the mysql udf plugin (https://github.com/Ideonella-sakaiensis/lib_mysqludf_redis) can help you to synchronize data from Mysql to Redis.

example:

DELIMITER $$
CREATE TABLE `my_table` (
  id    varchar(16) PRIMARY KEY,
  text  varchar(32)
);

set up a trigger for the table and call redis command by mysql udf

DELIMITER $$
CREATE TRIGGER `after_insert_my_table`
AFTER INSERT ON `my_table` FOR EACH ROW
BEGIN
  DO `redis`('redis://@127.0.0.1/0/', 'SET', new.`id`, new.`text`);
END $$
DELIMITER ;

then you can get value by my_table id

mysql>  SELECT `redis`('redis://@127.0.0.1/0/', 'GET', <my_table id>)\G
*************************** 1. row ***************************
`redis`('redis://@127.0.0.1/0/', 'GET', <my_table id>): {
        "out":  <my_table text>
}
查看更多
登录 后发表回答