start-stop-daemon: matching on world-writable pidfile /var/run/redis/redis-server.pid is insecurefailed

天琊藍發表於2020-08-16

Microsoft Store上 看到最新的Ubuntu 20.04 LTS 已經適配到WSL上了, 於是解除安裝了老版本 18.04 LTS,安裝上了最新版本的。

第一次啟動會比較慢,需耐心等待(接著需建立使用者帳戶和密碼)

Installing, this may take a few minutes...

看了下WSL的官方文件,發現WSL對各種資料庫的支援(Ref-1)還真是蠻全的。 更讓我驚喜的是WSL和Window其實可以互操作(Ref-2): 既可以從 Windows 命令列執行 Linux 工具,也可以從 Linux 命令列執行 Windows 工具。以前一直以為WSL只是提供了 Linux 模擬環境,可以讓使用 Windows 的開發人員敲敲 Linux 命令,哈哈,認識真的太淺薄了。這對於Redis尤其有用,再也不用安裝萬年不變的3.2.100(Ref-3)了(當然了,你也可以在Docker使用Redis)。

啟動執行Redis,解決啟動問題

安裝好Redis以後,在WSL中執行Redis

root@WSL:~# redis-server --version
Redis server v=5.0.7 sha=00000000:0 malloc=jemalloc-5.2.1 bits=64 build=636cde3b5c7a3923

root@WSL:~# sudo service redis-server start
Starting redis-server: redis-server.

root@WSL:~# redis-cli
127.0.0.1:6379> set Redis "Hello Redis!"
OK
127.0.0.1:6379> get Redis
"Hello Redis!"

設定Redis在後臺執行,並檢視當前程式(有兩個Redis例項在執行,注意後臺例項的啟動PID是1738,執行子PID是1739)

root@WSL:~# redis-server --daemonize yes
1738:C 16 Aug 2020 18:03:11.055 # oO0OoO0OoO0Oo Redis is starting oO0OoO0OoO0Oo
1738:C 16 Aug 2020 18:03:11.055 # Redis version=5.0.7, bits=64, commit=00000000, modified=0, pid=1738, just started
1738:C 16 Aug 2020 18:03:11.056 # Configuration loaded

root@WSL:~# ps -aux --sort=tty,command
USER       PID %CPU %MEM    VSZ   RSS TTY      STAT START   TIME COMMAND
root         1  0.0  0.0   8892   284 ?        Ssl  17:34   0:00 /init
redis     1602  0.0  0.0  61300  3836 ?        Ssl  17:44   0:00 /usr/bin/redis-server 127.0.0.1:6379
root      1739  0.0  0.0  55668  2828 ?        Ssl  18:03   0:00 redis-server *:6379
kevin     1643  0.0  0.0  18076  3528 tty1     S    17:46   0:00 -bash
root      1721  0.0  0.0  18080  3516 tty1     S    17:59   0:00 -bash
root      1642  0.0  0.0   8900   204 tty1     Ss   17:46   0:00 /init
root      1743  0.0  0.0  18880  1996 tty1     R    18:06   0:00 ps -aux --sort=tty,command
root      1720  0.0  0.0  18908  2680 tty1     S    17:59   0:00 sudo -i

把玩了一番,發現Redis無法啟動,搜了以下,通過刪除/var/run/redis/redis-server.pid 解決了問題。

root@WSL:~# sudo service redis-server start
Starting redis-server: start-stop-daemon: matching on world-writable pidfile /var/run/redis/redis-server.pid is insecurefailed

root@WSL:~# rm -f /var/run/redis/redis-server.pid

root@WSL:~# sudo service redis-server start
Starting redis-server: redis-server.

root@WSL:~# sudo service redis-server status
 * redis-server is running

通過CMDPowerShell執行 Redis

CMD/PowerShell 通過wsl命令進入WSL環境, 這時 WSL 會自動掛載當前目錄,也可以直接wsl redis-server(Ref-4)啟動 Redis。Redis在standalone模式下,控制檯無法進行互動。

C:\>wsl
kevin@WSL:/mnt/c# redis-server
2093:C 16 Aug 2020 18:44:15.097 # oO0OoO0OoO0Oo Redis is starting oO0OoO0OoO0Oo
2093:C 16 Aug 2020 18:44:15.097 # Redis version=5.0.7, bits=64, commit=00000000, modified=0, pid=2093, just started
2093:C 16 Aug 2020 18:44:15.097 # Warning: no config file specified, using the default config. In order to specify a config file use redis-server /path/to/redis.conf
2093:M 16 Aug 2020 18:44:15.098 * Increased maximum number of open files to 10032 (it was originally set to 1024).
                _._
           _.-``__ ''-._
      _.-``    `.  `_.  ''-._           Redis 5.0.7 (00000000/0) 64 bit
  .-`` .-```.  ```\/    _.,_ ''-._
 (    '      ,       .-`  | `,    )     Running in standalone mode
 |`-._`-...-` __...-.``-._|'` _.-'|     Port: 6379
 |    `-._   `._    /     _.-'    |     PID: 2093
  `-._    `-._  `-./  _.-'    _.-'
 |`-._`-._    `-.__.-'    _.-'_.-'|
 |    `-._`-._        _.-'_.-'    |           http://redis.io
  `-._    `-._`-.__.-'_.-'    _.-'
 |`-._`-._    `-.__.-'    _.-'_.-'|
 |    `-._`-._        _.-'_.-'    |
  `-._    `-._`-.__.-'_.-'    _.-'
      `-._    `-.__.-'    _.-'
          `-._        _.-'
              `-.__.-'

2093:M 16 Aug 2020 18:44:15.109 # WARNING: The TCP backlog setting of 511 cannot be enforced because /proc/sys/net/core/somaxconn is set to the lower value of 128.
2093:M 16 Aug 2020 18:44:15.110 # Server initialized
2093:M 16 Aug 2020 18:44:15.111 # WARNING overcommit_memory is set to 0! Background save may fail under low memory condition. To fix this issue add 'vm.overcommit_memory = 1' to /etc/sysctl.conf and then reboot or run the command 'sysctl vm.overcommit_memory=1' for this to take effect.
2093:M 16 Aug 2020 18:44:15.113 * Ready to accept connections

直接掛載C盤根目錄可能導致CTRL+C無法退出, 在其他控制檯,通過kill -9 PID可終止該Redis例項(2093)的執行。

2093:signal-handler (1597575573) Received SIGINT scheduling shutdown...
2093:M 16 Aug 2020 18:59:33.301 # User requested shutdown...
2093:M 16 Aug 2020 18:59:33.302 * Saving the final RDB snapshot before exiting.
2093:M 16 Aug 2020 18:59:33.303 # Failed opening the RDB file dump.rdb (in server root dir /mnt/c) for saving: Permission denied
2093:M 16 Aug 2020 18:59:33.304 # Error trying to save the DB, can't exit.
2093:M 16 Aug 2020 18:59:33.305 # SIGTERM received but errors trying to shut down the server, check the logs for more information

還可以通過redis-server &使Redis在後臺執行, 和直接redis-server執行不同,CTRL+C關閉以後,通過ps命令檢視,發現PID(2329)仍在執行, 但是這種後臺模式和守護模式(daemonize yes)的STAT(Ref-5) 是不同的。

kevin@WSL:/$ redis-server &
[1] 2329
kevin@WSL:/$ 2329:C 16 Aug 2020 19:36:10.196 # oO0OoO0OoO0Oo Redis is starting oO0OoO0OoO0Oo
2329:C 16 Aug 2020 19:36:10.196 # Redis version=5.0.7, bits=64, commit=00000000, modified=0, pid=2329, just started
2329:C 16 Aug 2020 19:36:10.196 # Warning: no config file specified, using the default config. In order to specify a config file use redis-server /path/to/redis.conf
2329:M 16 Aug 2020 19:36:10.197 * Increased maximum number of open files to 10032 (it was originally set to 1024).
                _._
           _.-``__ ''-._
      _.-``    `.  `_.  ''-._           Redis 5.0.7 (00000000/0) 64 bit
  .-`` .-```.  ```\/    _.,_ ''-._
 (    '      ,       .-`  | `,    )     Running in standalone mode
 |`-._`-...-` __...-.``-._|'` _.-'|     Port: 6379
 |    `-._   `._    /     _.-'    |     PID: 2329
  `-._    `-._  `-./  _.-'    _.-'
 |`-._`-._    `-.__.-'    _.-'_.-'|
 |    `-._`-._        _.-'_.-'    |           http://redis.io
  `-._    `-._`-.__.-'_.-'    _.-'
 |`-._`-._    `-.__.-'    _.-'_.-'|
 |    `-._`-._        _.-'_.-'    |
  `-._    `-._`-.__.-'_.-'    _.-'
      `-._    `-.__.-'    _.-'
          `-._        _.-'
              `-.__.-'

2329:M 16 Aug 2020 19:36:10.202 # WARNING: The TCP backlog setting of 511 cannot be enforced because /proc/sys/net/core/somaxconn is set to the lower value of 128.
2329:M 16 Aug 2020 19:36:10.202 # Server initialized
2329:M 16 Aug 2020 19:36:10.202 # WARNING overcommit_memory is set to 0! Background save may fail under low memory condition. To fix this issue add 'vm.overcommit_memory = 1' to /etc/sysctl.conf and then reboot or run the command 'sysctl vm.overcommit_memory=1' for this to take effect.
2329:M 16 Aug 2020 19:36:10.202 * Ready to accept connections
^C
kevin@WSL:/$ ps -aux --sort=tty,command
USER       PID %CPU %MEM    VSZ   RSS TTY      STAT START   TIME COMMAND
root         1  0.0  0.0   8892   264 ?        Ssl  17:34   0:00 /init
redis     1602  0.0  0.0  61300  3836 ?        Ssl  17:44   0:00 /usr/bin/redis-server 127.0.0.1:6379
root      1739  0.0  0.0  55668  2828 ?        Ssl  18:03   0:00 redis-server *:6379
kevin     1643  0.0  0.0  18076  3084 tty1     S    17:46   0:00 -bash
root      1721  0.0  0.0  18212  3432 tty1     S    17:59   0:00 -bash
root      1642  0.0  0.0   8900   176 tty1     Ss   17:46   0:00 /init
root      1720  0.0  0.0  18908  2140 tty1     S    17:59   0:00 sudo -i
kevin     2178  0.0  0.0  18208  3684 tty2     S    19:12   0:00 -bash
root      2177  0.0  0.0   8900   200 tty2     Ss   19:12   0:00 /init
kevin     2333  0.0  0.0  18880  2000 tty2     R    19:36   0:00 ps -aux --sort=tty,command
kevin     2329  0.1  0.0  55668  4080 tty2     Sl   19:36   0:00 redis-server

相關文章