Redis 服务配置与使用
2026/6/15 6:24:31 网站建设 项目流程

一,Redis 环境梳理

1.1 环境梳理

在讲解服务配置以及怎么使用 Redis 之前,我现在梳理一下现在 Redis 相关的目录环境

Redis 下载源码解压的目录如下:

/usr/local/src/redis-8.8.0

Redis 的源码目录不参与运行,只用于编译。这个目录后续基本可以不动,除非需要重新编译,或者部署多个 Redis 只需要指定编译到不同的目录即可。

当前小编这边只编译了一个 Redis , 也就是单机部署了一台,其目录位置如下:

/usr/local/redis

该目录就是当前 Redis 的实际运行目录(核心),后续处理都在这个目录之中。编译生成的内容只有一个 bin 目录。其内容如下:

[root@toast-server redis]# pwd /usr/local/redis [root@toast-server redis]# ll bin/ total 45492 -rwxr-xr-x 1 root root 9109176 May 28 10:54 redis-benchmark lrwxrwxrwx 1 root root 12 May 28 10:54 redis-check-aof -> redis-server lrwxrwxrwx 1 root root 12 May 28 10:54 redis-check-rdb -> redis-server -rwxr-xr-x 1 root root 10485808 May 28 10:54 redis-cli lrwxrwxrwx 1 root root 12 May 28 10:54 redis-sentinel -> redis-server -rwxr-xr-x 1 root root 26983888 May 28 10:54 redis-server

bin 目录里面的文件作用如下:

文件

作用

redis-server

Redis服务

redis-cli

客户端(用于连接 Redis 服务)

redis-benchmark

压测

redis-check-rdb

RDB检查

redis-check-aof

AOF检查

redis-sentinel

哨兵(高可用)

后续所有的操作处理基本都围着这里开始。

1.2 补充 Redis 关键目录与文件

在 redis 的运行目录补充标准的结构目录:

[root@toast-server redis]# mkdir -p /usr/local/redis/{conf,data,logs,pid} [root@toast-server redis]# ll # 最终结果如下 total 16 drwxr-xr-x 2 root root 4096 May 28 10:54 bin # 命令脚本,编译源码生成 drwxr-xr-x 2 root root 4096 Jun 1 11:34 conf # Redis 服务配置目录 drwxr-xr-x 2 root root 4096 Jun 1 11:34 data # 数据持久化保存的目录位置 drwxr-xr-x 2 root root 4096 Jun 1 11:34 logs # Redis 服务日志目录 drwxr-xr-x 2 root root 4096 Jun 1 11:34 pid # 服务进程PID文件存储路径

目前我们仅仅只是将目录给创建好了,真正需要 redis 将数据都输入到指定的目录中需要修改配置文件。所以现在我们首先从源码目录将 redis.conf 配置文件 复制到 conf 目录下,命令如下:

[root@toast-server redis]# cp /usr/local/src/redis-8.8.0/redis.conf /usr/local/redis/conf/redis.conf [root@toast-server redis]# ll conf/ total 120 -rw-r--r-- 1 root root 120616 Jun 1 11:50 redis.conf [root@toast-server redis]# cp conf/redis.conf conf/redis.conf.back # 修改前,为其备份一份 [root@toast-server redis]# ll conf/ total 240 -rw-r--r-- 1 root root 120616 Jun 1 11:50 redis.conf -rw-r--r-- 1 root root 120616 Jun 1 11:51 redis.conf.back

1.3 修改 redis.conf 配置文件

使用 vim 编辑器,进行修改 redis.conf 配置文件内容如下:

修改项

修改前 (默认配置)

修改后

服务绑定地址

bind 127.0.0.1 -::1

bind 127.0.0.1 你的内网IP

服务绑定端口

6379

不修改

服务后台运行

daemonize no

daemonize yes

进程 PID 文件路径

pidfile /var/run/redis_6379.pid

pidfile /usr/local/redis/pid/redis_6379.pid

日志存储路径

logfile ""

logfile /usr/local/redis/logs/redis_6379.log

数据存储路径

dir ./

dir /usr/local/redis/data

服务密码

空白-无内容

requirepass 你的强密码

在进行bind 配置的时候,可以配置多个地址,地址之间用空格进行分割,同时可以使用 IPV4 和 IPV6 的地址。使用 bind 0.0.0.0 则表示所有IP都可以连接。这个是属于比较危险的操作,redis有它自己的保护模式,如果你要开启 bind 0.0.0.0 方式,则需要先将 redis 的保护模式关闭否则也无法连接。

protected-mode yes # 保护模式 yes 默认开启,no 表示关闭

1.4 启动Redis

启动redis

[root@toast-server redis]# bin/redis-server conf/redis.conf [root@toast-server redis]# tail -f logs/redis_6379.log 4109615:M 01 Jun 2026 15:44:31.247 * Server initialized 4109615:M 01 Jun 2026 15:44:31.247 * Ready to accept connections tcp 4109806:C 01 Jun 2026 15:45:35.540 # WARNING Memory overcommit must be enabled! Without it, a background save or replication may fail under low memory condition. Being disabled, it can also cause failures without low memory condition, see https://github.com/jemalloc/jemalloc/issues/1328. To fix this issue add 'vm.overcommit_memory = 1' to /etc/sysctl.conf and then reboot or run the command 'sysctl vm.overcommit_memory=1' for this to take effect. 4109807:C 01 Jun 2026 15:45:35.541 * oO0OoO0OoO0Oo Redis is starting oO0OoO0OoO0Oo 4109807:C 01 Jun 2026 15:45:35.541 * Redis version=8.8.0, bits=64, commit=00000000, modified=0, pid=4109807, just started 4109807:C 01 Jun 2026 15:45:35.541 * Configuration loaded 4109807:M 01 Jun 2026 15:45:35.541 * monotonic clock: POSIX clock_gettime 4109807:M 01 Jun 2026 15:45:35.543 * Running mode=standalone, port=6379. 4109807:M 01 Jun 2026 15:45:35.543 * Server initialized 4109807:M 01 Jun 2026 15:45:35.543 * Ready to accept connections tcp

它这里有一条警告信息

WARNING Memory overcommit must be enabled! ......

原因是因为 Redis 采用的是 开启内存过量分配,避免持久化 / 复制时失败。 不改也没有关系,因为不影响启动,如果需要修复内存分配警告,则改变一下内容的分配规则即可。命令如下:

sysctl vm.overcommit_memory=1 echo "vm.overcommit_memory=1" >> /etc/sysctl.conf

重启 Redis 即可

[root@toast-server redis]# pkill redis-server # 先杀死旧进程 [root@toast-server redis]# ps -ef | grep redis root 4110133 4064778 0 15:47 pts/0 00:00:00 grep --color=auto redis [root@toast-server redis]# bin/redis-server conf/redis.conf # 重新启动 [root@toast-server redis]# tail -f logs/redis_6379.log 4110155:C 01 Jun 2026 15:47:51.102 * Configuration loaded 4110155:M 01 Jun 2026 15:47:51.103 * monotonic clock: POSIX clock_gettime 4110155:M 01 Jun 2026 15:47:51.105 * Running mode=standalone, port=6379. 4110155:M 01 Jun 2026 15:47:51.105 * Server initialized 4110155:M 01 Jun 2026 15:47:51.105 * Loading RDB produced by version 8.8.0 4110155:M 01 Jun 2026 15:47:51.105 * RDB age 22 seconds 4110155:M 01 Jun 2026 15:47:51.105 * RDB memory usage when created 0.68 Mb 4110155:M 01 Jun 2026 15:47:51.105 * Done loading RDB, keys loaded: 0, keys expired: 0. 4110155:M 01 Jun 2026 15:47:51.105 * DB loaded from disk: 0.000 seconds 4110155:M 01 Jun 2026 15:47:51.105 * Ready to accept connections tcp

从日志信息中我们可以得知

Configuration loaded # 加载配置文件 Running mode=standalone, port=6379 # 单例启动,端口 6379 Done loading RDB, ...... # 数据文件加载完成(无数据也正常) Ready to accept connections tcp # redis服务启动成功,可以接受客户端连接

二,Redis 认证授权

2.1 redis 默认登录

在将认证授权之前,小编先使用 Redis 连接一下服务端。连接命令过程如下:

[root@toast-server redis]# bin/redis-cli 127.0.0.1:6379> auth 你的密码 OK 127.0.0.1:6379> ping PONG

此时则表示连接成功。在redis 6 之前都是这样的方式来连接的。

现在,小编先抛出一句话,就是 Redis 在 6 之前,只有“认证”没有“授权”。上面的连接过程就是认证的过程。一个全局密码 requirepass,AUTH 密码 只是证明你能进大门,进去之后读写权限都是全开的,没有分读 / 写角色。所以并没有授权。

认证

通过用户名/密码登录成功,表示确认是本系统内部的“人”/“账号”

授权

控制这个用户能读、能写、能执行哪些命令、能访问哪些 key

现在的情况是在配置文件里面配置

requirepass 你的密码

这等价于给 default 用户设了密码

user default on <你的密码> ~* +@all
  • ~*:能访问所有 key
  • +@all:能执行所有命令(读、写、管理都可以)

所以:

[root@toast-server redis]# bin/redis-cli 127.0.0.1:6379> auth 你的密码

只是登录(认证),登录后就是超级管理员权限不区分读 / 写

2.2 Redis 6+ 提供的 ACL 登录

Redis 6+ 的 ACL 可以做到:

  • 只读用户:只能 GET、不能 SET
  • 只写用户:只能 SET、不能 GET
  • 只能访问某些 key 的用户
  • 禁止执行危险命令(FLUSHALL、KEYS 等)

1)创建一个只读用户(只能写,不能读)

[root@toast-server redis]# bin/redis-cli 127.0.0.1:6379> auth 你的密码 OK 127.0.0.1:6379> ACL SETUSER reader on >1234 ~* -@read +@write OK 127.0.0.1:6379>

2)创建一个只读用户(只能读,不能写)

[root@toast-server redis]# bin/redis-cli 127.0.0.1:6379> auth 你的密码 OK 127.0.0.1:6379> ACL SETUSER reader on >1234 ~* +@read -@write OK 127.0.0.1:6379>

3)创建只能访问user:*前缀 key 的用户

[root@toast-server redis]# bin/redis-cli 127.0.0.1:6379> auth 你的密码 OK 127.0.0.1:6379> ACL SETUSER userapp on >1234 ~user:* +@all OK 127.0.0.1:6379>

4)测试新建用户登录

[root@toast-server redis]# bin/redis-cli 127.0.0.1:6379> auth writer 1234 OK 127.0.0.1:6379>

5)命令解析

on:启用用户

off:禁用用户

>1234:密码是1234

~*:所有 key 都能访问

+@read:允许类命令-@read:禁止类命令

+@write:允许类命令-@write:禁止类命令

三,Redis 多数据库

redis 数据库,默认是拥有 16 个存储库。这样为不同的项目对业务数据管理有着非常好的隔离,不同项目之间的业务数据不会污染。

如果你的项目比较多,业务数据 16 个数据库也不够用的话,可以修改 redis.conf 配置文件,对其数据库数量进行增加,如下修改结果:

修改前(默认)

修改后

databases 16

databases 20

我将数据库数量增到 20个,然后杀死当前进程,重新启动,命令如下:

[root@toast-server redis]# pkill redis-server [root@toast-server redis]# ps -ef | grep redis root 4122052 4064778 0 17:05 pts/0 00:00:00 grep --color=auto redis [root@toast-server redis]# bin/redis-server conf/redis.conf [root@toast-server redis]# ps -ef | grep redis # redis 已启动 root 4122097 1 0 17:06 ? 00:00:00 bin/redis-server 127.0.0.1:6379 root 4122124 4064778 0 17:06 pts/0 00:00:00 grep --color=auto redis

查看当前 redis 数据库数量

127.0.0.1:6379> CONFIG get databases 1) "databases" 2) "20" 127.0.0.1:6379>

redis 8 开源版 不支持运行时动态扩容 databases 数量,databases 是启动时参数,只能在redis.conf配置文件里面写死。必须重启才能生效。

CONFIG SET databases 32 # 会直接报错或 silently 不生效,不能在线改。 127.0.0.1:6379> CONFIG set databases 32 (error) ERR CONFIG SET failed (possibly related to argument 'databases') - can't set immutable config

集群模式下更惨:整个集群只有 db 0,多库本身就不支持

所以:

线上环境:想把 16 改成 32/64,必须改配置 + 重启,没有 “不重启加库” 的官方方法。

需要专业的网站建设服务?

联系我们获取免费的网站建设咨询和方案报价,让我们帮助您实现业务目标

立即咨询