Write by lyc at 2021-7-30
Redis Download
redis 6.0.x简介和安装

CentOS7 编译安装 Redis-6.0.15

1.安装高版本 gcc

由于 redis 6.0 版本以上编译安装需要使用 5.3 以上的 gcc,而Linux默认 gcc 版本4.8.X

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
# 查看默认gcc版本号
$ gcc -v
....
gcc version 4.8.5 20150623 (Red Hat 4.8.5-44) (GCC)

# 安装scl源
yum install -y centos-release-scl scl-utils-build

# 安装9版本的gcc、gcc-c++、gdb工具链
yum install -y devtoolset-9-toolchain

# 启用gcc高版本
$ scl enable devtoolset-9 bash
$ gcc -v
....
gcc version 9.3.1 20200408 (Red Hat 9.3.1-2) (GCC) # 已成功切换gcc版本

注意:scl 命令启用只是临时的,退出shell或重启就会恢复原系统gcc版本。

1
2
# 设置版本长期生效(按需设置)
echo "source /opt/rh/devtoolset-9/enable" >> /etc/profile

2.编译安装 Redis-6.0.15

配置内核参数

1
2
3
4
echo 'net.core.somaxconn = 1024' >> /etc/sysctl.conf
echo 'vm.overcommit_memory= 1 ' >> /etc/sysctl.conf
echo madvise > /sys/kernel/mm/transparent_hugepage/enabled
echo 'echo madvise > /sys/kernel/mm/transparent_hugepage/enabled' >> /etc/rc.local

编译安装

1
2
3
4
5
6
7
8
wget https://download.redis.io/releases/redis-6.0.15.tar.gz
tar xzf redis-6.0.15.tar.gz && cd redis-6.0.15
make PREFIX=/usr/local/redis-6.0.15 install
ln -s /usr/local/redis-6.0.15 /usr/local/redis

# 添加环境变量
echo 'export PATH=/usr/local/redis/bin:$PATH' >> /etc/profile
source /etc/profile

创建6379实例目录

1
2
3
4
mkdir -p /usr/local/redis/{var,conf}
mkdir -p /var/log/redis
mkdir -p /data/redis_data/6379
mkdir -p /data/redis_log

拷贝默认配置文件

1
2
# 默认配置文件(可选)
cp /usr/local/src/redis-6.0.15/redis.conf /usr/local/redis-6.0.15/conf/redis-6379.conf
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
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
$ vim /usr/local/redis/conf/redis-6379.conf
daemonize yes
pidfile /var/run/redis_6379.pid
port 6379
bind 0.0.0.0
timeout 0
loglevel notice
logfile /data/redis_log/redis-6379.log
databases 32
#save 900 1
save 300 100
#save 60 10000
stop-writes-on-bgsave-error yes
rdbcompression yes
rdbchecksum yes
dbfilename dump-6379.rdb
dir /data/redis_data/6379
maxclients 10000
maxmemory 10GB
maxmemory-policy volatile-lru
maxmemory-samples 3

appendonly yes
appendfsync everysec
appendfilename appendonly-6379.aof
no-appendfsync-on-rewrite yes
auto-aof-rewrite-percentage 100
auto-aof-rewrite-min-size 64mb

lua-time-limit 5000
slowlog-log-slower-than 10000
slowlog-max-len 1024

## Special encoding of small aggregate data types
hash-max-ziplist-entries 512
hash-max-ziplist-value 64
list-max-ziplist-entries 512
list-max-ziplist-value 64
set-max-intset-entries 512
zset-max-ziplist-entries 128
zset-max-ziplist-value 64

activerehashing yes
client-output-buffer-limit normal 0 0 0
client-output-buffer-limit slave 256mb 64mb 60
client-output-buffer-limit pubsub 32mb 8mb 60

slave-serve-stale-data yes
slave-read-only no
slave-priority 100

requirepass ''

3.配置 systemd 启动脚本

1
2
3
4
5
6
7
8
9
10
11
12
$ vim /etc/systemd/system/redis_6379.service
[Unit]
Description=Redis
After=network.target

[Service]
ExecStart=/usr/local/redis/bin/redis-server /usr/local/redis/conf/redis-6379.conf --daemonize no
ExecStop=/usr/local/redis/bin/redis-cli -h 127.0.0.1 -p 6379 shutdown
LimitNOFILE=65535

[Install]
WantedBy=multi-user.target

启动

1
2
3
systemctl daemon-reload
systemctl enable redis_6379.service
systemctl start redis_6379.service