假设表结构如下
CREATE TABLE sas_sys_app
(user_id
varchar(38) DEFAULT NULL,app_id
int(11) DEFAULT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8
编写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,
'hkey' AS redis_key,
user_id AS hkey,
app_id AS hval
FROM lumi_saas_access.sas_sys_app where company_id=64
) AS t
执行导入语句
mysql -uadmin -h host -p数据库密码 库 --default-character-set=utf8 --skip-column-names --raw < app2redis.sql | redis-cli -h redis-host -p 6379 --pipe