在redis中将所有密钥从一个数据库复制到另一个数据库

ldioqlga  于 2022-10-31  发布在  Redis
关注(0)|答案(9)|浏览(478)

我想把我所有的键从一个特定的数据库复制到另一个数据库。如果是,那么怎么做?

ftf50wuq

ftf50wuq1#

如果你因为redis版本(2.6)而不能使用MIGRATE COPY,你可能想单独复制每个密钥,这会花费更长的时间,但不需要你登录到机器本身,并允许你从一个数据库移动数据到另一个数据库。


# set connection data accordingly

source_host=localhost
source_port=6379
source_db=0
target_host=localhost
target_port=6379
target_db=1

# copy all keys without preserving ttl!

redis-cli -h $source_host -p $source_port -n $source_db keys \* | while read key; do
    echo "Copying $key"
    redis-cli --raw -h $source_host -p $source_port -n $source_db DUMP "$key" \
        | head -c -1 \
        | redis-cli -x -h $target_host -p $target_port -n $target_db RESTORE "$key" 0
done

键不会被覆盖,要覆盖,请在复制之前删除这些键,或者在启动之前简单地刷新整个目标数据库。

b4qexyjb

b4qexyjb2#

将所有键从数据库编号0复制到本地主机上的数据库编号1。
redis-cli --scan | xargs redis-cli migrate localhost 6379 '' 1 0 copy keys
如果您使用相同的服务器/端口,您将收到超时错误,但密钥似乎无论如何都能成功复制。GitHub Redis issue #1903

0dxa2lsx

0dxa2lsx3#

redis-cli -a $source_password -p $source_port -h $source_ip keys /*| while read key; 
do echo "Copying $key"; 
redis-cli --raw -a $source_password -h $source_ip -p $source_port -n $dbname DUMP "$key"| head -c -1| redis-cli -x -a $destination_password -h $destination_IP -p $destination_port RESTORE "$key" 0;
92vpleto

92vpleto4#

我建议使用总是很方便的redis-rdb-tools包(来自Sripathi Krishnan)从普通的rdb转储中提取数据,并将其重新注入到另一个示例中。
请参阅https://github.com/sripathikrishnan/redis-rdb-tools

6mzjoqzu

6mzjoqzu5#

据我所知,您需要从一个特定的数据库(如5)复制密钥到一个特定的数据库,如10。如果是这种情况,您可以使用redis数据库转储程序https://github.com/r043v/rdd)。虽然根据文档,它有一个开关(-d)来选择一个数据库进行操作,但对我来说不起作用,所以我做了什么
1.)编辑rdd.c文件并查找int main(int argc,charargv)函数
2.)根据您的要求更改DB
3.)通过
make编译源代码
4.)使用
./rdd -o“保存.rdd”转储所有密钥
5.)再次编辑rdd.c文件并更改DB
6.)再次制造
7.)通过使用
./rdd“保存.rdd”-o插入-s“IP”-p“端口”**导入

qrjkbowd

qrjkbowd6#

我知道这是旧的,但对于那些你来到这里从谷歌:
我刚刚向npm和github发布了一个命令行界面实用程序,它允许您将与给定模式(甚至是 *)匹配的密钥从一个Redis数据库复制到另一个数据库。
您可以在此处找到该实用程序:
https://www.npmjs.com/package/redis-utils-cli

cnwbcb6i

cnwbcb6i7#

最新解决方案:

使用redislabs提供的RIOT开源命令行工具来复制数据。
参考:https://developer.redis.com/riot/riot-redis/cookbook.html#_performing_migration
Github项目链接:https://github.com/redis-developer/riot
如何安装:https://developer.redis.com/riot/riot-redis/


# Source redis db

SH=test1-redis.com
SP=6379

# Target redis db

TH=test1-redis.com
TP=6379 

# Copy from db0 to db1  (standalone redis db, Or cluster mode disabled)

# 

riot-redis -h $SH -p 6379 --db 0 replicate -h $TH -p 6379 --db 1 --batch 10000 \
--scan-count 10000 \
--threads 4 \
--reader-threads 4 \
--reader-batch 500 \
--reader-queue 2000 \
--reader-pool 4

RIOT速度更快,支持多线程,并能很好地与跨环境的Redis数据复制(AWS Elasticache,Redis OSS和Redislabs)等配合使用。

mf98qq94

mf98qq948#

尝试使用dump首先转储所有密钥,然后恢复相同的密钥

pgky5nke

pgky5nke9#

如果在同一个redis引擎内部迁移密钥,那么你可以使用内部命令MOVE(流水线操作可以提高速度):


# !/bin/bash

# set connection data accordingly

source_host=localhost
source_port=6379
source_db=4
target_db=0

total=$(redis-cli -n 4 keys \* | sed 's/^/MOVE /g' | sed 's/$/ '$target_db'/g' | wc -c)

# copy all keys without preserving ttl!

time redis-cli -h $source_host -p $source_port -n $source_db keys \* | \
  sed 's/^/MOVE /g' | sed 's/$/ 0/g' | \
  pv -s $total | \
  redis-cli -h $source_host -p $source_port -n $source_db >/dev/null

相关问题