我显然有一个redis-server实例正在运行,因为当我试图通过输入redis-server启动一个新服务器时,我受到以下欢迎:

Opening port: bind: Address already in use

我不知道如何停止这个服务器,并启动一个新的。

当我在CLI中输入时,是否有任何命令可以附加到redis-server ?

我的操作系统是Ubuntu 10.04。


当前回答

Redis有配置参数pidfile(例如/etc/redis.conf -检查Redis源代码),例如:

# If a pid file is specified, Redis writes it where specified at startup
# and removes it at exit.
#
# When the server runs non daemonized, no pid file is created if none is
# specified in the configuration. When the server is daemonized, the pid file
# is used even if not specified, defaulting to "/var/run/redis.pid".
#
pidfile /var/run/redis.pid

如果它已经设置或可以设置,可以使用ps + grep来搜索进程id (pid),而不是像这样使用:

kill $(cat /var/run/redis.pid)

如果需要,一个可以使redis停止脚本像这样(改编默认redis 5.0初始化。D脚本在redis源代码):

PIDFILE=/var/run/redis.pid
if [ ! -f $PIDFILE ]
then
    echo "$PIDFILE does not exist, process is not running"
else
    PID=$(cat $PIDFILE)
    echo "Stopping ..."
    kill $PID
    while [ -x /proc/${PID} ]
    do
        echo "Waiting for Redis to shutdown ..."
        sleep 1
    done
    echo "Redis stopped"
fi

其他回答

SystemD, Ubuntu 16.04:

$ sudo systemctl is-active redis-server
active

$ sudo systemctl is-enabled redis-server
enabled

$ sudo systemctl disable redis-server
Synchronizing state of redis-server.service with SysV service script with /lib/systemd/systemd-sysv-install.
Executing: /lib/systemd/systemd-sysv-install disable redis-server
Removed /etc/systemd/system/redis.service.

$ sudo systemctl stop redis-server

一个更干净、更可靠的方法是进入redis-cli,然后输入shutdown

在redis-cli中,输入help @server,你会在列表底部看到:

SHUTDOWN - summary:同步保存数据集到磁盘,然后 从:0.07开始关闭服务器

如果你在终端中运行一个redis-server实例,你会看到:

User requested shutdown...
[6716] 02 Aug 15:48:44 * Saving the final RDB snapshot before exiting.
[6716] 02 Aug 15:48:44 * DB saved on disk
[6716] 02 Aug 15:48:44 # Redis is now ready to exit, bye bye...

连接到节点实例并使用shutdown命令,或者如果你在ubuntu上,你可以尝试通过init.d重新启动redis服务器:

/etc/init.d/redis-server restart

或者stop/start it:

/etc/init.d/redis-server stop
/etc/init.d/redis-server start

在Mac

redis-cli shutdown

MacOSX -它工作了:)

步骤1:找到之前运行的Redis服务器

ps auxx | grep redis-server

步骤2:通过查找PID(进程ID) - Redis Sever来杀死特定的进程

kill -9 PID

通常这个问题出现后,我关闭我的电脑(或离开运行)不规则的方式。我认为端口被卡住打开,而进程停止,但继续绑定到前一个端口。

9/10次修复可以是:

$ ps aux | grep redis

-> MyUser 2976  0.0  0.0  2459704    320   ??  S    Wed01PM   0:29.94 redis-server *:6379

$ kill 2976

$ redis-server

准备好了。