Simulate the client REDIS-CLI implementation of Redis with a script: (Note: MySQL address is 10.86.30.203 Redis address is 10.86.31.50)
1. First determine the columns of the 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 the SQL script:
vi /home/darren/test.sqlSELECT 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 a total of 8 parameters, $length () is the length of the parameter. The TEST1,TEST2,TEST3 in the script are all MySQL column names and can be modified by themselves.
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
The errors above appears to be 0 for import success.
–raw: Make MySQL do not convert the line break in field values.
–skip-column-names: Does not include the column name in each line of MySQL output.
Importing MySQL data into Redis