redis-7 和 redis 5 版本在配置為systemd 方式管理時,配置關于有些許區別,否則會報systemctl status redis 如下錯誤:
redis.service: control process exited, code=exited status=1 Failed to
start Redis persistent key-value database. Unit redis.service entered
failed state.
這類報錯很多沒什么參考意義,可以嘗試通過啟動日志等排查具體問題,
journalctl -xe查看報錯如下:
– Unit redis.service has begun starting up.
May 22 18:11:54 worker1 systemd[1]: Failed to start Redis persistent key-value database.
– Subject: Unit redis.service has failed
– Defined-By: systemd
– Support: http://lists.freedesktop.org/mailman/listinfo/systemd-devel
–
– Unit redis.service has failed.
–
– The result is failed.
May 22 18:11:54 worker1 systemd[1]: Unit redis.service entered failed state.
May 22 18:11:54 worker1 systemd[1]: redis.service failed.
May 22 18:11:54 worker1 polkitd[795]: Unregistered Authentication Agent for unix-process:103583:240646911 (system bus name :1.17442, object path /org/freedesktop/PolicyKit1/A
正確的配置如下:
/usr/lib/systemd/system/redis.service
[Unit]
Description=Redis persistent key-value database
After=network.target
After=network-online.target
Wants=network-online.target[Service]
ExecStart=/usr/local/redis/bin/redis-server /usr/local/redis/conf/redis.conf
ExecStop=/usr/libexec/redis-shutdown
Restart=on-failure
Type=simple #注意這里為simple即可,舊版本為notify
User=root
Group=root
RuntimeDirectory=redis
RuntimeDirectoryMode=0755[Install]
WantedBy=multi-user.target
配置文件
關鍵信息如下
daemonize no #配置說明里面雖然說當supervised 為systemd的這里的設置不影響,但實測必須為no,為yes則自動報錯
supervised systemd #顯示指定管理方式為systemd
可將redis打包為rpm方便安裝,spec文件如下:
redis-7.4.2.spec
Name: redis
Version: 7.4.2
Release: 1%{?dist}
Summary: A persistent key-value databaseGroup: Applications/Databases
License: BSD
URL: http://redis.io
Source0: %{name}-%{version}.tar.gz
Source1: redis-7.conf
Source2: redis-7.service
Source3: redis-shutdown
BuildRoot: %{_tmppath}/%{name}-%{version}
BuildRequires: tcl >= 8.5
ExcludeArch: ppc64
#Requires:%description
Redis is an advanced key-value store. It is similar to memcached but the data
set is not volatile, and values can be strings, exactly like in memcached, but
also lists, sets, and ordered sets. All this data types can be manipulated with
atomic operations to push/pop elements, add/remove elements, perform server side
union, intersection, difference between sets, and so forth. Redis supports
different kind of sorting abilities.%prep
%setup -q%build
make%install
rm -fr %{buildroot}
make PREFIX=%{buildroot}/home/software/redis install
mkdir -p %{buildroot}/home/software/redis/log
install -p -D -m 644 %{SOURCE1} %{buildroot}/home/software/redis/conf/%{name}.conf
install -p -D -m 644 %{SOURCE2} %{buildroot}/usr/lib/systemd/system/redis.service
install -p -D -m 755 %{SOURCE3} %{buildroot}/usr/libexec/redis-shutdown%pre%post
if [ $1 -eq 1 ];then# 第一次安裝時啟用服務ln -s /home/software/redis /usr/local/redisln -s /usr/local/redis/bin/redis-cli /usr/local/bin/redis-cli/bin/systemctl daemon-reload/bin/systemctl enable %{name}.service >/dev/null 2>&1 || :
fi%preun
if [ $1 -eq 0 ]; then/bin/systemctl disable %{name}.service >/dev/null 2>&1 || :/bin/systemctl stop %{name}.service >/dev/null 2>&1 || :
fi%postunrm -f /usr/local/bin/redis-clirm -f /usr/local/redisrm -fr /home/software/redisrm -f /usr/libexec/redis-shutdownrm -f /usr/lib/systemd/system/redis.service%files
/home/software/redis
/usr/lib/systemd/system/redis.service
/usr/libexec/redis-shutdown%clean
rm -fr %{buildroot}%changelog
* Thu May 22 2025 Rockstics <rockstics@163.com> - 7.4.2-1
- Initial package creation for Redis 7.4.2