Centos7升级redis

  1. 1. 说明
  2. 2. 实施
    1. 2.1. redis 编译安装
      1. 2.1.1. 下载 redis 源码包
      2. 2.1.2. 停止备份旧服务
      3. 2.1.3. 编译 redis 安装
      4. 2.1.4. 创建 redis 软链接
      5. 2.1.5. 检查 redis 版本
      6. 2.1.6. 创建 redis 账号
      7. 2.1.7. 创建 redis 系统服务

说明

为了解决redis 4.x到5.x 主从复制漏洞修复将redis升级至6.x

实施

redis 编译安装

下载 redis 源码包

redis源码站

1
2
3
4

wget https://download.redis.io/releases/redis-6.2.9.tar.gz
tar -xf redis-6.2.9.tar.gz
pushd redis-6.2.9 > /dev/null

停止备份旧服务

1
2
systemctl stop redis
mv /usr/local/redis /usr/local/redis.old

编译 redis 安装

PREFIX用于指定安装目录

1
2
3
4
5
6
7
8
9
10

make PREFIX=/usr/local/redis install

mkdir -p /usr/local/redis/etc
mkdir -p /usr/local/redis/var

cp redis.conf /usr/local/redis/etc/redis-template.conf
cp /usr/local/redis.old/etc/redis.conf /usr/local/redis/etc/
chown redis.redis -R /usr/local/redis/

redis-template.conf 为了进行参数对照

创建 redis 软链接

1
2
3
4
5
6
ln -sf /usr/local/redis/bin/redis-server /usr/local/bin/redis-server
ln -sf /usr/local/redis/bin/redis-benchmark /usr/local/bin/redis-benchmark
ln -sf /usr/local/redis/bin/redis-check-aof /usr/local/bin/redis-check-aof
ln -sf /usr/local/redis/bin/redis-check-rdb /usr/local/bin/redis-check-rdb
ln -sf /usr/local/redis/bin/redis-cli /usr/local/bin/redis-cli
ln -sf /usr/local/redis/bin/redis-sentinel /usr/local/bin/redis-sentinel

检查 redis 版本

1
2
3
redis-server --version
popd > /dev/null
rm -rf redis-6.2.9
扩展

创建 redis 账号

1
2
id -u redis >/dev/null 2>&1
[ $? -ne 0 ] && useradd -M -s /sbin/nologin redis

创建 redis 系统服务

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
tee /lib/systemd/system/redis-server.service > /dev/null << 'EOF'
[Unit]
Description=Redis In-Memory Data Store
After=network.target

[Service]
Type=forking
PIDFile=/var/run/redis/redis.pid
User=redis
Group=redis

Environment=statedir=/var/run/redis
PermissionsStartOnly=true
ExecStartPre=/bin/mkdir -p ${statedir}
ExecStartPre=/bin/chown -R redis:redis ${statedir}
ExecStart=/usr/local/redis/bin/redis-server /usr/local/redis/etc/redis.conf
ExecStop=/bin/kill -s TERM $MAINPID
Restart=always
LimitNOFILE=1000000
LimitNPROC=1000000
LimitCORE=1000000

[Install]
WantedBy=multi-user.target

EOF

systemctl daemon-reload

systemctl start redis-server

systemctl enable redis-server