Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to migrate MySQL to Redis

2025-01-17 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Database >

Share

Shulou(Shulou.com)05/31 Report--

This article mainly introduces "how to migrate from MySQL to Redis". In daily operation, I believe many people have doubts about how to migrate from MySQL to Redis. The editor consulted all kinds of materials and sorted out simple and easy-to-use methods of operation. I hope it will be helpful to answer the doubts about "how to migrate from MySQL to Redis". Next, please follow the editor to study!

Article catalogue

Mysql to Redis data protocol

Move a large table from mysql to redis and you will find that it is unbearably slow to extract, transform, or load a row of data. Here I'm going to tell you a little trick to set you free. The content generated by the mysql command line is passed directly to the redis-cli by using the way of "pipe output" to bypass the "middleware" so that the two can achieve the best speed in data operation.

A mysql table with about 8 million rows of data originally takes 90 minutes to import into redis, but only two minutes after using this method. Whether you believe it or not, I did.

Other translated versions (1)

Zzxworld

Translated yesterday (14:36) 1CREATE TABLE events_all_time (2id int (11) unsigned NOT NULL AUTO_INCREMENT,3action varchar (255) NOT NULL,4count int (11) NOT NULL DEFAULT 0mai 5 primary KEY (id), 6UNIQUE KEY uniq_action (action) 7)

The redis commands to be executed in each row of data are as follows:

1HSET events_all_time [action] [count]

According to the above redis command rules, create an events_to_redis.sql file that is used to generate SQL in redis data protocol format:

01Mustang-events_to_redis.sql0203SELECT CONCAT (04 "* 4\ r\ n", 05mm events_to_redis.sql0203SELECT CONCAT, LENGTH (redis_cmd),'\ r\ nlmd,'\ r\ nmeme 06redispatch cmd,'\ r\ nmeme 07redispatch, LENGTH (redis_key),'\ r\ nmeme 08redispatch key,'\ r\ nmeme 09mb, LENGTH (hkey),'\ r\ nHere 10hkey,'\ r\ nHere, LENGTH (hval),'\ r\ n' 12hval,'\ rang 13) 14FROM (15SELECT16HSET'as redis_cmd,17'events_all_time' AS redis_key,18action AS hkey,19count AS hval20FROM events_all_time21) AS t

Ok, execute with the following command:

1mysql stats_db-skip-column-names-raw < events_to_redis.sql | redis-cli-pipe

Important mysql parameters state:

-- raw: causes mysql to not convert newline characters in field values.

-- skip-column-names: causes each row of mysql output to contain no column names.

At this point, the study on "how to transfer MySQL to Redis" is over. I hope to be able to solve your doubts. The collocation of theory and practice can better help you learn, go and try it! If you want to continue to learn more related knowledge, please continue to follow the website, the editor will continue to work hard to bring you more practical articles!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Database

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report