无法连接到127.0.0.1:0的Redis:连接被拒绝(当使用init脚本关闭带有unix套接字的redis服务器时)

时间:2017-10-01 23:01:41

标签: redis redis-cli redis-server

我无法通过init脚本关闭我的redis服务器。当我执行sudo reboot时,这会导致我的机器挂起巨大的副作用

我使用canonical guide新安装了redis,将其配置为接受unix套接字上的连接,现在正尝试停止服务器,以便我可以恢复以前保存的.rdb转储。

但我一直得到以下内容:

Could not connect to Redis at 127.0.0.1:0: Connection refused
Waiting for Redis to shutdown ...
Waiting for Redis to shutdown ...
Waiting for Redis to shutdown ...
Waiting for Redis to shutdown ...
Waiting for Redis to shutdown ...
Waiting for Redis to shutdown ...
Waiting for Redis to shutdown ...

我使用的命令是sudo /etc/init.d/redis-server stop。请注意,在连接到shutdown后,我仍然可以发出redis-cli -s /var/run/redis.sock,但我需要使用init脚本。

为什么不会关闭redis,我该如何解决这种情况?版本为Redis server v=4.0.2 sha=00000000:0 malloc=jemalloc-4.0.3 bits=64 build=8ddba2e6a24ce078

我自然在配置文件中设置了port 0,以及unixsocket /var/run/redis.sockunixsocketperm 755

此外,脚本文件的内容是:

!/bin/sh
#
# Simple Redis init.d script conceived to work on Linux systems
# as it does use of the /proc filesystem.

REDISPORT=0
EXEC=/usr/local/bin/redis-server
CLIEXEC=/usr/local/bin/redis-cli

PIDFILE=/var/run/redis.pid
CONF="/etc/redis/redis.conf"

case "$1" in
    start)
        if [ -f $PIDFILE ]
        then
                echo "$PIDFILE exists, process is already running or crashed"
        else
                echo "Starting Redis server..."
                $EXEC $CONF
        fi
        ;;
    stop)
        if [ ! -f $PIDFILE ]
        then
                echo "$PIDFILE does not exist, process is not running"
        else
                PID=$(cat $PIDFILE)
                echo "Stopping ..."
                $CLIEXEC -p $REDISPORT shutdown
                while [ -x /proc/${PID} ]
                do
                    echo "Waiting for Redis to shutdown ..."
                    sleep 1
                done
                echo "Redis stopped"
        fi
        ;;
    *)
        echo "Please use start or stop as first argument"
        ;;
esac

注意REDISPORT=0,应该如此。目前,这并不支持停止和启动redis服务器。

1 个答案:

答案 0 :(得分:0)

我对问题中发布的init脚本进行了自定义。我不得不使redis'init脚本支持基于unix socket的关闭。它不支持OOTB。

以下是我的剧本最终的结果:

<asp:ListBox ID="ListBox1" runat="server" SelectionMode="Multiple">
    <asp:ListItem Value="1" Selected="True">aaa</asp:ListItem>
    <asp:ListItem Value="2" Selected="True">bbb</asp:ListItem>
    <asp:ListItem Value="3">ccc</asp:ListItem>
    <asp:ListItem Value="4">ddd</asp:ListItem>
    <asp:ListItem Value="5" Selected="True">eee</asp:ListItem>
    <asp:ListItem Value="6">fff</asp:ListItem>
</asp:ListBox>

更改脚本后,尝试发出#!/bin/sh # # Simple Redis init.d script conceived to work on Linux systems # as it does use of the /proc filesystem. REDISPORT=0 EXEC=/usr/local/bin/redis-server CLIEXEC=/usr/local/bin/redis-cli REDISSOCKET="/var/run/redis.sock" PIDFILE=/var/run/redis.pid CONF="/etc/redis/redis.conf" case "$1" in start) if [ -f $PIDFILE ] then echo "$PIDFILE exists, process is already running or crashed" else echo "Starting Redis server..." $EXEC $CONF fi ;; stop) if [ ! -f $PIDFILE ] then echo "$PIDFILE does not exist, process is not running" else echo "Stopping ..." PID=$(cat $PIDFILE) if [ $REDISPORT -eq 0 ] && [ -n "$REDISSOCKET" ] then $CLIEXEC -s $REDISSOCKET shutdown else $CLIEXEC -p $REDISPORT shutdown fi while [ -x /proc/${PID} ] do echo "Waiting for Redis to shutdown ..." sleep 1 done echo "Redis stopped" fi ;; *) echo "Please use start or stop as first argument" ;; esac 。如果将redis-server添加到必要的运行级别,它应该可以工作。

否则只需尝试sudo service redis-server stop

注意:此处解决了类似的问题:https://groups.google.com/forum/#!topic/redis-db/o-fC_B-fjPs