标签:redis mysql
##创建表及数据
CREATE TABLE events_all_time (
id int(11) unsigned NOT NULL AUTO_INCREMENT,
action varchar(255) NOT NULL,
count int(11) NOT NULL DEFAULT 0,
PRIMARY KEY (id),
UNIQUE KEY uniq_action (action)
);
insert into events_all_time(action,count) values(‘yangdan‘,1000);
insert into events_all_time(action,count) values(‘liufeng‘,200);
##保存同步数据脚本
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,
‘events_all_time‘ AS redis_key,
action AS hkey,
count AS hval
FROM events_all_time
) AS t;
##执行同步命令(shell)
mysql [-h 127.0.0.1] -uroot -plinuxcast -DtestRedis --skip-column-names --raw </usr/local/events_to_redis.sql |redis-cli --pipe
##登录redis
hgetall events_all_time
本文出自 “小天犬的博客” 博客,请务必保留此出处http://9196589.blog.51cto.com/9186589/1728650
标签:redis mysql
原文地址:http://9196589.blog.51cto.com/9186589/1728650