mysql 数据导入redis缓存

时间:2022-02-25 02:31:01

假设表结构如下

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(
"*4rn",
‘$‘, LENGTH(redis_cmd), ‘rn‘,
redis_cmd, ‘rn‘,
‘$‘, LENGTH(redis_key), ‘rn‘,
redis_key, ‘rn‘,
‘$‘, LENGTH(hkey), ‘rn‘,
hkey, ‘rn‘,
‘$‘, LENGTH(hval), ‘rn‘,
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