標籤:mysql 遷移 redis
參考國外大牛的方法,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"
本文出自 “賀春暘的技術專欄” 部落格,請務必保留此出處http://hcymysql.blog.51cto.com/5223301/1749526
一步完成 MySQL 向 Redis 遷移