Linux下Redis開機自啟(Centos6)
阿新 • • 發佈:2019-01-05
1、設定redis.conf中daemonize為yes,確保守護程序開啟。
查詢redis配置檔案redis.conf
[[email protected] /]# find / -name redis.conf /usr/local/redis/redis.conf
編輯redis配置檔案
[[email protected] ~]# vim /usr/local/redis/redis.conf
命令列模式下輸入 /daemonize 查詢
將配置檔案中daemonize為yes
2、編寫開機自啟動指令碼
vi /etc/init.d/redis
#chkconfig: 2345 10 90 # description: Start and Stop redis PATH=/usr/local/bin:/sbin:/usr/bin:/bin REDISPORT=6379
EXEC=/usr/local/redis/redis-server
REDIS_CLI=/usr/local/redis/redis-cli
PIDFILE=/var/run/redis.pid
CONF="/usr/local/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 if [ "$?"="0" ] thenecho "Redis is running..." fi ;; stop) if [ ! -f $PIDFILE ] then echo "$PIDFILE exists, process is not running." else PID=$(cat $PIDFILE) echo "Stopping..." $REDIS_CLI -p $REDISPORT SHUTDOWN sleep 2 while [ -x $PIDFILE ] do echo "Waiting for Redis to shutdown..." sleep 1 done echo "Redis stopped" fi ;; restart|force-reload) ${0} stop ${0} start ;; *) echo "Usage: /etc/init.d/redis {start|stop|restart|force-reload}" >&2 exit 1 esac
3、寫完後儲存退出VI
4、設定許可權
[[email protected] init.d]# chmod 777 /etc/init.d/redis
5、啟動測試
/etc/init.d/redis start
啟動成功會提示如下資訊:
[[email protected] init.d]# /etc/init.d/redis start /etc/init.d/redis: line 1: kconfig:: command not found Starting Redis server... 4046:C 05 Jan 2019 12:00:02.611 # oO0OoO0OoO0Oo Redis is starting oO0OoO0OoO0Oo 4046:C 05 Jan 2019 12:00:02.611 # Redis version=5.0.0, bits=32, commit=00000000, modified=0, pid=4046, just started 4046:C 05 Jan 2019 12:00:02.611 # Configuration loaded Redis is running...
使用redis-cli測試:(可能會出現auth認證問題,應該是設定了認證密碼,輸入密碼既可以啦)
[[email protected] init.d]# redis-cli 127.0.0.1:6379> set fgf 123456 (error) NOAUTH Authentication required. 127.0.0.1:6379> auth 123456 OK 127.0.0.1:6379> set fgf 123 OK 127.0.0.1:6379> get fgf "123" 127.0.0.1:6379>
6、設定開機自啟動
[[email protected] init.d]# chkconfig redis on
7、關機重啟測試
reboot
然後在用redis-cli測試即可