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

MySQL quickly migrates to Redis method

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

Share

Shulou(Shulou.com)06/01 Report--

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

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