mysql向redis导入数据
数据库结构如下
如果是linux系统下,如此整备数据
SELECT CONCAT(
"*10\r\n",
'$', LENGTH(redis_cmd), '\r\n',redis_cmd, '\r\n','$', LENGTH(redis_key), '\r\n',redis_key, '\r\n',
'$', LENGTH(hkey1), '\r\n',hkey1, '\r\n','$', LENGTH(hval1), '\r\n', hval1, '\r\n'
'$', LENGTH(hkey2), '\r\n',hkey2, '\r\n','$', LENGTH(hval2), '\r\n', hval2, '\r\n'
'$', LENGTH(hkey3), '\r\n',hkey3, '\r\n','$', LENGTH(hval3), '\r\n', hval3, '\r\n'
'$', LENGTH(hkey4), '\r\n',hkey4, '\r\n','$', LENGTH(hval4), '\r\n', hval4, '\r'
)
FROM (
SELECT
'HMSET' AS redis_cmd, CONCAT(uid,'_hash') AS redis_key,
'uid' AS hkey1,uid AS hval1,
'userid' AS hkey2,userid AS hval2,
'username' AS hkey3,username AS hval3,
'pwd' AS hkey4,pwd AS hval4
FROM loginfo
) AS t
如果在windows下,没错,把fuck \r\n 换为 \n
SELECT CONCAT( "*10\n", '$', LENGTH(redis_cmd), '\n',redis_cmd, '\n','$', LENGTH(redis_key), '\n',redis_key, '\n', '$', LENGTH(hkey1), '\n',hkey1, '\n','$', LENGTH(hval1), '\n', hval1, '\n' '$', LENGTH(hkey2), '\n',hkey2, '\n','$', LENGTH(hval2), '\n', hval2, '\n' '$', LENGTH(hkey3), '\n',hkey3, '\n','$', LENGTH(hval3), '\n', hval3, '\n' '$', LENGTH(hkey4), '\n',hkey4, '\n','$', LENGTH(hval4), '\n', hval4, '\n' ) FROM ( SELECT 'HMSET' AS redis_cmd, CONCAT(uid,'_hash') AS redis_key, 'uid' AS hkey1,uid AS hval1, 'userid' AS hkey2,userid AS hval2, 'username' AS hkey3,username AS hval3, 'pwd' AS hkey4,pwd AS hval4 FROM loginfo ) AS t
dos 或者 终端命令如下
mysql -u root -p -D oldpassion --default-character-set=utf8 --skip-column-names --raw <mycoach.sql | redis-cli --pipe