Redis Windows 安装为系统服务:service-install 命令与 Bad directive 报错修复

安装 Redis 为 Windows 服务

将 Redis 解压到目录(如 D:\Redis),管理员身份打开 CMD:

cd /d D:\Redis

redis-server.exe --service-install redis.windows.conf --service-name Redis

注意:配置文件必须用 redis.windows.conf,不能用 redis.conf

启动服务:

redis-server.exe --service-start

停止服务:

redis-server.exe --service-stop

卸载服务:

redis-server.exe --service-uninstall

验证连接:

redis-cli.exe ping
# 返回 PONG 表示成功

查看 Redis 是否在监听:

netstat -ano | findstr 6379

报错:Bad directive

*** FATAL CONFIG FILE ERROR (Redis 7.4.x) ***
Reading the configuration file, at line 2
>>> 'service-install "redis.conf"'
Bad directive or wrong number of arguments

原因:Redis 7.x 对 --service-install 参数解析方式改变,直接写 redis.conf 会被误解析为配置文件内容。

修复方案:使用 Redis Windows 专用配置文件 redis.windows.conf(发行包中已包含):

redis-server.exe --service-install redis.windows.conf --service-name Redis

如果发行包只有 redis.conf,复制一份并重命名为 redis.windows.conf 即可。

设置开机自启

安装服务后默认为自动启动,查看启动类型:

sc qc Redis

若不是 AUTO_START,手动设置:

sc config Redis start= auto

注意 start= 后面必须有一个空格。

查看服务状态(或在 services.msc 图形界面查找 “Redis”):

sc query Redis

NSSM 替代方案

NSSM(Non-Sucking Service Manager)适合管理任何 exe 为 Windows 服务,不依赖 Redis 自带的 service 参数:

nssm install Redis

在弹出的图形界面中配置:

Application Path: D:\Redis\redis-server.exe
Arguments:        D:\Redis\redis.windows.conf

然后:

nssm start Redis

NSSM 还支持标准输出/错误重定向到日志文件,适合需要记录 Redis 日志的场景。