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

Opening port: bind: Address already in use

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

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

我的操作系统是Ubuntu 10.04。


当前回答

redis的服务名称为redis-server,可以使用以下命令禁用和停止redis:

sudo systemctl disable redis-server 
sudo systemctl stop redis-server 

其他回答

如果你知道哪个端口(默认值:6379)你的redis服务器正在运行,你可以选择选项1,或者你可以检查你的redis进程,你可以用选项2杀死

选项1:关闭端口上的进程:

check     : sudo lsof -t -i:6379
kill      : sudo kill `sudo lsof -t -i:6379`

选项2: 找到之前运行的Redis服务器:

 ps auxx | grep redis-server

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

kill -9 PID

现在启动你的redis服务器

redis-server /path/to/redis.conf 

如果您知道它将在哪个端口上运行(默认情况下是6379),您可以使用以下命令获取使用该端口的进程的pid,然后可以对相同的pid执行kill命令。

sudo lsof -i : <port> | awk '{print $2}'

上面的命令将为您提供pid。

杀死pid > <;

这将关闭您的服务器。

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

如果你在安装redis时做了安装(例如ubuntu),那么你可以这样做:

redis-cli shutdown

正如@yojimbo87所指出的:)

要优雅地使用密码关闭特定实例,而不诉诸于暴力杀死命令,请使用:

Redis-cli -p <port> -a <pass> shutdown

root@machine:~# ps aux
USER       PID %CPU %MEM    VSZ   RSS TTY      STAT START   TIME COMMAND
root       105  0.1  0.0  60552 10772 ?        Ssl  23:27   0:02 redis-server 127.0.0.1:10002
root       111  0.1  0.0  60552 10900 ?        Ssl  23:28   0:02 redis-server 127.0.0.1:10003
root       117  0.1  0.0  60552 10872 ?        Ssl  23:28   0:02 redis-server 127.0.0.1:10004

root@machine:~# redis-cli -p 10002 -a mypassword shutdown
Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe.

root@machine:~# ps aux
USER       PID %CPU %MEM    VSZ   RSS TTY      STAT START   TIME COMMAND
root       111  0.1  0.0  60552 10900 ?        Ssl  23:28   0:02 redis-server 127.0.0.1:10003
root       117  0.1  0.0  60552 10872 ?        Ssl  23:28   0:02 redis-server 127.0.0.1:10004

root@machine:~#

如果有大量的实例,就需要编写一个批处理脚本来循环遍历所有实例,以便主关机。