1. 創建服務
用service來管理服務的時候,是在/etc/init.d/目錄中創建一個腳本文件,來管理服務的啟動和停止,在systemctl中,也類似,文件目錄有所不同,在/lib/systemd/system目錄下創建一個腳本文件redis.service,裏面的內容如下:
[Unit]
Description=Redis
After=network.target
[Service]
ExecStart=/usr/local/bin/redis-server /usr/local/redis/redis.conf –daemonize no
ExecStop=/usr/local/bin/redis-cli -h -p 6379 shutdown
[Install]
WantedBy=multi-user.target
[Unit] 表示這是基礎信息
Description 是描述
After 是在那個服務後面啟動,一般是網絡服務啟動後啟動
[Service] 表示這裡是服務信息
ExecStart 是啟動服務的命令
ExecStop 是停止服務的指令
[Install] 表示這是是安裝相關信息
WantedBy 是以哪種方式啟動:multi-user.target表明當系統以多用戶方式(默認的運行級別)啟動時,這個服務需要被自動運行。
更詳細的service文件說明請訪問:這裡
2. 創建軟鏈接
創建軟鏈接是為了下一步系統初始化時自動啟動服務
ln -s /lib/systemd/system/redis.service /etc/systemd/system/multi-user.target.wants/redis.service
3. 刷新配置
剛剛配置的服務需要讓systemctl能識別,就必須刷新配置
#systemctl daemon-reload
如果沒有權限可以使用sudo
# systemctl daemon-reload
4. 啟動、重啟、停止
啟動redis
#systemctl start redis
重啟redis
#systemctl restart redis
停止redis
#systemctl stop redis
5. 開機自啟動
redis服務加入開機啟動
#systemctl enable redis
禁止開機啟動
#systemctl disable redis
6. 查看狀態
查看狀態
#systemctl status redis
同樣:增加redis-sentiel的啟停服務:
vi redis-sentinel.service
[Unit]
Description=Redis
After=network.target
[Service]
ExecStart=/usr/local/redis/bin/redis-sentinel /usr/local/redis/etc/sentinel.conf –sentinel
ExecStop=/usr/local/redis/bin/redis-cli -p 26379 shutdown
[Install]
WantedBy=multi-user.target
ln -s /lib/systemd/system/redis-sentinel.service /etc/systemd/system/multi-user.target.wants/redis-sentinel.service
systemctl daemon-reload
特別要注意:啟動redis 和redis-sentinel時,不要分開啟動,而是要同時啟,俺就是單獨與分開啟動,造成26379 sentinel端口總是無法正常打開,但是直接執行如下命令就又可以:
/usr/local/redis/bin/redis-sentinel /usr/local/redis/etc/sentinel.conf –sentinel 就可以打開26379
但如果你分別執行:
systemctl start redis
systemctl start redis-sentinel
就只能打開6379端口,26379就無法打開。
排查了很久,最後,才從網上一篇文章,獲取到靈感,一試之下,才知道要這樣啟動redis 和sentinel,6379 和26379端口才可以打開:
啟動redis redis-sentinel
systemctl start redis redis-sentinel.service #一起啟,不可以單獨起。
停止redis redis-sentinel
systemctl stop redis redis-sentinel.service #一起停,不可以單獨停。
[wpdm_package id=’1503′]
以下文章點擊率最高
Loading…