您的位置:首页 > 数据库 > Redis

CentOS7配置redis开机启动

2016-04-12 00:00 621 查看
摘要: CentOS7配置redis开机启动。系统开机启动时会去加载/etc/init.d/下面的脚本,通常而言每个脚本文件会自定义实现程序的启动;若想将新的程序开机自启动,只需在该目录下添加一个自定义启动程序的脚本,然后设置相应规则即可(如我在/etc/init.d/下新建一个redis的脚本,开机启动时会去加载执行该脚本;当然这只是开机设置自启动的一种方法)

CentOS7配置redis开机启动

[摘要:一:道理先容 先看下当前体系已设置的开机自启动顺序有哪些:

[root@localhost init.d]# chkconfig --list


Note: This output shows SysV services only and does]

一:原理介绍

先看下当前系统已设置的开机自启动程序有哪些:

[root@localhost init.d]# chkconfig --list


Note: This output shows SysV services only and does not include native

systemd services. SysV configuration data might be overridden by native

systemd configuration.

If you want to list systemd services use 'systemctl list-unit-files'.

To see services enabled on particular target use

'systemctl list-dependencies [target]'.

netconsole     0:off 1:off 2:off 3:off 4:off 5:off 6:off
network         0:off 1:off 2:on 3:on 4:on 5:on 6:off
[root@localhost init.d]# ls /etc/init.d/ -lrt
total 40
-rwxr-xr-x. 1 root root  6470 Jan 15  2015 network
-rwxr-xr-x. 1 root root  2989 Jan 15  2015 netconsole
-rw-r--r--. 1 root root 13430 Jan 15  2015 functions
-rw-r--r--. 1 root root  1160 Mar  6  2015 README
-rw-r--r--. 1 root root    37 Nov 30 05:43 dump.rdb


从上面可以看出系统已设置的开机自启动程序有network和netconsole,新增开机自启动程序,基本原理为:

系统开机启动时会去加载/etc/init.d/下面的脚本,通常而言每个脚本文件会自定义实现程序的启动;若想将新的程序开机自启动,只需在该目录下添加一个自定义启动程序的脚本,然后设置相应规则即可(如我在/etc/init.d/下新建一个redis的脚本,开机启动时会去加载执行该脚本;当然这只是开机设置自启动的一种方法)

二:实践

废话少说,直接来步骤:

1、设置redis.conf中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-3.0.0-beta5/src/redis-server
REDIS_CLI=/usr/local/redis-3.0.0-beta5/src/redis-cli
PIDFILE=/var/run/redis.pid
CONF="/usr/local/redis-3.0.0-beta5/redis.conf"
AUTH="1234"
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" ]
then
echo "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、设置权限

chmod 755 redis


5、启动测试

/etc/init.d/redis start


启动成功会提示如下信息:

Starting Redis server...
2358:M 30 Nov 05:34:16.991 * Increased maximum number of open files to 10032 (it was originally set to 1024).
_._
_.-``__ ''-._
_.-``    `.  `_.  ''-._          Redis 2.9.54 (00000000/0) 64 bit
.-`` .-```.  ```\/    _.,_ ''-._
(    '      ,      .-`  | `,    )    Running in stand alone mode
|`-._`-...-` __...-.``-._|'` _.-'|    Port: 6379
|    `-._  `._    /    _.-'    |    PID: 2358
`-._    `-._  `-./  _.-'    _.-'
|`-._`-._    `-.__.-'    _.-'_.-'|
|    `-._`-._        _.-'_.-'    |          http://redis.io `-._    `-._`-.__.-'_.-'    _.-'
|`-._`-._    `-.__.-'    _.-'_.-'|
|    `-._`-._        _.-'_.-'    |
`-._    `-._`-.__.-'_.-'    _.-'
`-._    `-.__.-'    _.-'
`-._        _.-'
`-.__.-'
2358:M 30 Nov 05:34:16.993 # Server started, Redis version 2.9.54
2358:M 30 Nov 05:34:16.993 # WARNING overcommit_memory is set to 0! Background save may fail under low memory condition. To fix this issue add 'vm.overcommit_memory = 1' to /etc/sysctl.conf and then reboot or run the command 'sysctl vm.overcommit_memory=1' for this to take effect.
2358:M 30 Nov 05:34:16.993 * The server is now ready to accept connections on port 6379


使用redis-cli测试:

[best_husband@localhost src]$ /usr/local/redis-3.0.0-beta5/src/redis-cli
127.0.0.1:6379> set name usbdrivers
OK
127.0.0.1:6379> get name
"usbdrivers"
127.0.0.1:6379> quit
[best_husband@localhost src]$


6、设置开机自启动

# 尝试启动或停止redis
service redis start
service redis stop

# 开启服务自启动
chkconfig redis on


7、关机重启测试

reboot


然后在用redis-cli测试即可。

说明:本人最初测试时在/etc/init.d/redis脚本文件里没有加上最开头的两行,然后执行chkconfig redis on时报错:service redis does not support

必须把下面两行注释放在/etc/init.d/redis文件靠前的注释中:

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


上面的注释的意思是,redis服务必须在运行级2,3,4,5下被启动或关闭,启动的优先级是90,关闭的优先级是10。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  redis 开机启动 centos7