检查并安装Redis
2026/5/24 10:11:23 网站建设 项目流程

查看服务器是否按照redis, 未安装的话需要提供安装操作步骤

检查 Redis 是否安装 + 完整安装教程

我给你整理了一键检查命令最常用、最稳定的 Redis 安装步骤(适用于 CentOS/RHEL 系列、Ubuntu/Debian 系列),直接复制执行即可。


一、先检查服务器是否已安装 Redis

直接在服务器终端执行以下任意一条命令检查:

bash

运行

# 检查是否安装了 redis redis-server --version # 或者检查 redis 服务是否运行 systemctl status redis

判断结果:

  1. 出现版本号(如Redis server v=7.2.3 ...)→已安装
  2. 提示 command not found未安装,按下面步骤安装

二、Redis 安装步骤(通用最稳定方案)

方案 1:Ubuntu / Debian 系统(最常用)

bash

运行

# 1. 更新软件源 sudo apt update # 2. 安装 Redis sudo apt install redis-server -y # 3. 设置开机自启并启动服务 sudo systemctl enable redis-server sudo systemctl start redis-server # 4. 验证是否安装成功 redis-server --version redis-cli ping

出现PONG说明安装并运行成功!


方案 2:CentOS / RHEL / Fedora 系统

bash

运行

# 1. 安装 EPEL 源(CentOS 默认没有 Redis) sudo yum install epel-release -y # 2. 安装 Redis sudo yum install redis -y # 3. 启动并设置开机自启 sudo systemctl enable redis sudo systemctl start redis # 4. 验证 redis-server --version redis-cli ping

出现PONG即为成功。


三、安装后常用命令(备用)

bash

运行

# 查看 Redis 运行状态 systemctl status redis # 停止 Redis systemctl stop redis # 重启 Redis systemctl restart redis # 进入 Redis 命令行 redis-cli

总结

  1. redis-server --version一键检查是否安装
  2. Ubuntu/Debian 使用apt install redis-server
  3. CentOS/RHEL 使用yum install redis
  4. 执行redis-cli ping返回PONG= 安装成功

需要我帮你配置Redis 密码、远程连接、开机自启等安全设置吗?

redis-cli ping Could not connect to Redis at 127.0.0.1:6379: Connection refused

这个报错Connection refused意思很明确:Redis 服务没启动 / 没监听 6379 端口,所以连不上。

我给你一步一步快速修复,直接复制命令执行即可:


第一步:先启动 Redis

如果你是Ubuntu / Debian

bash

运行

sudo systemctl start redis-server sudo systemctl enable redis-server

如果你是CentOS / RHEL

bash

运行

sudo systemctl start redis sudo systemctl enable redis

第二步:再测试连接

bash

运行

redis-cli ping

返回PONG= 修复成功!


如果还是报错(最常见 2 个原因)

原因 1:Redis 没安装成功(重装)

Ubuntu/Debian

bash

运行

sudo apt remove --purge redis-server -y sudo apt install redis-server -y

CentOS/RHEL

bash

运行

sudo yum remove redis -y sudo yum install redis -y

安装完再启动:

bash

运行

sudo systemctl start redis redis-cli ping

原因 2:端口被改了 / 配置错误

执行下面命令查看 Redis 是否在监听:

bash

运行

ss -tulpn | grep redis

如果看不到6379端口,说明配置异常。


快速检查 Redis 状态

bash

运行

systemctl status redis

看看里面有没有报错,我可以帮你直接解决。


总结

  1. 先执行启动命令90% 能好
  2. 再执行redis-cli ping看到PONG就是正常
  3. 还不行就重装,100% 解决

你把执行启动命令后的结果发给我,我可以直接帮你定位问题!

怎么查看服务器是centos还是ubuntu

查看服务器系统版本(一键命令)

1. 最通用命令

bash

运行

cat /etc/os-release
  • 看到CentOS Linux→ CentOS 系统
  • 看到Ubuntu→ Ubuntu 系统

2. 极简快速判断

bash

运行

lsb_release -a

3. 只看系统名称

bash

运行

uname -a

4. 最简单区分

  • 能用yum命令 =CentOS
  • 能用apt命令 =Ubuntu

对应启动 Redis 命令

确认是 CentOS 执行

bash

运行

systemctl start redis systemctl enable redis redis-cli ping

确认是 Ubuntu 执行

bash

运行

systemctl start redis-server systemctl enable redis-server redis-cli ping

systemctl status redis Unit redis.service could not be found. 这个什么解决, 只回答这个问题就行

说明Redis 根本没安装上,直接执行安装命令:

CentOS 系统:

bash

运行

yum install epel-release -y && yum install redis -y systemctl start redis && systemctl enable redis

Ubuntu 系统:

bash

运行

apt update && apt install redis-server -y systemctl start redis-server && systemctl enable redis-server

但是我Linux服务器上面是有目录的

你现在是源码包解压了但还没编译安装,所以系统里没有redis.service服务文件,直接按下面步骤操作即可:


一、先编译安装 Redis(在当前redis-6.2.6目录执行)

bash

运行

# 1. 编译(需要 gcc,没装的话先执行 yum install -y gcc) make # 2. 安装到系统(默认安装到 /usr/local/bin) make install

二、启动 Redis 并验证

bash

运行

# 1. 前台启动(测试用,按 Ctrl+C 可退出) redis-server # 2. 后台启动(生产用) redis-server --daemonize yes # 3. 验证是否成功 redis-cli ping

返回PONG就说明启动成功了。


三、配置成 systemd 服务(可选,方便开机自启)

  1. 新建服务文件:

    bash

    运行

    vi /etc/systemd/system/redis.service
  2. 写入以下内容并保存:

    ini

    [Unit] Description=Redis Server After=network.target [Service] Type=forking ExecStart=/usr/local/bin/redis-server /root/redis/redis-6.2.6/redis.conf --daemonize yes ExecStop=/usr/local/bin/redis-cli shutdown Restart=always [Install] WantedBy=multi-user.target
  3. 加载并启动服务:

    bash

    运行

    systemctl daemon-reload systemctl start redis systemctl enable redis

之后就可以用systemctl status redis查看状态了。


你先执行编译安装的步骤,有任何报错直接发给我,我帮你解决。

systemctl start redis Job for redis.service failed because the control process exited with error code. See "systemctl status redis.service" and "journalctl -xe" for details. 报错了

执行这两条命令,把完整报错截图 / 文字发我,我直接定位修复:

bash

运行

systemctl status redis.service journalctl -u redis -e

提示错误信息如上

我看到问题根源了!报错里明确写着:Fatal error, can't open config file '/root/redis/redis-6.2.6/redis.conf': No such file or directory说明redis.service里写的配置文件路径不对,Redis 找不到配置文件,所以启动失败。


解决步骤

1. 先确认你的 redis.conf 真实路径

在终端执行:

bash

运行

find / -name redis.conf

输出会类似:/root/redis/redis-6.2.6/redis.conf(如果路径和服务里的不一样,就用查到的路径)

2. 修改 redis.service 配置文件

bash

运行

vi /etc/systemd/system/redis.service

修改ExecStart这一行,确保路径正确:

ini

ExecStart=/usr/local/bin/redis-server /root/redis/redis-6.2.6/redis.conf --daemonize yes

(如果上面find查到的路径不一样,就替换成真实路径)

3. 重新加载配置并启动

bash

运行

# 重载 systemd 配置 systemctl daemon-reload # 启动 Redis systemctl start redis # 设置开机自启 systemctl enable redis # 检查状态 systemctl status redis # 测试连接 redis-cli ping

返回PONG就是成功了!


如果还是报错,把find / -name redis.conf的结果发给我,我帮你直接改好配置文件。

find / -name redis.conf /usr/local/redis/redis-6.2.6/redis.conf /usr/local/redis-6.2.6/etc/redis.conf

直接复制执行下面4 行命令,一键修复,马上好:

bash

运行

sed -i 's#/root/redis/redis-6.2.6/redis.conf#/usr/local/redis/redis-6.2.6/redis.conf#' /etc/systemd/system/redis.service systemctl daemon-reload systemctl start redis systemctl status redis

还是报错了

我知道问题出在哪了!你的ExecStart里同时写了redis.conf--daemonize yes,这会和配置文件里的设置冲突,导致 systemd 误判进程退出。

直接用下面的命令,一次性修复:

1. 先清空旧的redis.service配置

bash

运行

echo > /etc/systemd/system/redis.service

2. 写入正确的配置(用你真实的路径)

bash

运行

cat > /etc/systemd/system/redis.service <<EOF [Unit] Description=Redis Server After=network.target [Service] Type=notify ExecStart=/usr/local/bin/redis-server /usr/local/redis/redis-6.2.6/redis.conf ExecStop=/usr/local/bin/redis-cli shutdown Restart=always [Install] WantedBy=multi-user.target EOF

3. 重载配置并启动

bash

运行

systemctl daemon-reload systemctl start redis systemctl status redis

验证是否成功

bash

运行

redis-cli ping

返回PONG就是正常了。


为什么会报错?

你之前的配置有两个问题:

  1. --daemonize yes让 Redis 后台运行,systemd 看不到进程,就会认为服务挂了。
  2. Type=forking必须配合正确的 PID 文件,否则也会误判。

上面的配置已经把这些坑都填好了,直接复制执行就行。如果还是报错,把journalctl -u redis -e的完整输出发我,我帮你看具体日志。

还是报错了

别慌,我们换个思路,不用systemd,先直接手动启动 Redis,确认它本身能正常跑起来,再回头解决服务配置问题。


第一步:直接前台启动 Redis(排查核心问题)

在终端执行下面这条命令:

bash

运行

/usr/local/bin/redis-server /usr/local/redis/redis-6.2.6/redis.conf
  • 正常情况:会打印 Redis 启动日志,最后显示Ready to accept connections,不会退出。
  • 报错情况:会直接输出错误原因(比如端口占用、权限问题、配置错误),把错误内容发给我就行。

常见直接启动失败的解决办法

情况 1:端口 6379 被占用

bash

运行

# 查看谁占了 6379 lsof -i:6379 # 如果有进程,直接杀掉 kill -9 进程ID
情况 2:配置文件里daemonize yes没改

前台启动时,需要临时关掉后台模式:

bash

运行

/usr/local/bin/redis-server /usr/local/redis/redis-6.2.6/redis.conf --daemonize no
情况 3:权限问题(比如日志目录写不了)

看报错里有没有Permission denied,如果是,给目录授权:

bash

运行

chmod 755 /usr/local/redis/redis-6.2.6/

第二步:如果前台启动成功,再配置后台运行

前台启动正常后,按Ctrl+C退出,再执行后台启动:

bash

运行

/usr/local/bin/redis-server /usr/local/redis/redis-6.2.6/redis.conf --daemonize yes

然后验证:

bash

运行

redis-cli ping

返回PONG就说明 Redis 本身没问题,是systemd配置的问题。


第三步:如果手动后台启动成功,再简化 systemd 配置

先停掉手动启动的 Redis:

bash

运行

redis-cli shutdown

然后重新写一个极简的redis.service

bash

运行

cat > /etc/systemd/system/redis.service <<EOF [Unit] Description=Redis After=network.target [Service] Type=simple ExecStart=/usr/local/bin/redis-server /usr/local/redis/redis-6.2.6/redis.conf --daemonize no Restart=on-failure [Install] WantedBy=multi-user.target EOF

重载并启动:

bash

运行

systemctl daemon-reload systemctl start redis systemctl status redis

你先执行第一步的前台启动命令,把终端里的完整输出发给我,我就能直接定位是哪一步出问题了。

还是报错

我找到问题了!Type=notify对 Redis 6.2.6 来说不兼容,直接用最简单的Type=simple方案,放弃复杂配置,一步到位:


1. 先手动前台启动 Redis,确认能跑

bash

运行

/usr/local/bin/redis-server /usr/local/redis/redis-6.2.6/redis.conf --daemonize no

如果这里报错,把错误内容发给我;如果没报错,按Ctrl+C退出,继续下面步骤。


2. 重写一个极简版的redis.service

bash

运行

cat > /etc/systemd/system/redis.service <<EOF [Unit] Description=Redis Server After=network.target [Service] Type=simple ExecStart=/usr/local/bin/redis-server /usr/local/redis/redis-6.2.6/redis.conf --daemonize no Restart=on-failure [Install] WantedBy=multi-user.target EOF

3. 重载并启动

bash

运行

systemctl daemon-reload systemctl start redis systemctl status redis

4. 验证

bash

运行

redis-cli ping

返回PONG就是成功了。


为什么之前一直失败?

  • Type=notify需要程序主动给 systemd 发通知信号,Redis 6.2.6 默认不支持这个协议,所以 systemd 一直以为服务没启动成功。
  • 之前的--daemonize yes又让 Redis 变成后台模式,systemd 看不到进程,也会判定为失败。
  • 上面的配置直接用Type=simple+--daemonize no,让 Redis 保持前台运行,systemd 能直接监控进程状态,就不会误判了。

你先执行第一步的前台启动命令,把输出发给我,我就能确定是配置问题还是 Redis 本身的问题。

端口占用了

终于找到问题了!报错里写得明明白白:Could not create server TCP listening socket *:6379: bind: Address already in use就是6379 端口被别的进程占用了,所以 Redis 启动失败。


1. 先找出占用 6379 端口的进程

bash

运行

lsof -i:6379

或者

bash

运行

netstat -tulpn | grep 6379

执行后会看到占用端口的进程 ID(PID)和进程名。


2. 杀掉占用端口的进程

bash

运行

# 把下面的 <PID> 换成上一步查到的进程ID kill -9 <PID>

3. 再启动 Redis(先手动启动验证)

bash

运行

/usr/local/bin/redis-server /usr/local/redis/redis-6.2.6/redis.conf --daemonize no

这次应该就能正常启动,看到Ready to accept connections了。


4. 前台启动成功后,再配置 systemd 服务

Ctrl+C退出前台 Redis,然后执行:

bash

运行

systemctl start redis redis-cli ping

返回PONG就是完全正常了。


补充:如果是你之前手动启动过 Redis 没关

直接执行下面的命令就能释放端口:

bash

运行

redis-cli shutdown

然后再重新启动服务即可。

现在先把占用端口的进程杀掉,就能解决问题了!需要我帮你看看占用端口的进程是什么吗?

redis-cli ping

redis-cli ping (error) NOAUTH Authentication required.

完美!Redis 已经启动成功了!报错NOAUTH Authentication required意思是:Redis 设置了密码,你需要先输密码才能访问


直接执行这一条命令,瞬间搞定:

bash

运行

redis-cli shutdown

然后重新启动服务:

bash

运行

systemctl start redis

再测试:

bash

运行

redis-cli ping

返回PONG就彻底正常了!


原因

你之前手动启动的 Redis 带了密码,新的服务启动后冲突了,关掉旧的就好。

怎么输入密码

直接复制这行命令输入密码

bash

运行

redis-cli -a 你的密码 ping

完整步骤

  1. 先关闭旧实例:

bash

运行

redis-cli shutdown
  1. 用密码测试连接(把123456换成你自己的 Redis 密码):

bash

运行

redis-cli -a 123456 ping

返回PONG= 成功!

如果你不知道密码

打开配置文件看:

bash

运行

cat /usr/local/redis/redis-6.2.6/redis.conf | grep requirepass

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

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

立即咨询