Quickly implement MySQL migration to Redis

Source: Internet
Author: User

A simple and fast migration of MySQL data to Redis and stored in hash, data storage needs to comply with Redis Communication protocol (official connection Http://redis.io/topics/mass-insert),

Such as:

CR LF//three fields

$ CR LF//First field ' Set ' has 3 bytes

Set CR LF//field content set

$4 CR LF//second field Key1 has 4 bytes

Key1 CR LF//Field two content MyKey

$4 CR LF//The third field has 4 bytes

Val1 CR LF//Field three content Val1

Result: Set Key1 val1


The cases are as follows:

CREATE TABLE Events_all_time (

ID INT (one) UNSIGNED not NULL auto_increment,

ACTION VARCHAR (255) is not NULL,

COUNT INT (one) is not NULL DEFAULT 0,

PRIMARY KEY (ID),

UNIQUE KEY uniq_action (action)

);

# #数据自己添加到表

REDIS Storage structure:

1.hset events_all_time ACTION COUNT # #这里设置的键值方面于数据库对应起来

2, write variables to save to the file Abc.sql


SELECT CONCAT (

"*4\r\n", # # #表示redis命令有4个字段包括命令和参数 (table field)

' $ ', LENGTH (redis_cmd), ' \ r \ n ',

Redis_cmd, ' \ r \ n ',

' $ ', LENGTH (redis_key), ' \ r \ n ',

Redis_key, ' \ r \ n ',

' $ ', LENGTH (hkey), ' \ r \ n ',

HKEY, ' \ r \ n ',

' $ ', LENGTH (hval), ' \ r \ n ',

Hval, ' \ r'# # #最后一行没有 \ n

)

From (

SELECT

' Hset ' as Redis_cmd,

'events_all_time' as Redis_key, # #这里用表名作为redis中的field名

Action as hkey,# #数据库字段

Count as Hval

From Events_all_time

) as T


3. Execution: /usr/local/mysql/bin/mysql-h192.168.1.195-udlan-p log--skip-column-names--raw <abc.sql | Redis-cli-h 192.168.1.130--pipe

Important Parameter Description:

--raw: Make MySQL do not convert the line break in field values.

--skip-column-names: Do not include column names in each line of MySQL output


This article is from the "Dbaspace" blog, make sure to keep this source http://dbaspace.blog.51cto.com/6873717/1982357

Quickly implement MySQL migration to Redis

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.