Get the App
SLTechnology News&Howtos  ›  Database  › 

MySQL quickly migrates to Redis method

Shulou Source: shulou.com Published: 2022-06-01 18:43:20 09月20日 Update

This article mainly introduces the rapid migration of MySQL to Redis method, the content of the article is carefully selected and edited by the author, with a certain pertinence, for everyone's reference significance is still relatively great, the following with the author to understand the rapid migration of MySQL to Redis method.

A simple and fast way to migrate MySQL data to Redis and store it in hash mode, the data storage needs to follow the communication protocol of redis (officially connect to http://redis.io/topics/mass-insert)

Such as:

* 3 CR LF / / three fields

$3 CR LF / / the first field 'set' has 3 bytes

SET CR LF / / Field content set

$4 CR LF / / the 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

Results: set key1 val1

The examples are as follows:

CREATE TABLE events_all_time (

Id INT (11) UNSIGNED NOT NULL AUTO_INCREMENT

ACTION VARCHAR (255) NOT NULL

COUNT INT (11) NOT NULL DEFAULT 0

PRIMARY KEY (id)

UNIQUE KEY uniq_action (ACTION)

);

# # add data to the table by yourself

Redis storage structure:

1. Hset events_all_time ACTION COUNT # # the key values set here correspond to the database

2. Write variables and save them in the file abc.sql

SELECT CONCAT (

"* 4\ r\ n", # indicates that the redis command has four fields including commands and parameters (table fields)

'$', 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' # No on the last line\ n

)

FROM (

SELECT

'HSET' as redis_cmd

'events_all_time' AS redis_key, # # here use the table name as the field name in redis

Action AS hkey, # # database field

Count AS hval

FROM events_all_time

) AS t

3. Execute: / usr/local/mysql/bin/mysql-h292.168.1.195-udlan-p log-- skip-column-names-- raw

Tags: Field data content method byte storage three parameter command database author industry important one line information variable masterpiece official meaning newline character Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Docker MySQL Apple Huawei Microsoft