1. 首页 > 软件安装教程 > 正文

Redis迁移-Redis 6.0迁移到新服务器详细过程

1. 迁移前准备

在进行Redis迁移之前,需要进行充分的准备工作,包括规划迁移时间、准备目标服务器、备份数据等。更多学习教程www.fgedu.net.cn

生产环境建议:
– 安排在业务低峰期进行迁移
– 提前通知相关业务方,做好迁移计划
– 确保目标服务器的硬件配置不低于源服务器
– 准备足够的网络带宽用于数据传输
– 制定回滚方案,以防迁移失败

2. 源服务器检查

检查源服务器的Redis版本、数据大小、配置文件等信息。

# 检查源服务器Redis版本
# ssh root@192.168.1.51 “/redis/bin/redis-server –version”
Redis server v=6.0.16 sha=00000000:0 malloc=jemalloc-5.1.0 bits=64 build=abcdef1234567890

# 检查源服务器Redis状态
# ssh root@192.168.1.51 “/redis/bin/redis-cli -a fgedudb123 info server”
# Server
redis_version:6.0.16
redis_git_sha1:00000000
redis_git_dirty:0
redis_build_id:abcdef1234567890
redis_mode:standalone
os:Linux 4.19.90-24.4.v2101.ky10.x86_64 x86_64
arch_bits:64
multiplexing_api:epoll
gcc_version:7.3.0
process_id:12345
process_supervised:no
run_id:1234567890abcdef1234567890abcdef12345678
tcp_port:6379
server_time_usec:1680270000000000
uptime_in_seconds:86400
uptime_in_days:1
hz:10
executable:/redis/bin/redis-server
config_file:/redis/conf/redis.conf

# 检查源服务器数据大小
# ssh root@192.168.1.51 “du -sh /redis/data/”
50G /redis/data/

# 检查源服务器配置文件
# ssh root@192.168.1.51 “cat /redis/conf/redis.conf”

3. 目标服务器准备

准备目标服务器,包括操作系统安装、网络配置、目录创建等。学习交流加群风哥微信: itpux-com

# 检查目标服务器硬件环境
# ssh root@192.168.1.52 “free -h”
total used free shared buff/cache available
Mem: 64G 2.1G 60G 8.5M 1.8G 61G
Swap: 32G 0B 32G

# 检查目标服务器磁盘空间
# ssh root@192.168.1.52 “df -h”
Filesystem Size Used Avail Use% Mounted on
devtmpfs 32G 0 32G 0% /dev
tmpfs 32G 0 32G 0% /dev/shm
tmpfs 32G 8.5M 32G 1% /run
tmpfs 32G 0 32G 0% /sys/fs/cgroup
/dev/sda1 50G 15G 36G 30% /
/dev/sdb1 500G 20G 480G 4% /redis

# 在目标服务器创建目录
# ssh root@192.168.1.52 “mkdir -p /redis/{bin,conf,data,log,run}”
# ssh root@192.168.1.52 “chmod 755 /redis/{bin,conf,data,log,run}”

4. 数据备份

在迁移之前,对源服务器的Redis数据进行备份。

# 在源服务器创建备份目录
# ssh root@192.168.1.51 “mkdir -p /backup/redis/migration”

# 执行备份
# ssh root@192.168.1.51 “/redis/bin/redis-cli -a fgedudb123 save”
OK

# 复制备份文件
# ssh root@192.168.1.51 “cp /redis/data/dump.rdb /backup/redis/migration/dump-$(date +”%Y%m%d%H%M%S”).rdb”
# ssh root@192.168.1.51 “cp /redis/data/appendonly.aof /backup/redis/migration/appendonly-$(date +”%Y%m%d%H%M%S”).aof”

# 检查备份文件
# ssh root@192.168.1.51 “ls -la /backup/redis/migration/”
total 102400
-rw-r–r– 1 root root 52428800 Mar 31 10:00 dump-20260331100000.rdb
-rw-r–r– 1 root root 52428800 Mar 31 10:00 appendonly-20260331100000.aof

5. 数据传输

将备份文件传输到目标服务器。

# 传输备份文件到目标服务器
# scp root@192.168.1.51:/backup/redis/migration/dump-20260331100000.rdb root@192.168.1.52:/redis/data/dump.rdb
# scp root@192.168.1.51:/backup/redis/migration/appendonly-20260331100000.aof root@192.168.1.52:/redis/data/appendonly.aof

# 传输配置文件到目标服务器
# scp root@192.168.1.51:/redis/conf/redis.conf root@192.168.1.52:/redis/conf/

# 检查传输结果
# ssh root@192.168.1.52 “ls -la /redis/data/”
total 102400
-rw-r–r– 1 root root 52428800 Mar 31 10:10 dump.rdb
-rw-r–r– 1 root root 52428800 Mar 31 10:10 appendonly.aof

6. 目标服务器安装Redis

在目标服务器安装Redis 6.0版本。

# 下载Redis 6.0.16
# ssh root@192.168.1.52 “cd /tmp && wget https://download.redis.io/releases/redis-6.0.16.tar.gz”

# 解压源码包
# ssh root@192.168.1.52 “cd /tmp && tar -zxvf redis-6.0.16.tar.gz”

# 编译并安装
# ssh root@192.168.1.52 “cd /tmp/redis-6.0.16 && make && make install PREFIX=/redis”

# 检查安装结果
# ssh root@192.168.1.52 “ls -la /redis/bin/”
total 38400
drwxr-xr-x 2 root root 4096 Mar 31 10:15 .
drwxr-xr-x 6 root root 4096 Mar 31 10:15 ..
-rwxr-xr-x 1 root root 10090816 Mar 31 10:15 redis-benchmark
-rwxr-xr-x 1 root root 10414864 Mar 31 10:15 redis-check-aof
-rwxr-xr-x 1 root root 10414864 Mar 31 10:15 redis-check-rdb
-rwxr-xr-x 1 root root 5007440 Mar 31 10:15 redis-cli
lrwxrwxrwx 1 root root 12 Mar 31 10:15 redis-sentinel -> redis-server
-rwxr-xr-x 1 root root 10414864 Mar 31 10:15 redis-server

风哥提示:确保目标服务器安装的Redis版本与源服务器相同,以避免兼容性问题。

7. 数据恢复

在目标服务器恢复Redis数据。学习交流加群风哥QQ113257174

# 修改目标服务器配置文件中的IP地址
# ssh root@192.168.1.52 “sed -i ‘s/bind 192.168.1.51/bind 192.168.1.52/g’ /redis/conf/redis.conf”

# 检查配置文件
# ssh root@192.168.1.52 “grep bind /redis/conf/redis.conf”
bind 192.168.1.52

# 设置目录权限
# ssh root@192.168.1.52 “chown -R root:root /redis”

8. 启动服务

在目标服务器启动Redis服务。

# 在目标服务器创建系统服务文件
# ssh root@192.168.1.52 “cat > /etc/systemd/system/redis.service << EOF [Unit] Description=Redis Server After=network.target [Service] Type=forking PIDFile=/redis/run/redis_6379.pid ExecStart=/redis/bin/redis-server /redis/conf/redis.conf ExecStop=/redis/bin/redis-cli -a fgedudb123 shutdown Restart=on-failure [Install] WantedBy=multi-user.target EOF" # 重新加载系统服务 # ssh root@192.168.1.52 "systemctl daemon-reload" # 启动Redis服务 # ssh root@192.168.1.52 "systemctl start redis" # 检查服务状态 # ssh root@192.168.1.52 "systemctl status redis" ● redis.service - Redis Server Loaded: loaded (/etc/systemd/system/redis.service; disabled; vendor preset: disabled) Active: active (running) since Mon 2026-03-31 10:20:00 CST; 1min ago Main PID: 67890 (redis-server) Tasks: 4 Memory: 100.0M CGroup: /system.slice/redis.service └─67890 /redis/bin/redis-server 192.168.1.52:6379 # 设置开机自启 # ssh root@192.168.1.52 "systemctl enable redis" Created symlink /etc/systemd/system/multi-user.target.wants/redis.service → /etc/systemd/system/redis.service.

9. 迁移验证

验证目标服务器的Redis服务是否正常运行,数据是否完整。

# 连接目标服务器的Redis
# ssh root@192.168.1.52 “/redis/bin/redis-cli -h 192.168.1.52 -p 6379 -a fgedudb123”

# 测试命令
192.168.1.52:6379> ping
PONG

192.168.1.52:6379> set test “Hello Redis Migration”
OK

192.168.1.52:6379> get test
“Hello Redis Migration”

192.168.1.52:6379> info server
# Server
redis_version:6.0.16
redis_git_sha1:00000000
redis_git_dirty:0
redis_build_id:abcdef1234567890
redis_mode:standalone
os:Linux 4.19.90-24.4.v2101.ky10.x86_64 x86_64
arch_bits:64
multiplexing_api:epoll
gcc_version:7.3.0
process_id:67890
process_supervised:no
run_id:1234567890abcdef1234567890abcdef12345678
tcp_port:6379
server_time_usec:1680270000000000
uptime_in_seconds:360
uptime_in_days:0
hz:10
executable:/redis/bin/redis-server
config_file:/redis/conf/redis.conf

192.168.1.52:6379> dbsize
(integer) 10000

192.168.1.52:6379> exit

10. 业务切换

将业务流量从源服务器切换到目标服务器。更多学习教程公众号风哥教程itpux_com

# 停止源服务器的Redis服务
# ssh root@192.168.1.51 “systemctl stop redis”

# 更新应用配置,将Redis地址指向目标服务器
# 例如,修改应用配置文件中的Redis地址
# vi /app/config/application.yml
# redis:
# host: 192.168.1.52
# port: 6379

# 重启应用服务
# systemctl restart app

# 验证应用是否正常访问Redis
# curl http://localhost:8080/redis-test
{“status”:”success”,”message”:”Redis connection successful”}

生产环境建议:
– 切换业务流量前,确保目标服务器的Redis服务正常运行
– 切换过程中,密切监控应用的运行状态
– 保留源服务器的Redis服务一段时间,以防需要回滚
– 切换完成后,验证所有业务功能是否正常

风哥提示:Redis迁移过程中,数据一致性是关键,建议在业务低峰期进行,并确保有完整的备份和回滚方案。from:www.itpux.com

本文由风哥教程整理发布,仅用于学习测试使用,转载注明出处:http://www.fgedu.net.cn/10327.html

联系我们

在线咨询:点击这里给我发消息

微信号:itpux-com

工作日:9:30-18:30,节假日休息