如何让Redis实现开机自启动(如何让redis开机启动)

Redis是一款目前比较成熟、非常受欢迎的key-value存储数据库,可以被用来做缓存服务器。正常情况下,如果服务器重新启动,就必须重新启动Redis,本文将介绍如何让Redis实现开机自启动,让Redis在服务器重新启动时也能自行开启。

一般情况下,安装完Redis之后,希望让Redis在服务器重新启动后也能自动启动,需要把redis加入开机启动服务中,推荐的脚本如下(CentOS安装Redis时):

vim /etc/rc.d/rc.local
#!/bin/sh
#
# This script will be executed *after* all the other init scripts.
# You can put your own initialization stuff in here if you don't
# want to do the full Sys V style init stuff.

touch /var/lock/subsys/local

#编写启动脚本
echo "redis-server /etc/redis.conf" >> /etc/rc.d/rc.local
#给它添加可执行权限
chmod +x /etc/rc.d/rc.local

上述脚本是把启动Redis服务的代码添加到rc.local文件,让其在服务器启动后自动运行,以实现自动启动Redis。

另外,也可以使用systemctl来配置自动启动,systemctl可以很好地管理服务的启动和停止,步骤如下:

#在/etc/systemd/system/目录中创建一个systemd配置文件
touch redis.service

#编辑该文件,加入以下指令
[Unit]
Description=Redis
After=network.target
[Service]
Type=forking
ExecStart=/path/to/redis-server /etc/redis.conf
ExecStop=/path/to/redis-cli shutdown
# 启用配置
systemctl enable redis.service
#启动服务
systemctl start redis.service

以上就是两种让Redis实现开机自启动的方法,任何一种都可以用来实现它。通过以上步骤,可以方便地让Redis在服务器重新启动时也能开启。


数据运维技术 » 如何让Redis实现开机自启动(如何让redis开机启动)