redis的三种启动方式(后台运行)

redis的启动方式
1.直接启动
进入redis根目录,执行命令:
#加上‘&’号使redis以后台程序方式运行

./redis-server &

2.通过指定配置文件启动
可以为redis服务启动指定配置文件,例如配置为/etc/redis/6379.conf
进入redis根目录,输入命令:

redis-server redis.windows.conf

#如果更改了端口,使用redis-cli客户端连接时,也需要指定端口,例如:

redis-cli -p 6380

3.使用redis启动脚本设置开机自启动
启动脚本 redis_init_script 位于位于Redis的 /utils/ 目录下,redis_init_script脚本代码如下:

#!/bin/sh
#
# Simple Redis init.d script conceived to work on Linux systems
# as it does use of the /proc filesystem.
 
#redis服务器监听的端口
REDISPORT=6379
 
#服务端所处位置
EXEC=/usr/local/bin/redis-server
 
#客户端位置
CLIEXEC=/usr/local/bin/redis-cli
 
#redis的PID文件位置,需要修改
PIDFILE=/var/run/redis_${REDISPORT}.pid
 
#redis的配置文件位置,需将${REDISPORT}修改为文件名
CONF="/etc/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

根据启动脚本,将修改好的配置文件复制到指定目录下,用root用户进行操作:

mkdir /etc/redis
cp redis.conf /etc/redis/6379.conf

将启动脚本复制到/etc/init.d目录下,本例将启动脚本命名为redisd(通常都以d结尾表示是后台自启动服务)。

cp redis_init_script /etc/init.d/redisd

设置为开机自启动,直接配置开启自启动 chkconfig redisd on 发现错误: service redisd does not support chkconfig

解决办法,在启动脚本开头添加如下注释来修改运行级别:

#!/bin/sh
# chkconfig:   2345 90 10

再设置即可

#设置为开机自启动服务器
chkconfig redisd on
#打开服务
service redisd start
#关闭服务
service redisd stop
Redis是一个开源的内存数据结构存储系统,通常用于缓存和低延迟的数据存储。为了保证其在Linux系统中作为服务持续运行并自动开机启动,你需要将Redis配置为守护进程并在系统启动时加载。 首先,确认Redis是否已经安装,并且配置文件redis.conf位于合适的目录下(默认是/etc/redis/)。然后按照以下步骤操作: 1. **编辑配置文件**: 使用文本编辑器打开`redis.conf`文件,查找`daemonize yes`这一行,设置它为`yes`,表示让Redis后台运行。同时,确保`pidfile`指定了一个合理的路径,以便管理器可以找到进程ID。 2. **添加到启动脚本**: 将Redis添加到系统的启动脚本中,如Systemd、Upstart或chkconfig等,具体取决于你的Linux发行版。创建或编辑相应的启动文件,例如在Systemd下,可以在`/etc/systemd/system/redis.service`添加启动命令: ```bash [Unit] Description=Redis in-memory data store After=network.target [Service] User=root WorkingDirectory=/path/to/your/redis ExecStart=/usr/bin/redis-server /path/to/redis.conf Restart=always KillMode=process Type=forking [Install] WantedBy=multi-user.target ``` 3. **启动并启用服务**: - 使用`sudo systemctl daemon-reload`更新服务列表。 - 启动Redis服务:`sudo systemctl start redis` - 确保它会在下次系统重启时自动启动:`sudo systemctl enable redis` 完成以上步骤后,Redis就会在系统启动时自动后台运行了。你可以通过`systemctl status redis`检查服务状态。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值