新聞中心
Redis是一款開源的內(nèi)存數(shù)據(jù)結(jié)構(gòu)存儲系統(tǒng),它可以用作數(shù)據(jù)庫、緩存和消息代理,在許多應(yīng)用場景中,Redis都發(fā)揮著重要的作用,有時候我們希望在系統(tǒng)啟動時自動運行Redis,以便及時處理一些初始化任務(wù)或者提供實時的數(shù)據(jù)服務(wù),本文將介紹如何設(shè)置Redis開機自啟動。

創(chuàng)新互聯(lián)公司是一家專業(yè)提供北流企業(yè)網(wǎng)站建設(shè),專注與做網(wǎng)站、成都網(wǎng)站制作、H5開發(fā)、小程序制作等業(yè)務(wù)。10年已為北流眾多企業(yè)、政府機構(gòu)等服務(wù)。創(chuàng)新互聯(lián)專業(yè)網(wǎng)站制作公司優(yōu)惠進行中。
一、檢查Redis是否已經(jīng)安裝
在設(shè)置Redis開機自啟動之前,我們需要確保Redis已經(jīng)正確安裝并配置好,可以通過以下命令檢查Redis是否已經(jīng)安裝:
redis-cli --version
如果顯示出Redis的版本信息,說明Redis已經(jīng)安裝成功,如果沒有安裝成功,請參考Redis官方文檔進行安裝:
二、創(chuàng)建Redis配置文件
為了讓Redis在開機時自動啟動,我們需要創(chuàng)建一個配置文件,配置文件通常位于`/etc/redis/redis.conf`,如果該文件不存在,可以手動創(chuàng)建一個,在配置文件中,我們需要設(shè)置以下幾個參數(shù):
1. `daemonize no`:這個參數(shù)表示讓Redis以非守護進程方式運行,這樣在后臺運行時不會影響到系統(tǒng)的正常運行。
2. `pidfile /var/run/redis_6379.pid`:這個參數(shù)表示設(shè)置Redis的進程ID文件路徑,當(dāng)Redis作為守護進程運行時,需要使用這個PID文件來管理進程。
3. `port 6379`:這個參數(shù)表示設(shè)置Redis監(jiān)聽的端口號,默認為6379,可以根據(jù)實際需求修改端口號。
4. `bind 127.0.0.1`:這個參數(shù)表示設(shè)置Redis只能監(jiān)聽本地回環(huán)地址,不能接受外部連接,如果需要讓其他設(shè)備訪問Redis,可以將此參數(shù)注釋掉或者修改為相應(yīng)的IP地址。
5. `loglevel notice`:這個參數(shù)表示設(shè)置Redis的日志級別為notice,這樣只會輸出警告和錯誤信息,方便排查問題。
三、創(chuàng)建開機自啟動腳本
為了讓Redis在開機時自動啟動,我們需要創(chuàng)建一個開機自啟動腳本,在`/etc/init.d`目錄下創(chuàng)建一個名為`redis_6379`的腳本文件:
sudo touch /etc/init.d/redis_6379 sudo chmod +x /etc/init.d/redis_6379
接下來,編輯`redis_6379`腳本文件,添加以下內(nèi)容:
#!/bin/sh
### BEGIN INIT INFO
# Provides: redis_6379
# Required-Start: $remote_fs $syslog
# Required-Stop: $remote_fs $syslog
# Default-Start: 2 3 4 5
# Default-Stop: 0 1 6
# Short-Description: Start Redis on boot
# Description: Enable service provided by redis.service.
### END INIT INFO
case "$1" in
start)
echo "Starting Redis ..."
/etc/init.d/redis-server start >> /var/log/redis_start.log 2>&1 & echo $! > /var/run/redis_6379.pid
;;
stop)
echo "Stopping Redis ..."
/etc/init.d/redis-server stop >> /var/log/redis_stop.log 2>&1 & kill $(cat /var/run/redis_6379.pid) && echo $? > /var/run/redundant_shutdown_status.txt || echo "failed to stop Redis"
rm -f /var/run/redis_6379.pid
;;
*)
echo "Usage: $0 start|stop" >&2
exit 1
;;
esac
exit 0
保存文件后,給腳本文件添加可執(zhí)行權(quán)限:
sudo chmod +x /etc/init.d/redis_6379
四、創(chuàng)建開機自啟動鏈接
為了讓系統(tǒng)在啟動時自動加載我們的開機自啟動腳本,我們需要創(chuàng)建一個鏈接,在`/etc/rc.local`文件中添加以下內(nèi)容:
#!/bin/sh -e
### BEGIN INIT INFO
# Provides: rc-local update script (required for systemctl)
# Required-Start: $remote_fs $syslog $network $named $time $syslogd $tmpfs $tcpdump $udpdump icmp $rtcwake $crond $syslogd $maildrop $messagebus *
# Required-Stop: $remote_fs $syslog $network $named $time $syslogd $tmpfs $tcpdump $udpdump icmp $rtcwake $crond $syslogd $maildrop $messagebus *
# Default-Start: 2 3 4 5
# Default-Stop: 0 1 6
# Short-Description: Run custom scripts at startup before normal process startup.
# Description: This script is executed at the end of each multiuser runlevel.
### END INIT INFO
if [ "$RUNLEVEL" == "0" ]; then # After this level, all processes not listed below will be stopped.${PATH=~^(?::/usr/localbin:)${PATH}}
[ "$TERM" != "dumb" ] && exec sudo su --login --command "${PATH=~^(?::/usr/localbin:)${PATH}} date;
$HOME/apps/redis_6379 start" &>> "$HOME"/apps/logs/redis_startup.log & disown # Add any command specific options above this line if needed.${PATH=~^(?::/usr/localbin:)${PATH}}
[ "$TERM" = "dumb" ] && exec sh <> "$HOME"/apps/logs/redis_startup.log & disown # This ensures that the container does not get locked (in case of CPU or memory constraints).${PATH=~^(?::/usr/localbin:)${PATH}}
fi # End script execution if it runs successfully (success means that the last command executed returned a zero exit code).${PATH=~^(?::/usr/localbin:)${PATH}}
exit 0 # Exit status of the script (0 indicates success).${PATH=~^(?::/usr/localbin:)${PATH}}
當(dāng)前文章:redis設(shè)置開機自啟動
地址分享:http://m.fisionsoft.com.cn/article/dhsjdsc.html


咨詢
建站咨詢
