1. 程式人生 > 資料庫 >Redis migrate資料遷移工具的使用教程

Redis migrate資料遷移工具的使用教程

前言

在工作中可能會遇到單點Redis向Redis叢集遷移資料的問題,但又不能老麻煩運維來做。為了方便研發自己遷移資料,我這裡寫了一個簡單的Redis遷移工具,希望對有需要的人有用。

本工具支援:

  • 單點Redis到單點Redis遷移
  • 單點Redis到Redis叢集遷移
  • Redis叢集到Redis叢集遷移
  • Redis叢集到單點Redis遷移

該工具已經編譯成了多平臺命令,直接從Github下載二進位制檔案執行就好了。

專案地址: https://github.com/icowan/redis-tool

把程式碼拉下來之後直接執行命令 make 就可以編譯多個平臺可執行檔案,需要依賴golang編譯器。

  • Windows amd64: redis-tool-windows-amd64.exe
  • MacOS amd64: redis-tool-darwin-amd64
  • Linux amd64: redis-tool-linux-amd64
  • Linux arm64: redis-tool-linux-arm64

檢視使用方法:

$ chmod a+x redis-tool-linux-amd64
$ ./redis-tool-linux-amd64 -h

支援的資料型別

  • string 字串
  • hash 雜湊列表
  • list 列表
  • sorted-set 有序集合

如何使用

下載好命令並授權之後執行 ./redis-tool-linux-amd64 -h 可以檢視該工具所支援的所有功能:

$ ./redis-tool-darwin-amd64 migrate -h
資料遷移命令

Usage:
redis-tool migrate [command]

Examples:

支援命令:
[hash,set,sorted-set,list]


Available Commands:
all  遷移所有
hash 雜湊列表遷移
list 列表遷移
 set  redis set 遷移
sorted-set 有序集合遷移

Flags:
 -h,--help   help for migrate
 --source-auth string 源密碼
 --source-database int 源database
 --source-hosts string 源redis地址,多個ip用','隔開 (default "127.0.0.1:6379")
 --source-prefix string 源redis字首
 --source-redis-cluster 源redis是否是叢集
 --target-auth string 目標密碼
 --target-database int 目標database
 --target-hosts string 目標redis地址,'隔開 (default "127.0.0.1:6379")
 --target-prefix string 目標redis字首
 --target-redis-cluster 目標redis是否是叢集

Use "redis-tool migrate [command] --help" for more information about a command.

引數說明:

  • --source-auth: 源redis密碼,如果有的話就填
  • --source-database: 源database,預設是 0
  • --source-hosts: 源redis地址,叢集的多個ip用','隔開 (default "127.0.0.1:6379")
  • --source-prefix: 源redis字首,可不填
  • --source-redis-cluster: 源redis是否是叢集,預設 false
  • --target-auth: 遷移目標redis密碼,如果有的話就填
  • --target-database: 遷移目標database,預設是 0
  • --target-hosts: 遷移目標redis地址,'隔開 (default "127.0.0.1:6379")
  • --target-prefix: 遷移目標redis字首,可不填
  • --target-redis-cluster: 遷移目標redis是否是叢集,預設 false

遷移單個key的資料

下面就舉兩個例子吧,其他的都差不太多。

Hash型別

可以通過命令 redis-tool migrate hash -h 檢視使用說明

$ redis-tool migrate hash helloworld \
 --source-hosts 127.0.0.1:6379 \
 --target-redis-cluster true \
 --target-hosts 127.0.0.1:6379,127.0.0.1:7379 \
 --target-auth 123456

Redis migrate資料遷移工具的使用教程

有序集合

可以通過命令 redis-tool migrate sorted-set -h 檢視使用說明

有序集合的資料量可能會比較大,所以這裡按 50000 為單位進行了切割。我這裡測試過遷移近17000000萬條的資料,用時40多分鐘。

$ redis-tool migrate hash helloworld \
 --source-hosts 127.0.0.1:6379 \
 --target-redis-cluster true \
 --target-hosts 127.0.0.1:6379,127.0.0.1:7379 \
 --target-auth 123456

Redis migrate資料遷移工具的使用教程

遷移所有key的資料支援萬用字元過濾

可以通過命令 redis-tool migrate all -h 檢視使用說明

$ redis-tool migrate all "ipdetect:*" \ 
 --source-hosts 127.0.0.1:6379 \
 --target-redis-cluster true \
 --target-hosts 127.0.0.1:6379,127.0.0.1:7379 \
 --target-auth 123456

這個命令會編譯匹配到的所有型別的key,再根據key的型別進行逐步遷移。

尾巴

使用golang寫的一個比較簡單的工具,主要用於在Redis沒有持久化或多套Redis向一套Redis遷移的情況下使用。

總結

到此這篇關於Redis migrate資料遷移工具的文章就介紹到這了,更多相關Redis migrate資料遷移工具內容請搜尋我們以前的文章或繼續瀏覽下面的相關文章希望大家以後多多支援我們!