Redis:启动和关闭

redis 的启动和关闭非常简单,目前发现两种 redis 启动方式,如下:

方式一:直接启动

编译安装完 redis 后会在 src 目录下产生 redis-server 等可执行文件, 配置好 redis_6379.conf 文件后直接启动即可。

配置文件 redis_6379.conf

1
2
3
4
5
6
daemonize yes  
pidfile /usr/local/redis/redis_6379.pid
port 6379
logfile "/usr/local/redis/redis.log"
dbfilename dump.rdb
dir /usr/local/redis

备注: 仅修改以上配置参数。

启动 redis

1
[redis@db1 redis]$ redis-server redis_6379.conf

关闭 redis

1
[redis@db1 redis]$ redis-cli shutdown

方式二:通过服务启动

redis 启动脚本位于 redis-2.8.17/utils/ 目录下, 名为 redis_init_script ,通过以下步骤配置

copy 文件

1
# cp redis_init_script /etc/init.d/redisd

修改 redisd 文件

1
2
3
4
5
6
7
8
9
10
#!/bin/sh  
# Simple Redis init.d script conceived to work on Linux systems
# as it does use of the /proc filesystem.

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

PIDFILE=/usr/local/redis/redis_${REDISPORT}.pid
CONF="/usr/local/redis/redis_${REDISPORT}.conf"

备注:主要根据实际情况修改 redisd 文件头部配置项。

启动 redis

1
2
[root@db1 init.d]# /etc/init.d/redisd start  
Starting Redis server...

关闭 redis

1
2
3
[root@db1 init.d]# /etc/init.d/redisd stop   
Stopping ...
Redis stopped

加入自启动

1
2
[root@db1 init.d]# chkconfig redisd on  
service redisd does not support chkconfig

备注: 加入 redis 服务自启动报错,需要修改 /etc/init.d/redisd 文件,增加以下两行

1
2
# chkconfig: 2345 90 10   
# description: Redis is a persistent key-value database

再次执行 chkconfig

1
[root@db1 init.d]# chkconfig redisd on

附: 本实验配置的 redisd 文件

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
[root@db1 init.d]# cat /etc/init.d/redisd   
#!/bin/sh
# chkconfig: 2345 90 10
# description: Redis is a persistent key-value database
# Simple Redis init.d script conceived to work on Linux systems
# as it does use of the /proc filesystem.

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

PIDFILE=/usr/local/redis/redis_${REDISPORT}.pid
CONF="/usr/local/redis/redis_${REDISPORT}.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

参考

原创文章,作者:745907710,如若转载,请注明出处:https://blog.ytso.com/239592.html

(0)
上一篇 2022年2月12日
下一篇 2022年2月12日

相关推荐

发表回复

登录后才能评论