Centos7编译安装redis并通过systemd管理服务

Centos7编译安装redis并通过systemd管理服务

1.准备基础环境

1.1.安装编译环境

[root@db-52 ~]# yum install -y  wget make gcc-c++
···
完毕!

1.2.下载源码并解压

[root@db-52 ~]#  cd /opt
[root@db-52 opt]# wget https://download.redis.io/releases/redis-6.2.4.tar.gz
--2025-02-26 14:58:12--  https://download.redis.io/releases/redis-6.2.4.tar.gz
正在解析主机 download.redis.io (download.redis.io)... 104.18.27.34, 104.18.26.34, 2606:4700::6812:1b22, ...
正在连接 download.redis.io (download.redis.io)|104.18.27.34|:443... 已连接。
已发出 HTTP 请求,正在等待回应... 200 OK
长度:2457940 (2.3M) [application/octet-stream]
正在保存至: “redis-6.2.4.tar.gz”

100%[================================================================>] 2,457,940   2.33MB/s 用时 1.0s   

2025-02-26 14:58:14 (2.33 MB/s) - 已保存 “redis-6.2.4.tar.gz” [2457940/2457940])

[root@db-52 opt]# ll
总用量 2404
-rw-r--r-- 1 root root 2457940 61 2021 redis-6.2.4.tar.gz
[root@db-52 opt]#
[root@db-52 opt]# tar -xf redis-6.2.4.tar.gz 
[root@db-52 opt]# ll
总用量 2408
drwxrwxr-x 7 root root    4096 61 2021 redis-6.2.4
-rw-r--r-- 1 root root 2457940 61 2021 redis-6.2.4.tar.gz
[root@db-52 opt]#

2.安装和配置redis

2.1.编译安装redis

[root@db-52 opt]# cd /opt/redis-6.2.4/
[root@db-52 redis-6.2.4]# make && make install PREFIX=/usr/local/redis
···

2.2.配置文件参数

将配置文件放到/etc/redis/目录

[root@db-52 redis-6.2.4]# mkdir /etc/redis
[root@db-52 redis-6.2.4]# cp redis.conf /etc/redis/redis.conf
[root@db-52 redis-6.2.4]# sed -i 's#^logfile ""#logfile "/var/log/redis.log"#' /etc/redis/redis.conf 
[root@db-52 redis-6.2.4]# sed -i "s/bind 127.0.0.1/bind 0.0.0.0/g" /etc/redis/redis.conf 
[root@db-52 redis-6.2.4]# sed -i "s/daemonize no/daemonize yes/g" /etc/redis/redis.conf 
[root@db-52 redis-6.2.4]# sed -i "561i maxmemory-policy allkeys-lru" /etc/redis/redis.conf 
[root@db-52 redis-6.2.4]# sed -i "481i requirepass mima666" /etc/redis/redis.conf 

参数解释:

 sed -i 's#^logfile ""#logfile "/var/log/redis.log"#' /etc/redis/redis.conf   
#logfile 参数用于指定 Redis 的日志文件路径。原配置中 logfile "" 表示不记录日志到文件,修改后将日志记录到 /var/log/redis.log 文件中。

sed -i "s/bind 127.0.0.1/bind 0.0.0.0/g" /etc/redis/redis.conf 
bind 参数用于指定 Redis 服务器监听的 IP 地址。原配置 bind 127.0.0.1 表示 Redis 只监听本地回环地址,只能在本地访问;修改为 bind 0.0.0.0 后,Redis 会监听所有可用的网络接口,允许来自任何 IP 地址的客户端连接。


 sed -i "s/daemonize no/daemonize yes/g" /etc/redis/redis.conf
daemonize 参数用于控制 Redis 是否以守护进程模式运行。daemonize no 表示 Redis 以非守护进程模式运行,会在前台运行并输出日志到控制台;daemonize yes 表示 Redis 以守护进程模式运行,会在后台运行,不占用终端。
 
 sed -i "561i maxmemory-policy allkeys-lru" /etc/redis/redis.conf 
maxmemory-policy 参数用于指定 Redis 在达到最大内存限制时的内存淘汰策略。allkeys-lru 表示当 Redis 内存达到上限时,从所有键中选择最近最少使用(LRU)的键进行淘汰。

sed -i "481i requirepass mima666" /etc/redis/redis.conf 
requirepass 参数用于设置 Redis 的访问密码。设置后,客户端连接 Redis 时需要使用 AUTH 命令提供正确的密码才能进行操作,这里的密码是 mima666。

3.配置systemd启动文件

[root@db-52 redis-6.2.4]# grep pidfile /etc/redis/redis.conf 
pidfile /var/run/redis_6379.pid
[root@db-52 redis-6.2.4]# vim /etc/systemd/system/redis.service
[root@db-52 redis-6.2.4]# cat /etc/systemd/system/redis.service 
[Unit]
Description=Redis persistent key-value database
After=network.target
After=network-online.target
Wants=network-online.target

[Service]
Type=forking
PIDFile=/var/run/redis_6379.pid
ExecStart=/usr/local/redis/bin/redis-server /etc/redis/redis.conf
ExecReload=/bin/kill -s HUP $MAINPID
ExecStop=/bin/kill -s QUIT $MAINPID

[Install]
WantedBy=multi-user.target
[root@db-52 redis-6.2.4]# 

参数解释

以下是对 /etc/systemd/system/redis.service 文件中各个参数的详细解释:

[Unit] 部分

该部分主要用于定义服务的基本信息、依赖关系和启动顺序。

  • Description=Redis persistent key-value database

    • 作用:为该服务提供一个简短的描述信息,方便用户识别该服务的用途。当使用 systemctl status redis.service 查看服务状态时,会显示这个描述。
  • After=network.target

    • 作用:指定该服务在 network.target 启动之后启动。network.target 表示系统网络基本配置完成的一个状态点,确保 Redis 服务在网络可用后再启动,避免因网络未就绪而导致启动失败。
  • After=network-online.target

    • 作用:同样是指定启动顺序,要求 Redis 服务在 network-online.target 启动之后启动。network-online.target 意味着网络不仅配置完成,而且已经可以正常使用,相比 network.target 更严格,进一步保证 Redis 启动时网络是可用的。
  • Wants=network-online.target

    • 作用:表明该服务与 network-online.target 存在弱依赖关系。当启动 Redis 服务时,systemd 会尝试启动 network-online.target,但如果 network-online.target 启动失败,不会影响 Redis 服务的启动。

[Service] 部分

该部分定义了服务的具体行为,如启动方式、启动命令、停止命令等。

  • Type=forking

    • 作用:指定服务的启动类型为 forking。这种类型表示服务在启动时会进行 fork 操作,父进程退出,子进程继续在后台运行。Redis 以守护进程模式运行时就是这种情况,所以设置为 forking
  • PIDFile=/var/run/redis_6379.pid

    • 作用:指定 Redis 进程的 PID(进程 ID)文件路径。systemd 通过该文件来跟踪 Redis 进程的状态,确保在需要停止或重启服务时能够准确找到对应的进程。
  • ExecStart=/usr/local/redis/bin/redis-server /etc/redis/redis.conf

    • 作用:定义服务启动时要执行的命令。这里指定使用 /usr/local/redis/bin/redis-server 可执行文件,并加载 /etc/redis/redis.conf 配置文件来启动 Redis 服务。
  • ExecReload=/bin/kill -s HUP $MAINPID

    • 作用:定义服务重新加载配置时要执行的命令。/bin/kill -s HUP 表示向指定进程发送 HUP(Hangup)信号,$MAINPIDsystemd 自动替换为服务主进程的 PID。发送 HUP 信号通常可以让 Redis 重新加载配置文件而不中断服务。
  • ExecStop=/bin/kill -s QUIT $MAINPID

    • 作用:定义服务停止时要执行的命令。/bin/kill -s QUIT 表示向指定进程发送 QUIT 信号,$MAINPID 是服务主进程的 PID。发送 QUIT 信号可以让 Redis 安全地关闭服务,保存数据并清理资源。

[Install] 部分

该部分指定服务安装的相关信息,主要用于设置服务的开机自启。

  • WantedBy=multi-user.target
    • 作用:表示当系统进入多用户模式(multi-user.target)时,该服务应该被启动。当使用 systemctl enable redis.service 命令设置服务开机自启时,systemd 会在 multi-user.target.wants 目录下创建一个指向 redis.service 的符号链接,从而实现开机自动启动。

4.测试启动

测试启动

[root@db-52 redis-6.2.4]# systemctl start redis
Job for redis.service failed. See "systemctl status redis.service" and "journalctl -xe" for details.

启动失败,查看详情

[root@db-52 redis-6.2.4]# systemctl status redis -l
● redis.service - Redis persistent key-value database
   Loaded: loaded (/etc/systemd/system/redis.service; disabled; vendor preset: disabled)
   Active: failed (Result: protocol) since 三 2025-02-26 16:08:42 CST; 1min 47s ago
  Process: 9665 ExecStart=/usr/local/redis/bin/redis-server /etc/redis/redis.conf (code=exited, status=0/SUCCESS)

226 16:08:42 db-52 systemd[1]: Starting Redis persistent key-value database...
226 16:08:42 db-52 systemd[1]: New main PID 9666 does not exist or is a zombie.
226 16:08:42 db-52 systemd[1]: Failed to start Redis persistent key-value database.
226 16:08:42 db-52 systemd[1]: Unit redis.service entered failed state.
226 16:08:42 db-52 systemd[1]: redis.service failed.

查看日志

[root@db-52 redis-6.2.4]# cat /var/log/redis.log 
9666:C 26 Feb 2025 16:08:42.731 # oO0OoO0OoO0Oo Redis is starting oO0OoO0OoO0Oo
9666:C 26 Feb 2025 16:08:42.731 # Redis version=6.2.4, bits=64, commit=00000000, modified=0, pid=9666, just started
9666:C 26 Feb 2025 16:08:42.731 # Configuration loaded
9666:M 26 Feb 2025 16:08:42.732 * Increased maximum number of open files to 10032 (it was originally set to 1024).
9666:M 26 Feb 2025 16:08:42.732 * monotonic clock: POSIX clock_gettime
9666:M 26 Feb 2025 16:08:42.732 * Running mode=standalone, port=6379.
9666:M 26 Feb 2025 16:08:42.732 # WARNING: The TCP backlog setting of 511 cannot be enforced because /proc/sys/net/core/somaxconn is set to the lower value of 128.
9666:M 26 Feb 2025 16:08:42.732 # Server initialized
9666:M 26 Feb 2025 16:08:42.732 # WARNING overcommit_memory is set to 0! Background save may fail under low memory condition. To fix this issue add 'vm.overcommit_memory = 1' to /etc/sysctl.conf and then reboot or run the command 'sysctl vm.overcommit_memory=1' for this to take effect.
9666:M 26 Feb 2025 16:08:42.733 # WARNING Your kernel has a bug that could lead to data corruption during background save. Please upgrade to the latest stable kernel.
9666:M 26 Feb 2025 16:08:42.733 # Redis will now exit to prevent data corruption. Note that it is possible to suppress this warning by setting the following config: ignore-warnings ARM64-COW-BUG

因为我的系统是ARM架构的centos7,日志大概意思是我这个版本的kernel内核存在bug,让我更新到最新版本。也可以忽略这个bug,在redis配置文件中最后一行取消ignore-warnings ARM64-COW-BUG这个注释。

这里我选择升级我的kernel

yum update kernel -y

再次测试

[root@db-52 redis-6.2.4]# systemctl start redis
[root@db-52 redis-6.2.4]# systemctl status redis -l
 redis.service - Redis persistent key-value database
   Loaded: loaded (/etc/systemd/system/redis.service; disabled; vendor preset: disabled)
   Active: failed (Result: exit-code) since  2025-02-26 16:28:27 CST; 2s ago
  Process: 19395 ExecStart=/usr/local/redis/bin/redis-server /etc/redis/redis.conf (code=exited, status=0/SUCCESS)
 Main PID: 19396 (code=exited, status=1/FAILURE)

2 26 16:28:26 db-52 systemd[1]: Starting Redis persistent key-value database...
2 26 16:28:26 db-52 systemd[1]: Can't open PID file /var/run/redis_6379.pid (yet?) after start: No such file or directory
2 26 16:28:27 db-52 systemd[1]: Started Redis persistent key-value database.
2 26 16:28:27 db-52 systemd[1]: redis.service: main process exited, code=exited, status=1/FAILURE
2 26 16:28:27 db-52 systemd[1]: Unit redis.service entered failed state.
2 26 16:28:27 db-52 systemd[1]: redis.service failed.
[root@db-52 redis-6.2.4]# 

还是没有成功,提示没有redis_6379.pid这个文件;
这个情况先要手动运行一下redis
然后就有了

[root@db-52 redis-6.2.4]# /usr/local/redis/bin/redis-server /etc/redis/redis.conf    
[root@db-52 redis-6.2.4]# ll /var/run/redis_6379.pid 
-rw-r--r-- 1 root root 6 226 16:34 /var/run/redis_6379.pid

再次启动,还是不行
继续查看日志

[root@db-52 redis-6.2.4]# tail /var/log/redis.log 
19587:C 26 Feb 2025 16:39:29.662 # Redis version=6.2.4, bits=64, commit=00000000, modified=0, pid=19587, just started
19587:C 26 Feb 2025 16:39:29.662 # Configuration loaded
19587:M 26 Feb 2025 16:39:29.663 * Increased maximum number of open files to 10032 (it was originally set to 1024).
19587:M 26 Feb 2025 16:39:29.663 * monotonic clock: POSIX clock_gettime
19587:M 26 Feb 2025 16:39:29.663 * Running mode=standalone, port=6379.
19587:M 26 Feb 2025 16:39:29.663 # WARNING: The TCP backlog setting of 511 cannot be enforced because /proc/sys/net/core/somaxconn is set to the lower value of 128.
19587:M 26 Feb 2025 16:39:29.663 # Server initialized
19587:M 26 Feb 2025 16:39:29.663 # WARNING overcommit_memory is set to 0! Background save may fail under low memory condition. To fix this issue add 'vm.overcommit_memory = 1' to /etc/sysctl.conf and then reboot or run the command 'sysctl vm.overcommit_memory=1' for this to take effect.
19587:M 26 Feb 2025 16:39:29.664 # WARNING Your kernel has a bug that could lead to data corruption during background save. Please upgrade to the latest stable kernel.
19587:M 26 Feb 2025 16:39:29.664 # Redis will now exit to prevent data corruption. Note that it is possible to suppress this warning by setting the following config: ignore-warnings ARM64-COW-BUG

发现还是提示我kernel有bug,我刚刚不是升级过了吗,不会还要重启机器吧
reboot
···

[root@db-52 ~]# systemctl start redis
[root@db-52 ~]# systemctl status redis -l
 redis.service - Redis persistent key-value database
   Loaded: loaded (/etc/systemd/system/redis.service; disabled; vendor preset: disabled)
   Active: active (running) since  2025-02-26 17:11:47 CST; 2s ago
  Process: 1642 ExecStart=/usr/local/redis/bin/redis-server /etc/redis/redis.conf (code=exited, status=0/SUCCESS)
 Main PID: 1643 (redis-server)
   CGroup: /system.slice/redis.service
           └─1643 /usr/local/redis/bin/redis-server 0.0.0.0:6379         

2 26 17:11:47 db-52 systemd[1]: Starting Redis persistent key-value database...
2 26 17:11:47 db-52 systemd[1]: Can't open PID file /var/run/redis_6379.pid (yet?) after start: No such file or directory
2 26 17:11:47 db-52 systemd[1]: Started Redis persistent key-value database.
[root@db-52 ~]# 

终于重启过后就正常了。==!

5.设置PATH全局生效,测试命令执行。

/etc/profile中末行添加export PATH=$PATH:/usr/local/redis/bin

[root@db-52 ~]# source /etc/profile
[root@db-52 ~]# redis-cli -v
redis-cli 6.2.4
[root@db-52 ~]# redis-cli auth mima666
OK

ok,全部完成。

posted @   先ping  阅读(36)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· Manus的开源复刻OpenManus初探
· 写一个简单的SQL生成工具
· AI 智能体引爆开源社区「GitHub 热点速览」
· C#/.NET/.NET Core技术前沿周刊 | 第 29 期(2025年3.1-3.9)
点击右上角即可分享
微信分享提示