Centos下设置Redis开机重启

xiaohai 2021-05-05 21:59:26 3485人围观 标签: Redis 
简介Centos下设置Redis开机重启

1、首先复制配置文件redis.conf(在安装目录下)到/etc/redis/下(如果/etc下没有redis目录,先创建)

[root@localhost utils]# mkdir -p /etc/redis
[root@localhost utils]# cp /apps/redis/redis.conf /etc/redis/6379.conf #这里为什么是6379,这里主要是取决于启动脚本
[root@localhost utils]# ll /etc/redis/
total 60
-rw-r--r-- 1 root root 58766 Jun  6 16:28 6379.conf

2、设置/etc/redis/6379.conf中daemonize为yes,确保守护进程开启,也就是在后台可以运行

daemonize yes

3、复制redis启动脚本(在安装目录下/apps/redis/utils/redis_init_script)文件到/etc/init.d下

[root@localhost utils]# cp /apps/redis/utils/redis_init_script /etc/init.d/redis

4、修改启动脚本

#1、在/etc/init.d/redis文件的头部添加下面两行注释代码,也就是在文件中#!/bin/sh的下方添加
# chkconfig: 2345 10 90  
# description: Start and Stop redis 

#2、修改EXEC和CLIEXEC路径为自己安装的路径
EXEC=/apps/redis/src/redis-server
CLIEXEC=/apps/redis/src/redis-cli

这里说明下,上面的配置文件为什么是6379.conf,主要在于启动脚本的这行代码:CONF=”/etc/redis/${REDISPORT}.conf”,所以为了简便起见使用了6379.conf,也可以根据个人习惯修改.

5、启动和停止
打开redis命令:service redis start
关闭redis命令:service redis stop

[root@localhost utils]# service redis start
Starting Redis server...
6820:C 06 Jun 16:34:51.045 # oO0OoO0OoO0Oo Redis is starting oO0OoO0OoO0Oo
6820:C 06 Jun 16:34:51.045 # Redis version=4.0.9, bits=64, commit=00000000, modified=0, pid=6820, just started
6820:C 06 Jun 16:34:51.045 # Configuration loaded
[root@localhost utils]# ps -axu|grep redis
root      5095  0.0  0.1  16272  1424 pts/1    S+   16:03   0:00 src/redis-cli
root      6821  0.0  0.2  41676  2212 ?        Ssl  16:34   0:00 /apps/redis/src/redis-server 127.0.0.1:6379
root      6826  0.0  0.0 112660   968 pts/0    R+   16:34   0:00 grep --color=auto redis
[root@localhost utils]# 
[root@localhost utils]# 
[root@localhost utils]# service redis stop
Stopping ...
Redis stopped
[root@localhost utils]# ps -axu|grep redis
root      5095  0.0  0.1  16272  1424 pts/1    S+   16:03   0:00 src/redis-cli
root      6839  0.0  0.0 112660   964 pts/0    R+   16:35   0:00 grep --color=auto redis

6、设置为开机启动命令
设为开机启动:chkconfig redis on
设为开机关闭:chkconfig redis off