使用redis-cli与AOF迁移数据

使用redis-cli与AOF迁移数据

June 27, 2018

把一台Redis上的数据,迁移到另外一台Redis服务器上,有很多种方式。其中一个简便的方法是,利用Redis自带的命令行工具redis-cli,实现数据的无缝迁移。

在旧的Redis服务器上开启AOF

执行下面的命令,检查AOF是否开启。返回yes表示开启,no表示关闭

redis-cli -h old_redis_ip -p old_redis_port config get appendonly

如果未开启,执行下面的命令开启AOF

redis-cli -h old_redis_ip -p old_redis_port config set appendonly yes

复制AOF文件

默认生成的AOF文件名是appendonly.aof。到Redis数据目录可以看到appendonly.aof文件,复制该文件到新的Redis服务器上。

如果不知道Redis数据目录或者改过AOF的文件名,可以查看Redis的配置文件中的配置:

# The name of the append only file (default: "appendonly.aof")
appendfilename "appendonly.aof"


# The working directory.
#
# The DB will be written inside this directory, with the filename specified
# above using the 'dbfilename' configuration directive.
#
# The Append Only File will also be created inside this directory.
#
# Note that you must specify a directory here, not a file name.
dir /var/lib/redis

将AOF文件中的数据导入到新的Redis服务器

执行下面的命令,将AOF文件中的数据导入

redis-cli -h new_redis_ip -p new_redis_port --pipe < appendonly.aof

在旧的Redis服务器上关闭AOF

如果原有旧的Redis服务器不需要一直开启AOF,可通过以下命令关闭

redis-cli -h old_redis_ip -p old_redis_port config set appendonly no
最后更新于