孤九沫
发布于 2026-04-27 / 7 阅读
0
0

redis

一、下载地址

官方下载地址:https://download.redis.io/releases/

二、安装部署步骤

1. 下载安装包

执行命令下载Redis 6.2.18安装包:

wget https://download.redis.io/releases/redis-6.2.18.tar.gz

2. 解压缩并进入目录

tar zxvf redis-6.2.18.tar.gz
cd redis-6.2.18

3. 创建Redis用户

useradd -r -s /sbin/nologin -d /var/lib/redis redis

4. 编译并安装

yum install systemd-devel
make
make install PREFIX=/usr/local/redis USE_SYSTEMD=yes

5. 添加环境变量

echo "export PATH=/usr/local/redis/bin:\$PATH" >> /etc/profile
source /etc/profile

6. 配置文件设置

mkdir -p /data/redis/6379
chown redis.redis /data/redis/6379/ -R
mkdir /etc/redis

编辑配置文件 /etc/redis/6379.conf,核心配置如下:

bind 127.0.0.1 172.17.181.178
daemonize no
supervised no
requirepass *******
masterauth ********
protected-mode yes
port 6379
dir /data/redis/6379
pidfile /data/redis/6379/redis_6379.pid
logfile "/data/redis/6379/redis_6379.log"
tcp-backlog 511
timeout 300
tcp-keepalive 300
loglevel notice
databases 16
always-show-logo no
set-proc-title yes
proc-title-template "{title} {listen-addr} {server-mode}"
stop-writes-on-bgsave-error no
rdbcompression yes
rdbchecksum yes
dbfilename dump.rdb
rdb-del-sync-files no
replica-serve-stale-data yes
replica-read-only yes
repl-diskless-sync no
repl-diskless-sync-delay 5
repl-diskless-load disabled
repl-disable-tcp-nodelay no
replica-priority 100
acllog-max-len 128
lazyfree-lazy-eviction no
lazyfree-lazy-expire no
lazyfree-lazy-server-del no
replica-lazy-flush no
lazyfree-lazy-user-del no
lazyfree-lazy-user-flush no
oom-score-adj no
oom-score-adj-values 0 200 800

maxmemory 8gb       
maxmemory-policy allkeys-lru

appendonly yes
appendfsync everysec
no-appendfsync-on-rewrite no
auto-aof-rewrite-percentage 100
auto-aof-rewrite-min-size 64mb
aof-load-truncated yes
aof-use-rdb-preamble yes
lua-time-limit 5000
slowlog-log-slower-than 10000
slowlog-max-len 128
latency-monitor-threshold 0
notify-keyspace-events ""
hash-max-ziplist-entries 512
hash-max-ziplist-value 64
list-max-ziplist-size -2
list-compress-depth 0
set-max-intset-entries 512
zset-max-ziplist-entries 128
zset-max-ziplist-value 64
hll-sparse-max-bytes 3000
stream-node-max-bytes 4096
stream-node-max-entries 100
activerehashing 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
hz 10
dynamic-hz yes
aof-rewrite-incremental-fsync yes
rdb-save-incremental-fsync yes
jemalloc-bg-thread yes

save 3600 1
save 300 100
save 60 10000

7. 修改系统参数

# 启用内存overcommit
echo "vm.overcommit_memory = 1" >> /etc/sysctl.conf
# 修改内核参数
echo "net.core.somaxconn = 1024" >> /etc/sysctl.conf
# 配置立即生效
sysctl -p

8. 创建Systemd服务并启动

[Unit]
Description=Redis In-Memory Data Store
After=network.target

[Service]
User=redis
Group=redis
ExecStart=/usr/local/redis/bin/redis-server /etc/redis/6379.conf
ExecStop=/usr/local/redis/bin/redis-cli -a ******* shutdown save
Restart=always
Type=simple

[Install]
WantedBy=multi-user.target

systemctl daemon-reload

systemctl start redis

systemctl enable redis

systemctl status redis

redis-cli -a *******

配置redis主从

vim /etc/redis/6379.conf

# 绑定 IP
bind 0.0.0.0

# 端口
port 6379

maxmemory 8gb
# 后台运行
daemonize no   ##这个要选no 不后台,防止systemd找不到

# 数据目录
dir /data/redis/6379

# 日志
logfile "/data/redis/6379/redis_6379.log"

# 自身访问密码(可与主相同)
requirepass *******

# 主节点地址
replicaof 172.17.181.178  6379

# 访问主节点的密码(必须)
masterauth AMdTcTxbvM

# 关闭保护模式
protected-mode no

# 从节点只读(推荐)
replica-read-only yes

vim /etc/systemd/system/redis.service

[Service]
User=redis
Group=redis
Type=simple
ExecStart=/usr/local/redis/bin/redis-server /etc/redis/6379.conf
# 确保注释掉 ExecStop,避免密码问题
# ExecStop=...
Restart=always

systemctl daemon-reload

systemctl start redis

systemctl enable redis

systemctl status redis


评论