redis的安装

redis的安装

centos7 redis源码安装
复制代码
#安装依赖包
[root@localhost ~]# yum install tcl gcc gcc-c++ -y
#解压redis安装包
[root@localhost ~]# tar zxvf redis-6.2.14.tar.gz -C /usr/local/
#编译安装
[root@localhost ~]# cd /usr/local/redis-6.2.14/
[root@localhost redis-6.2.14]#make && make install
#启动并放在后台运行
[root@localhost redis-6.2.14]# redis-server redis.conf &
#查看启动端口
[root@localhost redis-6.2.14]# netstat -antp | grep 6379
tcp        0      0 127.0.0.1:6379          0.0.0.0:*               LISTEN     
 7751/redis-server 1 
tcp6       0      0 ::1:6379               :::*                   LISTEN     
 7751/redis-server 1 
#客户端进入
[root@localhost redis-6.2.14]# redis-cli 
127.0.0.1:6379>
centos 8 yum安装
复制代码
# centos stream 8 由appstream仓库提供,centos7需要配置epel仓库
[root@localhost ~]# dnf info redis
Last metadata expiration check: 0:00:18 ago on Thu 12 Feb 2026 03:04:51 PM CST.
Available Packages
Name         : redis
Version     : 5.0.3
Release     : 5.module_el8.4.0+955+7126e393
Architecture : x86_64
Size         : 927 k
Source       : redis-5.0.3-5.module_el8.4.0+955+7126e393.src.rpm
Repository   : appstream
Summary     : A persistent key-value database
URL         : http://redis.io
License     : BSD and MIT
Description : Redis is an advanced key-value store. It is often referred to as a 
data
             : structure server since keys can contain strings, hashes, lists, 
sets and
             : sorted sets.
             : 
             : You can run atomic operations on these types, like appending to a 
string;
             : incrementing the value in a hash; pushing to a list; computing set             : intersection, union and difference; or getting the member with 
highest
             : ranking in a sorted set.
             : 
             : In order to achieve its outstanding performance, Redis works with 
an
             : in-memory dataset. Depending on your use case, you can persist it 
either
             : by dumping the dataset to disk every once in a while, or by 
appending
             : each command to a log.
             : 
             : Redis also supports trivial-to-setup master-slave replication, 
with very
             : fast non-blocking first synchronization, auto-reconnection on net 
split
             : and so forth.
             : 
             : Other features include Transactions, Pub/Sub, Lua scripting, Keys 
with a
             : limited time-to-live, and configuration settings to make Redis 
behave like
             : a cache.
             : 
             : You can use Redis from most programming languages also.
             
             
# 安装redis
[root@localhost ~]# dnf install -y redis
# 设置服务开机自启并启动
[root@localhost ~]# systemctl enable redis --now
#查看redis-server监听6379端口
[root@localhost ~]# ss -tunlp
Netid         State         Recv-Q         Send-Q                 Local 
Address:Port                 Peer Address:Port         Process                   
                       
tcp           LISTEN         0              128                         
 0.0.0.0:22                         0.0.0.0:*             users:
(("sshd",pid=954,fd=3))                   
tcp           LISTEN         0              511                       
 127.0.0.1:6379                       0.0.0.0:*             users:(("redisserver",pid=10350,fd=6))
        
tcp           LISTEN         0              128                             
[::]:22                           [::]:*             users:
(("sshd",pid=954,fd=4))      
[root@localhost ~]# pstree -p | grep redis
           |-redis-server(10350)-+-{redis-server}(10351)
           |                     |-{redis-server}(10352)
           |                     `-{redis-server}(10353)
           
[root@localhost ~]# redis-cli
127.0.0.1:6379> ping
PONG开启Redis多实例
 
Redis配置文件说明 
why 6379
Redis 默认使用端口号 6379,这一选择主要源于其作者 Salvatore Sanfilippo(昵称 antirez)的一个个
人趣闻,而非技术上的特殊含义。
根据作者在个人博客和社交媒体上的说明,6379 这个数字来源于手机键盘上字母 "MERZ" 对应的按键。 
6379 "MERZ" 是作者及其朋友用来形容"愚蠢"的俚语,源自意大利女演员 Alessia Merz 在电视节目中的
一些表现,作者觉得她的话很可笑,于是创造了这个词。 6379当为 Redis 选择默认端口时,antirez 没有
过多思考,直接将 "MERZ" 在手机键盘上对应的数字 6379 用作了 Redis 的默认端口。 
127.0.0.1:6379> info
# Server
redis_version:5.0.3
redis_git_sha1:00000000
开启Redis多实例
复制代码
[root@localhost ~]# redis-server --port 6380
[root@localhost ~]# ss -ntl
State               Recv-Q               Send-Q                             
Local Address:Port                             Peer Address:Port               
Process               
LISTEN               0                    128                                     
 0.0.0.0:22                                    0.0.0.0:*                         
               
LISTEN               0                    511                                   
 127.0.0.1:6379                                  0.0.0.0:*                       
                 
LISTEN               0                    511                                     
 0.0.0.0:6380                                  0.0.0.0:*           
[root@localhost ~]# redis-cli -p 6380
127.0.0.1:6380> ping
PONG

Redis配置文件说明

why 6379

Redis 默认使用端口号 6379,这一选择主要源于其作者 Salvatore Sanfilippo(昵称 antirez)的一个个 人趣闻,而非技术上的特殊含义。

根据作者在个人博客和社交媒体上的说明,6379 这个数字来源于手机键盘上字母 "MERZ" 对应的按键。 6379 "MERZ" 是作者及其朋友用来形容"愚蠢"的俚语,源自意大利女演员 Alessia Merz 在电视节目中的 一些表现,作者觉得她的话很可笑,于是创造了这个词。 6379当为 Redis 选择默认端口时,antirez 没有 过多思考,直接将 "MERZ" 在手机键盘上对应的数字 6379 用作了 Redis 的默认端口。

复制代码
[root@localhost ~]# vim /etc/redis.conf 
69 bind 127.0.0.1          #Redis 仅监听本地回环地址 127.0.0.1,这意味着只有本机可以连接 
Redis。若需远程访问,应改为 bind 0.0.0.0(但务必配合密码使用)。
88 protected-mode yes      #启用保护模式。当 Redis 未设置密码且未明确绑定到特定 IP(或绑定
了外网 IP)时,只允许来自 127.0.0.1 和 ::1 的连接。这是防止未授权访问的安全机制。
92 port 6379               #Redis 监听 TCP 端口 6379(默认端口)。
101 tcp-backlog 511        #设置 TCP 连接全队列(completed connection queue)的最大长
度为 511。高并发场景下可能需要调大,并同步调整系统参数 /proc/sys/net/core/somaxconn
113 timeout 0              #客户端空闲连接超时时间为 0,表示永不因空闲而断开连接。
130 tcp-keepalive 300      #每 300 秒(5 分钟)向客户端发送一次 TCP keepalive 探测包,用
于检测死连接。
136 daemonize no           #Redis 不作为守护进程运行,而是以前台方式启动(适合容器化部署或
手动调试)。
147 supervised no          #不与操作系统进程管理器(如 systemd 或 upstart)集成。
158 pidfile /var/run/redis_6379.pid  #指定 Redis 主进程 PID 写入的文件路径。注意:该目录
需存在且 Redis 进程有写权限。
166 loglevel notice        #日志级别设为 notice,记录重要信息(如启动、关闭、持久化事件),
适合生产环境。  
171 logfile /var/log/redis/redis.log     #日志输出到指定文件 
/var/log/redis/redis.log。确保该路径可写。
186 databases 16            #Redis 启动时创建 16 个逻辑数据库(编号 0 到 15),可通过 
SELECT <dbid> 切换。
194 always-show-logo yes    #在启动redis时是否显示或在日志中记录记录redis的logo
# RDB 快照持久化策略
218 save 900 1              #900 秒内至少有 1 个 key 变更 → 触发 bgsave
219 save 300 10             #300 秒内至少有 10 个 key 变更 → 触发 bgsave
220 save 60 10000           #60 秒内至少有 10000 个 key 变更 → 触发 bgsave
stop-writes-on-bgsave-error yes  #如果后台 RDB 保存失败(如磁盘满),Redis 将拒绝写入操
作,防止数据丢失未被察觉。
rdbcompression yes               #对 RDB 文件中的字符串值启用 LZF 压缩,节省磁盘空间(推
荐开启)。
rdbchecksum yes                  #在 RDB 文件末尾添加 CRC64 校验和,提高文件损坏检测能力
(性能损耗约 10%)。
dir /var/lib/redis               #指定 Redis 工作目录。RDB 文件、AOF 文件等都将存储在此
目录下。必须是目录路径(不能是文件)。
dbfilename dump.rdb              #RDB默认文件名
replica-serve-stale-data yes     #当从节点(replica)与主节点失联时,仍可继续响应客户端请
求(可能返回过期数据)。若设为 no,则除部分管理命令外均返回错误。
replica-read-only yes            #从节点默认为只读模式,禁止客户端在其上执行写命令(防止数
据不一致)。
repl-diskless-sync no            #主节点在全量同步时,先将 RDB 文件写入磁盘,再发送给从节
点(而非直接通过 socket 流式传输)。
repl-diskless-sync-delay 5       #当启用 repl-diskless-sync yes 时,主节点等待 5 秒再
开始传输,以便更多从节点能加入批量同步。
replica-priority 100            #从节点优先级(供 Redis Sentinel 选主使用)。数值越小优
先级越高;设为 0 表示永不参与选主。
requirepass foobared             #设置 Redis 访问密码为 foobared,客户端需先执行 AUTH 
foobared 才能操作。
maxclients 10000                                    # 最大客户端连接数
appendonly no                                       # 禁用 AOF 持久化(仅使用 RDB)
appendfilename "appendonly.aof"                     # AOF 文件名(当前未生效)
appendfsync everysec                                 # 若启用 AOF,每秒 fsync 一次
(平衡性能与安全)
no-appendfsync-on-rewrite no                        # RDB/AOF 重写期间仍执行 
appendfsync
auto-aof-rewrite-percentage 100                     # AOF 文件比上次重写后增长 100% 
时触发重写
auto-aof-rewrite-min-size 64mb                      # AOF 文件至少 64MB 才触发自动重
写
aof-load-truncated yes                              # 启动时若 AOF 被截断,尝试加载并
继续启动
aof-use-rdb-preamble yes                            # AOF 重写使用 RDB 前导(混合持久
化,加快恢复)
lua-time-limit 5000                                 # Lua 脚本最大执行时间(毫秒)
slowlog-log-slower-than 10000                       # 慢查询阈值:10000 微秒(10 毫
秒)
slowlog-max-len 128                                 # 慢查询日志最多保留 128 条
latency-monitor-threshold 0                         # 延迟监控阈值(0 = 关闭)
notify-keyspace-events ""                           # 键空间通知事件(空 = 禁用)
hash-max-ziplist-entries 512                        # Hash 使用 ziplist 编码的最大元
素数
hash-max-ziplist-value 64                           # Hash 使用 ziplist 编码的单个 
field/value 最大字节数
list-max-ziplist-size -2                            # List 的 quicklist 每个 
ziplist 节点最大为 8KB(-2)
list-compress-depth 0                               # List 的 quicklist 压缩深度(0 
= 不压缩)
set-max-intset-entries 512                          # Set 使用 intset 编码的最大整数
元素数
zset-max-ziplist-entries 128                        # Sorted Set 使用 ziplist 编码
的最大元素数
zset-max-ziplist-value 64                           # Sorted Set 使用 ziplist 编码
的单个 member/score 最大字节数
hll-sparse-max-bytes 3000                           # HyperLogLog 稀疏表示最大字节数
(超过转稠密)
stream-node-max-bytes 4096                          # Stream 单个节点最大字节数
stream-node-max-entries 100                         # Stream 单个节点最大消息条数
activerehashing yes                                 # 启用渐进式 rehash,避免卡顿
client-output-buffer-limit normal 0 0 0             # 普通客户端输出缓冲区无限制
client-output-buffer-limit replica 256mb 64mb 60    # 从节点:硬限 256MB,软限 64MB 
持续 60 秒则断开
client-output-buffer-limit pubsub 32mb 8mb 60       # Pub/Sub 客户端:硬限 32MB,软
限 8MB 持续 60 秒则断开
hz 10                                               # 后台任务每秒执行 10 次(如过期 
key 清理)
dynamic-hz yes                                      # 动态调整 hz(连接多时自动提高)
aof-rewrite-incremental-fsync yes                   # AOF 重写时增量 fsync(每 32MB 
刷一次)
rdb-save-incremental-fsync yes                      # RDB 保存时增量 fsync(每 32MB 
刷一次)

config命令实现动态修改配置

config命令用于查看当前redis配置,以及不重启redis服务实现动态更改redis配置等

CONFIG SET parameter value 时间复杂度:O(1) CONFIG SET命令可以动态地调整Redis服务器的配置(configuration)而无须重启。

可以使用它修改配置参数,或者改变Redis的持久化(Persistence)方式。 CONFIG SET可以修改的配置参数可以使用命令CONFIG GET *来列出,所有被CONFIG SET修改的配置参 数都会立即生效。

CONFIG GET parameter 时间复杂度:O(N),其中N为命令返回的配置选项数量。 cONFIG GET命令用于取得运行中的Redis服务器的配置参数(configuration parameters),在Redis2.4 版本中,有部分参数没有办法用CONFIG GET访问,但是在最新的Redis2.6版本中,所有配置参数都已经 可以用CONFIG GET访问了。

CONFIG GET接受单个参数parameter作为搜索关键字,查找所有匹配的配置参数,其中参数和值以"键值对"(key-value pairs)的方式排列。 比如执行CONFIG GET s*命令,服务器就会返回所有以s开头的配置参数及参数的值:

复制代码
# redis 7支持动态修改端口
127.0.0.1:6379> config set port 8888
OK
# redis 5不支持动态修改端口
127.0.0.1:6379> config set port 8888
(error) ERR Unsupported CONFIG parameter: port

设置客户端连接密码

复制代码
# 设置连接密码
127.0.0.1:6379> CONFIG SET requirepass 123456
OK
# 查看连接密码
[root@localhost ~]# redis-cli -a 123456
Warning: Using a password with '-a' or '-u' option on the command line interface 
may not be safe.
127.0.0.1:6379> CONFIG GET requirepass
1) "requirepass"
2) "123456"

获取当前配置

复制代码
# 奇数行为键,偶数行为值
127.0.0.1:6379> CONFIG GET *
  1) "dbfilename"
  2) "dump.rdb"
  3) "requirepass"
  4) "123456"
......
# 查看bind
127.0.0.1:6379> CONFIG GET bind
1) "bind"
2) "127.0.0.1"
# Redis5.0有些设置无法修改,Redis6.2.14版本支持修改bind
127.0.0.1:6379> CONFIG SET BIND 0.0.0.0
(error) ERR Unsupported CONFIG parameter: BIND
127.0.0.1:6379> config set bind 0.0.0.0
OK

设置Redis使用最大内存容量

复制代码
# 查看默认配置
127.0.0.1:6379> CONFIG GET maxmemory
1) "maxmemory"
2) "0"
# 默认以字节为单位
127.0.0.1:6379> CONFIG SET maxmemory 10086
OK
127.0.0.1:6379> CONFIG GET maxmemory
1) "maxmemory"
2) "10086"
# 也可以写单位
127.0.0.1:6379> CONFIG SET maxmemory 1G
OK
127.0.0.1:6379> CONFIG GET maxmemory
1) "maxmemory"
2) "1000000000"

慢查询

许多存储系统(例如MySQL)提供慢查询日志帮助开发和运维人员定位系统存在的慢操作。所谓慢查询 日志就是系统在命令执行前后计算每条命令的执行时间,当超过预设阀值,就将这条命令的相关信息 (例如:发生时间,耗时,命令的详细信息)记录下来,Redis也提供了类似的功能。

一条客户端命令声明周期如下:

1)发送命令 2)命令排队 3)命令执行 4)返回结果

两点说明:

  1. 慢查询发生在第三阶段 2. 客户端超时不一定慢查询,但慢查询是客户端超时的一个可能因数

    1000us(微秒)=1ms(毫秒)

    [root@localhost ~]# vim /etc/redis.conf
    995 slowlog-log-slower-than 1 #单位为us,指定超过1us即为慢的指令,默认值为10000us
    999 slowlog-max-len 1024 #指定只保存最近的1024条慢记录,默认值为128
    [root@localhost ~]# systemctl restart redis

    查看慢日志的记录条数

    [root@localhost ~]# redis-cli -a 123456
    Warning: Using a password with '-a' or '-u' option on the command line interface
    may not be safe.
    127.0.0.1:6379> SLOWLOG LEN
    (integer) 5
    127.0.0.1:6379> SLOWLOG GET

      1. (integer) 5 #项目唯一ID
      2. (integer) 1770892970 #unix时间戳
      3. (integer) 3 #执行耗时(微秒)
        1. "SLOWLOG" #执行的命令
        2. "LEN"
      4. "127.0.0.1:56406" #客户端IP和端口
      5. "" #客户端名称(未设置)
      1. (integer) 4
      2. (integer) 1770892963
      3. (integer) 379
        1. "COMMAND"
      4. "127.0.0.1:56406"
      5. ""
      1. (integer) 3
      2. (integer) 1770892963
      3. (integer) 1
        1. "AUTH"
        2. "123456"
      4. "127.0.0.1:56406"
      5. ""
      1. (integer) 2
      2. (integer) 1770892863
      3. (integer) 18
        1. "CONFIG"
        2. "GET"
        3. "bind"
      4. "127.0.0.1:38008"
      5. ""
      1. (integer) 1
      2. (integer) 1770892859
      3. (integer) 659
        1. "COMMAND"
      4. "127.0.0.1:38008"
      5. ""
      1. (integer) 0
      2. (integer) 1770892859
      3. (integer) 1
        1. "AUTH"
        2. "123456"
      4. "127.0.0.1:38008"
      5. ""
        127.0.0.1:6379>

    清空慢日志

    127.0.0.1:6379> SLOWLOG RESET
    OK

相关推荐
zqit rdlo1 小时前
MYSQL 创建索引
数据库·mysql
gmaajt2 小时前
CSS 背景图片无法加载的常见原因与正确写法详解
jvm·数据库·python
2601_949816682 小时前
MySQL 数据库连接池爆满问题排查与解决
android·数据库·mysql
解救女汉子2 小时前
Python如何计算NumPy数组的协方差矩阵_调用cov函数进行特征分析
jvm·数据库·python
2201_761040592 小时前
Golang怎么安全关闭channel_Golang channel关闭教程【通俗】
jvm·数据库·python
jerryinwuhan2 小时前
基于结构可控性的给水管网传感器布点选择算法
数据库·算法
m0_493934532 小时前
Redis怎样合并多天访客数据_通过PFMERGE指令聚合HyperLogLog记录
jvm·数据库·python
tjc199010052 小时前
bootstrap怎么实现响应式的文章瀑布流布局
jvm·数据库·python
旺王雪饼 www2 小时前
MySQL常用查询语句
数据库·sql