参考国外大牛的方法,http://www.oschina.net/translate/mysql-to-redis-in-one-step
实验成功,以下是操作方法:
MySQL表结构数据
[test]> select * from hechunyang; +----+--------+---------+ | id | myname | mymoney | +----+--------+---------+ | 1 | aa | 1000 | | 2 | bb | 2000 | +----+--------+---------+ 2 rows in set (0.00 sec) |
编辑迁移SQL脚本
cat mysql_to_redis.sql SELECT CONCAT( "*4\r\n", '$', LENGTH(redis_cmd), '\r\n', redis_cmd, '\r\n', '$', LENGTH(redis_key), '\r\n', redis_key, '\r\n', '$', LENGTH(hkey), '\r\n', hkey, '\r\n', '$', LENGTH(hval), '\r\n', hval, '\r' ) FROM ( SELECT 'HSET' AS redis_cmd, 'keyname' AS redis_key, myname AS hkey, mymoney AS hval FROM hechunyang ) AS t; |
一步完成 MySQL 向 Redis 迁移
mysql test --skip-column-names --raw < mysql_to_redis.sql | redis-cli --pipe All data transferred. Waiting for the last reply... Last reply received from server. errors: 0, replies: 2 |
Redis查看数据
127.0.0.1:6379> keys *
1) "keyname"
127.0.0.1:6379> hkeys keyname
1) "aa"
2) "bb"
127.0.0.1:6379> hget keyname aa
"1000"
127.0.0.1:6379> hget keyname bb
"2000"