Windows 10 Linux 子系统 Redis 无法自动启动

Windows 10 Linux 子系统 Redis 无法自动启动

我已经在 Windows 10 Linux 子系统上安装了 Redis。我按照以下说明操作:https://redis.io/topics/quickstart并且我已确保遵循了第节中提到的所有步骤更正确地安装 Redis正确。

但是尝试运行以下命令

sudo update-rc.d redis_6379 defaults

我收到以下错误:

~$ sudo update-rc.d redis_6379 defaults
insserv: warning: script 'K01redis_6379' missing LSB tags and overrides
insserv: warning: script 'redis_6379' missing LSB tags and overrides
initctl: Unable to connect to Upstart: Failed to connect to socket /com/ubuntu/upstart: Connection refused
The script you are attempting to invoke has been converted to an Upstart
job, but lsb-header is not supported for Upstart jobs.
insserv: warning: script 'cron' missing LSB tags and overrides
insserv: Default-Start undefined, assuming empty start runlevel(s) for script `cron'
insserv: Default-Stop  undefined, assuming empty stop  runlevel(s) for script `cron'
initctl: Unable to connect to Upstart: Failed to connect to socket /com/ubuntu/upstart: Connection refused
The script you are attempting to invoke has been converted to an Upstart
job, but lsb-header is not supported for Upstart jobs.
insserv: warning: script 'friendly-recovery' missing LSB tags and overrides
insserv: Default-Start undefined, assuming empty start runlevel(s) for script `friendly-recovery'
insserv: Default-Stop  undefined, assuming empty stop  runlevel(s) for script `friendly-recovery'

手动启动服务器的工作原理:

~$ sudo /etc/init.d/redis_6379 start
Starting Redis server...
~$ redis-cli ping
PONG

有人能帮我在 WLS 上配置 Redis 以便它像在实际的 Ubuntu 机器上一样在后台自动启动吗?

谢谢。

答案1

下面是关于如何配置 redis 在 Windows 启动时作为后台任务启动的说明。

  1. 安装 WSL(已使用 Ubuntu 18.04 版本测试)

  2. 在 WSL 中安装 redis-server:

    sudo apt install redis-server
    
  3. 为您的用户添加 sudo 权限,以便无需密码执行服务命令打开 sudoers 文件 sudo visudo 并在末尾添加:

    your_username ALL=NOPASSWD:/usr/sbin/service redis-server
    

    或者如果你想禁用 sudo 密码,通常将其添加到末尾:

    your_username ALL=(ALL:ALL) NOPASSWD:ALL
    
  4. 在启动文件夹中创建 vbs 文件,例如 start-redis.vbs (打开运行并输入 shell:startup)

    在 vbs 文件中插入以下内容:

    Set oShell = CreateObject("WScript.Shell")
    oShell.Run "wsl", 0
    oShell.Run "bash -c ""sudo service redis-server start --daemonize yes"""
    

就是这样。您可以通过运行 vbs 脚本然后在 WSL 终端内运行 htop 来尝试。您应该看到 redis 正在运行。

我已经发布了这些说明在 GitHub 上

答案2

  1. 您可以等待 Windows 的下一个版本,或者安装支持后台服务的内部版本https://blogs.msdn.microsoft.com/commandline/2017/12/04/background-task-support-in-wsl/
  2. 您可以安装 Windows redis 服务(不需要 Linux 子系统) https://github.com/MicrosoftArchive/redis个人经验是,这对于单个测试实例来说效果很好,但在尝试创建可靠的集群时会带来很大的麻烦。
  3. 将 Linux 实例作为 VM 或在单独的服务器上运行。

如果这是用于测试代码的开发人员实例,则使用 2 或 3。如果是生产部署,并且您希望运行集群,则使用 3。WSL 是为运行小脚本、测试事物而创建的,但我发现它作为 Linux 服务器的替代品并不是很友好。YMMV

相关内容