一Redis 环境梳理1.1 环境梳理在讲解服务配置以及怎么使用 Redis 之前我现在梳理一下现在 Redis 相关的目录环境Redis 下载源码解压的目录如下/usr/local/src/redis-8.8.0Redis 的源码目录不参与运行只用于编译。这个目录后续基本可以不动除非需要重新编译或者部署多个 Redis 只需要指定编译到不同的目录即可。当前小编这边只编译了一个 Redis , 也就是单机部署了一台其目录位置如下/usr/local/redis该目录就是当前 Redis 的实际运行目录核心后续处理都在这个目录之中。编译生成的内容只有一个 bin 目录。其内容如下[roottoast-server redis]# pwd /usr/local/redis [roottoast-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-serverbin 目录里面的文件作用如下文件作用redis-serverRedis服务redis-cli客户端用于连接 Redis 服务redis-benchmark压测redis-check-rdbRDB检查redis-check-aofAOF检查redis-sentinel哨兵高可用后续所有的操作处理基本都围着这里开始。1.2 补充 Redis 关键目录与文件在 redis 的运行目录补充标准的结构目录[roottoast-server redis]# mkdir -p /usr/local/redis/{conf,data,logs,pid} [roottoast-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 目录下命令如下[roottoast-server redis]# cp /usr/local/src/redis-8.8.0/redis.conf /usr/local/redis/conf/redis.conf [roottoast-server redis]# ll conf/ total 120 -rw-r--r-- 1 root root 120616 Jun 1 11:50 redis.conf [roottoast-server redis]# cp conf/redis.conf conf/redis.conf.back # 修改前为其备份一份 [roottoast-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.back1.3 修改 redis.conf 配置文件使用 vim 编辑器进行修改 redis.conf 配置文件内容如下修改项修改前 (默认配置)修改后服务绑定地址bind 127.0.0.1 -::1bind 127.0.0.1 你的内网IP服务绑定端口6379不修改服务后台运行daemonize nodaemonize yes进程 PID 文件路径pidfile /var/run/redis_6379.pidpidfile /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[roottoast-server redis]# bin/redis-server conf/redis.conf [roottoast-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_memory1 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 version8.8.0, bits64, commit00000000, modified0, pid4109807, 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 modestandalone, port6379. 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_memory1 echo vm.overcommit_memory1 /etc/sysctl.conf重启 Redis 即可[roottoast-server redis]# pkill redis-server # 先杀死旧进程 [roottoast-server redis]# ps -ef | grep redis root 4110133 4064778 0 15:47 pts/0 00:00:00 grep --colorauto redis [roottoast-server redis]# bin/redis-server conf/redis.conf # 重新启动 [roottoast-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 modestandalone, port6379. 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 modestandalone, port6379 # 单例启动端口 6379 Done loading RDB, ...... # 数据文件加载完成无数据也正常 Ready to accept connections tcp # redis服务启动成功可以接受客户端连接二Redis 认证授权2.1 redis 默认登录在将认证授权之前小编先使用 Redis 连接一下服务端。连接命令过程如下[roottoast-server redis]# bin/redis-cli 127.0.0.1:6379 auth 你的密码 OK 127.0.0.1:6379 ping PONG此时则表示连接成功。在redis 6 之前都是这样的方式来连接的。现在小编先抛出一句话就是 Redis 在 6 之前只有“认证”没有“授权”。上面的连接过程就是认证的过程。一个全局密码 requirepassAUTH 密码 只是证明你能进大门进去之后读写权限都是全开的没有分读 / 写角色。所以并没有授权。认证通过用户名/密码登录成功表示确认是本系统内部的“人”/“账号”授权控制这个用户能读、能写、能执行哪些命令、能访问哪些 key现在的情况是在配置文件里面配置requirepass 你的密码这等价于给 default 用户设了密码user default on 你的密码 ~* all~*能访问所有 keyall能执行所有命令读、写、管理都可以所以[roottoast-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创建一个只读用户只能写不能读[roottoast-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:63792创建一个只读用户只能读不能写[roottoast-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:63793创建只能访问user:*前缀 key 的用户[roottoast-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:63794测试新建用户登录[roottoast-server redis]# bin/redis-cli 127.0.0.1:6379 auth writer 1234 OK 127.0.0.1:63795命令解析on启用用户off禁用用户1234密码是1234~*所有 key 都能访问read允许读类命令-read禁止读类命令write允许写类命令-write禁止写类命令三Redis 多数据库redis 数据库默认是拥有 16 个存储库。这样为不同的项目对业务数据管理有着非常好的隔离不同项目之间的业务数据不会污染。如果你的项目比较多业务数据 16 个数据库也不够用的话可以修改 redis.conf 配置文件对其数据库数量进行增加如下修改结果修改前默认修改后databases 16databases 20我将数据库数量增到 20个然后杀死当前进程重新启动命令如下[roottoast-server redis]# pkill redis-server [roottoast-server redis]# ps -ef | grep redis root 4122052 4064778 0 17:05 pts/0 00:00:00 grep --colorauto redis [roottoast-server redis]# bin/redis-server conf/redis.conf [roottoast-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 --colorauto redis查看当前 redis 数据库数量127.0.0.1:6379 CONFIG get databases 1) databases 2) 20 127.0.0.1:6379redis 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) - cant set immutable config集群模式下更惨整个集群只有 db 0多库本身就不支持。所以线上环境想把 16 改成 32/64必须改配置 重启没有 “不重启加库” 的官方方法。
Redis 服务配置与使用
一Redis 环境梳理1.1 环境梳理在讲解服务配置以及怎么使用 Redis 之前我现在梳理一下现在 Redis 相关的目录环境Redis 下载源码解压的目录如下/usr/local/src/redis-8.8.0Redis 的源码目录不参与运行只用于编译。这个目录后续基本可以不动除非需要重新编译或者部署多个 Redis 只需要指定编译到不同的目录即可。当前小编这边只编译了一个 Redis , 也就是单机部署了一台其目录位置如下/usr/local/redis该目录就是当前 Redis 的实际运行目录核心后续处理都在这个目录之中。编译生成的内容只有一个 bin 目录。其内容如下[roottoast-server redis]# pwd /usr/local/redis [roottoast-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-serverbin 目录里面的文件作用如下文件作用redis-serverRedis服务redis-cli客户端用于连接 Redis 服务redis-benchmark压测redis-check-rdbRDB检查redis-check-aofAOF检查redis-sentinel哨兵高可用后续所有的操作处理基本都围着这里开始。1.2 补充 Redis 关键目录与文件在 redis 的运行目录补充标准的结构目录[roottoast-server redis]# mkdir -p /usr/local/redis/{conf,data,logs,pid} [roottoast-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 目录下命令如下[roottoast-server redis]# cp /usr/local/src/redis-8.8.0/redis.conf /usr/local/redis/conf/redis.conf [roottoast-server redis]# ll conf/ total 120 -rw-r--r-- 1 root root 120616 Jun 1 11:50 redis.conf [roottoast-server redis]# cp conf/redis.conf conf/redis.conf.back # 修改前为其备份一份 [roottoast-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.back1.3 修改 redis.conf 配置文件使用 vim 编辑器进行修改 redis.conf 配置文件内容如下修改项修改前 (默认配置)修改后服务绑定地址bind 127.0.0.1 -::1bind 127.0.0.1 你的内网IP服务绑定端口6379不修改服务后台运行daemonize nodaemonize yes进程 PID 文件路径pidfile /var/run/redis_6379.pidpidfile /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[roottoast-server redis]# bin/redis-server conf/redis.conf [roottoast-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_memory1 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 version8.8.0, bits64, commit00000000, modified0, pid4109807, 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 modestandalone, port6379. 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_memory1 echo vm.overcommit_memory1 /etc/sysctl.conf重启 Redis 即可[roottoast-server redis]# pkill redis-server # 先杀死旧进程 [roottoast-server redis]# ps -ef | grep redis root 4110133 4064778 0 15:47 pts/0 00:00:00 grep --colorauto redis [roottoast-server redis]# bin/redis-server conf/redis.conf # 重新启动 [roottoast-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 modestandalone, port6379. 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 modestandalone, port6379 # 单例启动端口 6379 Done loading RDB, ...... # 数据文件加载完成无数据也正常 Ready to accept connections tcp # redis服务启动成功可以接受客户端连接二Redis 认证授权2.1 redis 默认登录在将认证授权之前小编先使用 Redis 连接一下服务端。连接命令过程如下[roottoast-server redis]# bin/redis-cli 127.0.0.1:6379 auth 你的密码 OK 127.0.0.1:6379 ping PONG此时则表示连接成功。在redis 6 之前都是这样的方式来连接的。现在小编先抛出一句话就是 Redis 在 6 之前只有“认证”没有“授权”。上面的连接过程就是认证的过程。一个全局密码 requirepassAUTH 密码 只是证明你能进大门进去之后读写权限都是全开的没有分读 / 写角色。所以并没有授权。认证通过用户名/密码登录成功表示确认是本系统内部的“人”/“账号”授权控制这个用户能读、能写、能执行哪些命令、能访问哪些 key现在的情况是在配置文件里面配置requirepass 你的密码这等价于给 default 用户设了密码user default on 你的密码 ~* all~*能访问所有 keyall能执行所有命令读、写、管理都可以所以[roottoast-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创建一个只读用户只能写不能读[roottoast-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:63792创建一个只读用户只能读不能写[roottoast-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:63793创建只能访问user:*前缀 key 的用户[roottoast-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:63794测试新建用户登录[roottoast-server redis]# bin/redis-cli 127.0.0.1:6379 auth writer 1234 OK 127.0.0.1:63795命令解析on启用用户off禁用用户1234密码是1234~*所有 key 都能访问read允许读类命令-read禁止读类命令write允许写类命令-write禁止写类命令三Redis 多数据库redis 数据库默认是拥有 16 个存储库。这样为不同的项目对业务数据管理有着非常好的隔离不同项目之间的业务数据不会污染。如果你的项目比较多业务数据 16 个数据库也不够用的话可以修改 redis.conf 配置文件对其数据库数量进行增加如下修改结果修改前默认修改后databases 16databases 20我将数据库数量增到 20个然后杀死当前进程重新启动命令如下[roottoast-server redis]# pkill redis-server [roottoast-server redis]# ps -ef | grep redis root 4122052 4064778 0 17:05 pts/0 00:00:00 grep --colorauto redis [roottoast-server redis]# bin/redis-server conf/redis.conf [roottoast-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 --colorauto redis查看当前 redis 数据库数量127.0.0.1:6379 CONFIG get databases 1) databases 2) 20 127.0.0.1:6379redis 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) - cant set immutable config集群模式下更惨整个集群只有 db 0多库本身就不支持。所以线上环境想把 16 改成 32/64必须改配置 重启没有 “不重启加库” 的官方方法。