Using script to simulate the implementation of redis cli on the client side of redis: (Note: mysql address is 10.86.30.203 redis address is 10.86.31.50)
1. First determine the columns of mysql table:
desc test; +-----------+-------------+------+-----+---------+-------+ | Field | Type | Null | Key | Default | Extra | +-----------+-------------+------+-----+---------+-------+ | id | int(11) | YES | | NULL | | | test1 | varchar(50) | YES | | NULL | | | test2 | varchar(64) | YES | | NULL | | | test3 | int(11) | YES | | NULL | | +-----------+-------------+------+-----+---------+-------+ 4 rows in set (0.00 sec)
2. Write sql script:
vi /home/darren/test.sql SELECT CONCAT( "*8\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') FROM ( SELECT 'HMSET' AS redis_cmd, CONCAT('id:',id) AS redis_key, 'test1' AS hkey1,test1 AS hval1, 'test2' AS hkey2,test2 AS hval2, 'test3' AS hkey3,test3 AS hval3 FROM test ) AS t
Note: where * 8 means there are 8 parameters in total, and $length() is the length of the parameter. test1, test2 and test3 in the script are all mysql column names, which can be modified by yourself.
3. Execute the Import command:
mysql -uroot -pxxx -h 10.86.30.203 test --skip-column-names --raw < /home/darren/test.sql | redis-cli --pipe ---------------------------------------------------------------------------------------------------------------- mysql: [Warning] Using a password on the command line interface can be insecure. All data transferred. Waiting for the last reply... Last reply received from server. errors: 0, replies: 1
If the above errors are 0, the import is successful.
– raw: causes mysql not to convert line breaks in field values.
– skip column names: make each row of mysql output contain no column names.