使用Systemd启动Redis

时间:2021-05-18
本文章向大家介绍使用Systemd启动Redis,主要包括使用Systemd启动Redis使用实例、应用技巧、基本知识点总结和需要注意事项,具有一定的参考价值,需要的朋友可以参考一下。

Centos7使用了systemd来启动守护进程,用来取代传统的init启动方式的

通过编译安装的Redis需要设置启动脚本来实现systemd启动,从而可以开机自启

编译

编译的时候,需要加入对systemd的支持

# libsystemd-dev on Debian/Ubuntu or systemd-devel on CentOS
yum install systemd-devel 
make USE_SYSTEMD=yes  PREFIX=/usr/local/redis/ install

否则使用systemd启动后,Redis日志中会有如下的报错:

systemd supervision requested or auto-detected, but Redis is compiled without libsystemd support!

参考:redis的systemd启动

配置

参考:Redis安装配置

创建需要的文件夹:

cd /usr/local/redis/
mkdir run
mkdir config
mkdir log
mkdir data
mkdir scripts

配置文件范例:

bind * -::*
protected-mode no
port 8000
daemonize no
pidfile "/usr/local/redis/run/redis_8000.pid"
loglevel notice
logfile "/usr/local/redis/log/redis8000.log"
save 3600 1
save 300 100
save 60 10000
stop-writes-on-bgsave-error no
dbfilename "dump8000.rdb"
dir "/usr/local/redis-6.2.3/data"
requirepass "monkey"
maxclients 1000
maxmemory 24gb
appendonly yes
appendfilename "appendonly8000.aof"
appendfsync everysec
no-appendfsync-on-rewrite no
auto-aof-rewrite-percentage 100
auto-aof-rewrite-min-size 1gb
aof-load-truncated yes
aof-use-rdb-preamble yes
supervised systemd

Systemd脚本

cd /usr/lib/systemd/system
vi redis-server.service
-------------------------------------------------
[Unit]
Description=Redis Server
After=network.target

[Service]
User=redis
Group=redis
Type=notify
LimitNOFILE=65535
ExecStart=/usr/local/redis/bin/redis-server /usr/local/redis/config/redis8000.conf --daemonize no --supervised systemd
ExecStop=/bin/kill -SIGTERM $MAINPID

[Install]
WantedBy=multi-user.target
-------------------------------------------------

vi redis-sentinel.service
-------------------------------------------------
[Unit]
Description=Redis Sentinel
After=network.target

[Service]
User=redis
Group=redis
Type=notify
LimitNOFILE=65535
ExecStart=/usr/local/redis/bin/redis-sentinel /usr/local/redis/config/sentinel6800.conf --daemonize no --supervised systemd
ExecStop=/bin/kill -SIGTERM $MAINPID

[Install]
WantedBy=multi-user.target
-------------------------------------------------
systemctl start redis-server
systemctl start redis-sentinel
systemctl enable redis-server
systemctl enable redis-sentinel

原文地址:https://www.cnblogs.com/monkey6/p/14781775.html