Skip to content

CentOS8 编译安装Redis

编译安装

下载redis 6.2.1源码

https://redis.io/download

安装编译工具

shell
sudo yum install make gcc openssl-dev systemd-devel

编译redis

进入项目根目录,执行以下命令

shell
make USE_SYSTEMD=yes BUILD_TLS=yes MALLOC=libc

安装redis

PREFIX参数可根据自己的需要进行修改。

shell
make PREFIX=/opt/redis-6.2.1 install

添加redis服务

在代码根目录下,复制service脚本至系统systemd路径下。

shell
cp utils/systemd-redis_server.service /etc/systemd/user/

并修改service文件的ExecStart为真实命令,如

shell
[Unit]
Description=Redis data structure server
Documentation=https://redis.io/documentation
#Before=your_application.service another_example_application.service
#AssertPathExists=/var/lib/redis
Wants=network-online.target
After=network-online.target
 
[Service]
ExecStart=/opt/redis-6.2.1/bin/redis-server /opt/redis-6.2.1/redis.conf
## Alternatively, have redis-server load a configuration file:
#ExecStart=/usr/local/bin/redis-server /path/to/your/redis.conf
LimitNOFILE=10032
NoNewPrivileges=yes
#OOMScoreAdjust=-900
#PrivateTmp=yes
Type=notify
TimeoutStartSec=infinity
TimeoutStopSec=infinity
UMask=0077
#User=redis
#Group=redis
#WorkingDirectory=/var/lib/redis
 
[Install]
WantedBy=multi-user.target

修改配置文件以支持服务形式启动

修改redis.conf文件,目标参数为“daemonize”和“supervised”。如已在ExecStart中配置,可忽略此步。

properties
######################### GENERAL ############################
 
# By default Redis does not run as a daemon. Use 'yes' if you need it.
# Note that Redis will write a pid file in /var/run/redis.pid when daemonized.
# When Redis is supervised by upstart or systemd, this parameter has no impact.
daemonize yes
 
# If you run Redis from upstart or systemd, Redis can interact with your
# supervision tree. Options:
#   supervised no      - no supervision interaction
#   supervised upstart - signal upstart by putting Redis into SIGSTOP mode
#                        requires "expect stop" in your upstart job config
#   supervised systemd - signal systemd by writing READY=1 to $NOTIFY_SOCKET
#                        on startup, and updating Redis status on a regular
#                        basis.
#   supervised auto    - detect upstart or systemd method based on
#                        UPSTART_JOB or NOTIFY_SOCKET environment variables
# Note: these supervision methods only signal "process is ready."
#       They do not enable continuous pings back to your supervisor.
#
# The default is "no". To run under upstart/systemd, you can simply uncomment
# the line below:
#
supervised systemd

修改其他配置

properties
# 绑定网卡地址
bind * -::*

重新加载redis服务

shell
systemctl daemon-reload
systemctl enable systemd-redis_server.service
systemctl start systemd-redis_server.service

防火墙配置

shell
# 检查端口是否已放行
sudo firewall-cmd --zone=public --list-ports
# 添加6379端口放行
sudo firewall-cmd --zone=public --add-port=6379/tcp --permanent
# 重新加载防火墙
sudo firewall-cmd --reload

报错排查

抛出cc: ../deps/hiredis/libhiredis.a: No such file or directory 等错误

进入deps目录,执行如下命令

shell
make hiredis

如提示缺失其他.a文件,解决方法同上,make后拼接的名字与deps下的文件夹名字相同。

抛出cc: 错误:../deps/hiredis/libhiredis_ssl.a:没有那个文件或目录 错误

进入deps目录,追加参数单独编译hiredis模块即可解决。

shell
make hiredis USE_SSL=1

参考

hiredis_ssl.a文件缺失解决办法