1. 升级前准备
在升级Redis之前,需要进行充分的准备工作,包括检查当前版本、备份数据、检查硬件环境等。更多学习教程www.fgedu.net.cn
# /redis/bin/redis-server –version
Redis server v=6.0.16 sha=00000000:0 malloc=jemalloc-5.1.0 bits=64 build=abcdef1234567890
# 检查硬件环境
# free -h
total used free shared buff/cache available
Mem: 64G 2.1G 60G 8.5M 1.8G 61G
Swap: 32G 0B 32G
# 检查磁盘空间
# df -h /redis
Filesystem Size Used Avail Use% Mounted on
/dev/sdb1 500G 50G 450G 10% /redis
– 确保有足够的磁盘空间用于备份和安装新版本
– 确保内存足够,Redis 7.0对内存的使用可能会有所增加
– 安排在业务低峰期进行升级
– 提前通知相关业务方,做好升级计划
2. 数据备份
在升级之前,必须对Redis数据进行备份,以防止升级过程中出现问题导致数据丢失。学习交流加群风哥微信: itpux-com
# mkdir -p /backup/redis/upgrade
# 执行备份
# /redis/bin/redis-cli -a fgedudb123 save
OK
# 复制备份文件
# cp /redis/data/dump.rdb /backup/redis/upgrade/dump-$(date +”%Y%m%d%H%M%S”).rdb
# cp /redis/data/appendonly.aof /backup/redis/upgrade/appendonly-$(date +”%Y%m%d%H%M%S”).aof
# 检查备份文件
# ls -la /backup/redis/upgrade/
total 10240
-rw-r–r– 1 root root 5242880 Mar 31 10:00 dump-20260331100000.rdb
-rw-r–r– 1 root root 5242880 Mar 31 10:00 appendonly-20260331100000.aof
3. 下载新版本
从Redis官方网站下载最新版本的Redis 7.0。
# cd /tmp
# wget https://download.redis.io/releases/redis-7.0.8.tar.gz
# 解压源码包
# tar -zxvf redis-7.0.8.tar.gz
# 检查解压结果
# ls -la redis-7.0.8/
total 2048
drwxr-xr-x 6 root root 4096 Mar 31 10:05 redis-7.0.8
-rw-r–r– 1 root root 1048576 Mar 31 10:05 redis-7.0.8.tar.gz
4. 停止服务
在安装新版本之前,需要停止当前运行的Redis服务。
# systemctl stop redis
# 检查服务状态
# systemctl status redis
● redis.service – Redis Server
Loaded: loaded (/etc/systemd/system/redis.service; enabled; vendor preset: disabled)
Active: inactive (dead) since Mon 2026-03-31 10:10:00 CST; 1min ago
Process: 12345 ExecStop=/redis/bin/redis-cli -a fgedudb123 shutdown (code=exited, status=0/SUCCESS)
Main PID: 12345 (code=exited, status=0/SUCCESS)
5. 安装新版本
编译并安装Redis 7.0版本。
# cd /tmp/redis-7.0.8
# 编译
# make
# 安装到指定目录
# make install PREFIX=/redis_new
# 检查安装结果
# ls -la /redis_new/bin/
total 38400
drwxr-xr-x 2 root root 4096 Mar 31 10:15 .
drwxr-xr-x 3 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
6. 配置文件迁移
将旧版本的配置文件迁移到新版本,并根据Redis 7.0的新特性进行优化。学习交流加群风哥QQ113257174
# cp /redis/conf/redis.conf /redis_new/conf/
# 编辑配置文件,添加Redis 7.0的新特性
# vi /redis_new/conf/redis.conf
# 添加以下配置
# Redis 7.0新特性:ACL
aclfile /redis_new/conf/users.acl
# Redis 7.0新特性:细粒度的内存管理
maxmemory-samples 10
# Redis 7.0新特性:客户端跟踪
client-output-buffer-limit normal 0 0 0
client-output-buffer-limit replica 256mb 64mb 60
client-output-buffer-limit pubsub 32mb 8mb 60
7. 启动服务
使用新版本的Redis启动服务。
# vi /etc/systemd/system/redis.service
[Unit]
Description=Redis Server
After=network.target
[Service]
Type=forking
PIDFile=/redis_new/run/redis_6379.pid
ExecStart=/redis_new/bin/redis-server /redis_new/conf/redis.conf
ExecStop=/redis_new/bin/redis-cli -a fgedudb123 shutdown
Restart=on-failure
[Install]
WantedBy=multi-user.target
# 重新加载系统服务
# systemctl daemon-reload
# 启动Redis服务
# systemctl start redis
# 检查服务状态
# systemctl status redis
● redis.service – Redis Server
Loaded: loaded (/etc/systemd/system/redis.service; enabled; 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_new/bin/redis-server 192.168.1.51:6379
8. 升级验证
升级完成后,需要验证Redis服务是否正常运行。
# /redis_new/bin/redis-cli -h 192.168.1.51 -p 6379 -a fgedudb123
# 测试命令
192.168.1.51:6379> ping
PONG
192.168.1.51:6379> set test “Hello Redis 7.0”
OK
192.168.1.51:6379> get test
“Hello Redis 7.0”
192.168.1.51:6379> info server
# Server
redis_version:7.0.8
redis_git_sha1:00000000
redis_git_dirty:0
redis_build_id:1234567890abcdef
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_new/bin/redis-server
config_file:/redis_new/conf/redis.conf
192.168.1.51:6379> exit
9. 性能调优
Redis 7.0引入了一些新的性能特性,需要进行相应的调优。更多学习教程公众号风哥教程itpux_com
# vi /redis_new/conf/redis.conf
# Redis 7.0性能调优
# 内存管理优化
maxmemory 48gb
maxmemory-policy allkeys-lru
maxmemory-samples 10
# 网络优化
tcp-keepalive 300
# 持久化优化
appendonly yes
appendfsync everysec
no-appendfsync-on-rewrite yes
# 客户端优化
client-output-buffer-limit normal 0 0 0
client-output-buffer-limit replica 256mb 64mb 60
client-output-buffer-limit pubsub 32mb 8mb 60
# 线程优化
io-threads 4
io-threads-do-reads yes
– 根据服务器CPU核心数调整io-threads参数
– 启用io-threads-do-reads以提高读取性能
– 合理设置maxmemory和maxmemory-policy
– 定期监控Redis的内存使用情况
本文由风哥教程整理发布,仅用于学习测试使用,转载注明出处:http://www.fgedu.net.cn/10327.html
