One step to complete MySQL migration to Redis

Source: Internet
Author: User
Tags bulk insert mysql query

When migrating a large table from MySQL to Redis, you may find that every time you extract, convert, and import a single piece of data, it's unbearable to be slow! Here's a tip that you can use the pipeline to input MySQL output directly to the REDIS-CLI input, which allows two databases to run at their top speed.

Using this technique, I've reduced the time it takes to import 8 million MySQL data into Redis from 90 minutes to two minutes.

MySQL-to-redis data protocol

The REDIS-CLI command-line tool has a bulk insert mode that is specifically designed for batch execution commands. The first step is to format the contents of the MySQL query into REDIS-CLI available data formats. Here we go!

My Table of statistics:

  1. CREATE TABLE events_all_time (
  2. ID int (one) unsigned not NULL auto_increment,
  3. Action varchar (255) is not NULL,
  4. Count Int (one) not NULL DEFAULT 0,
  5. PRIMARY KEY (id),
  6. UNIQUE KEY uniq_action (action)
  7. );

The Redis commands that are prepared to execute in each row of data are as follows:

    1. hset Events_all_time [action] [count]

Follow the Redis command rules above to create a events_to_redis.sql file that is used to generate the SQL for the Redis Data protocol format:

  1. --Events_to_redis.sql  
  2. SELECT CONCAT (
  3. "*4\r\n",
  4. ' $ ', LENGTH (redis_cmd) , ' \ r \ n ',
  5. Redis_cmd, ' \ r \ n ' ,  
  6. ' $ ', LENGTH (Redis_key) , ' \ r \ n ',
  7. Redis_key, ' \ r \ n ' ,  
  8. ' $ ', LENGTH (hkey) , ' \ r \ n ',
  9. hkey, ' \ r \ n ' ,  
  10. ' $ ', LENGTH (hval) , ' \ r \ n ',
  11. Hval, ' \ r '  
  12. )
  13. from  (  
  14. SELECT
  15. ' hset ' as Redis_cmd,
  16. ' events_all_time ' as Redis_key,
  17. action as hkey,
  18. count as hval
  19. from events_all_time
  20. ) as t

OK, execute with the following command:

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

Important MySQL Parameter description:

--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.

Redis Data Bulk Import Export

one way to do this is to export and import Redis data from one server to a new server for work, but he needs to install the Ruby environment, and there may be unexpected errors during the installation of the REDIS-DUMP environment. So we have to choose other methods. A moment is a few ideas for reference.

1, data export, do not write their own, and do not use third-party scripts,
echo "Hgetall xxx" | redis-cli-h localhost-p 6379
echo "Hgetall xxx" | redis-cli-h localhost-p 6379 >> wordlist.raw
2. So get the result that you can go up to the server to view the Wordlist.raw file
3. Organize the file format into the input required
$f = ' xx.oo ';
$lines = 0;
$inkey = 0;
$rawfile = ' xx.oo ';
$hashkey = ' xx ';
$buff = ";
$fp = fopen ($rawfile, ' W ');
$fps = fopen ($f, ' R ');
While ($line = fgets ($fps)) {
$inkey =! $inkey;
if ($inkey) {
$f = ' Bayes_wordlist.raw ';
$lines = 0;
$inkey = 0;
$rawfile = ' Bayes_wordlist.3.raw ';
$hashkey = ' bayes_wordlist ';
$buff = ";
$fp = fopen ($rawfile, ' W ');
$fps = fopen ($f, ' R ');
While ($line = fgets ($fps)) {
$inkey =! $inkey;
if ($inkey) {
$line = sprintf (' "%s" ', Trim ($line));
$buff = "Hset $hashkey". Trim ($line);
  }
Else
 {
$buff. = ' "'. Trim ($line). '";
fwrite ($fp, $buff. " \ r \ n ");
 }
}
$buff = "Hset $hashkey". Trim ($line);
 }
Else
 {
$buff. = ' "'. Trim ($line). '";
fwrite ($fp, $buff. " \ r \ n ");
 }
}
If you choose which library to write to select X in the first row
4. Using REDIS-CLI for import
echo ' Date ' > Pipe.log && cat xx.oo | redis-cli >> pipe.log && echo ' Date ' >> pipe.log< /c5>
5. Add a time record, and record the import results, the actual execution of the imported statement is
Cat Wordlist.raw | redis-cli, of course, strict redis-cli also add-h localhost-p 6379 parameters.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.