如何在Oracle Linux 9.x编译部署Redis 8.4.x?

编译安装

1 基础知识

1.1 Redis的简介

Redis是一种开放源代码(BSD许可)的运行于内存中的数据结构存储

1.2 Redis的用途

– Redis可作为数据库
– Redis可作为缓存
– Redis可作为消息代理

1.3 Redis支持的数据结构

– 字符串结构
– 哈希结构
– 列表结构
– 集合结构
– 带范围查询的排序集合结构
– 位图结构
– 超日志结构
– 带半径查询和流的地理空间索引结构

1.4 Redis的功能

– Redis支持内置复制功能
– Redis支持内置Lua脚本功能
– Redis支持内置LRU驱逐功能
– Redis支持内置事务功能
– Redis支持不同级别的磁盘持久性功能

1.5 Redis-cli

1.5.1 Redis-cli的简介

– redis-cli是Redis命令行界面
– redis-cli可直接从终端向Redis发送命令并读取服务器的回复

1.5.2 Redis-cli的主要模式

– 交互模式,键入命令且能获取回复
– 标准输出模式,将命令行作为参数发送
– 特殊模式,接收主服务器的流并模拟从服务器输出该流

2 最佳实践

2.1 部署前的准备

2.1.1 系统基本配置

OS = Oracle Linux 9.x x86_64
HostName = any
IPAddress = any
请参阅如下章节完成系统的基本配置,

如何完成CentOS 7.x的基本服务?

2.1.2 准备编译环境

dnf -y install gcc gcc-c++ make

2.1.3 准备软件包

cd ~
wget https://github.com/redis/redis/archive/8.4.0.tar.gz -O redis-8.4.0.tar.gz
tar -xf redis-8.4.0.tar.gz

另外,其他版本请从以下链接下载,
https://redis.io/download

2.2 编译并安装软件包

2.2.1 编译软件包

cd ~/redis-8.4.0/
make PREFIX=/usr

2.2.2 安装软件包

cd ~/redis-8.4.0/
make PREFIX=/usr install

另外,如果需要卸载,请使用如下命令,

cd ~/redis-8.4.0/
make PREFIX=/usr uninstall

2.2.3 确认安装

redis-server -v

可见如下提示,

Redis server v=8.4.0 sha=00000000:0 malloc=jemalloc-5.3.0 bits=64 build=81fb14348048237

2.3 配置redis

2.3.1 创建运行用户

groupadd redis
useradd -g redis -d /var/lib/redis -s /sbin/nologin -c "Redis Database Server" redis

2.3.2 修改配置文件

mkdir -p /etc/redis/
cp ~/redis-8.4.0/redis.conf /etc/redis/redis.conf
cp /etc/redis/redis.conf /etc/redis/redis.conf.default
vim /etc/redis/redis.conf

修改配置如下,

bind * -::*
supervised systemd
logfile "/var/log/redis/redis.log"
dir /var/lib/redis
requirepass redispasswd

另外,也可以使用如下快速命令执行,

sed -i "s/^bind 127.0.0.1 -::1/bind * -::*/g" /etc/redis/redis.conf
sed -i "s/# supervised auto/supervised systemd/g" /etc/redis/redis.conf
sed -i 's/logfile ""/logfile "\/var\/log\/redis\/redis.log"/g' /etc/redis/redis.conf
sed -i "s/dir .\//dir \/var\/lib\/redis/g" /etc/redis/redis.conf
sed -i "s/# requirepass foobared/requirepass redispasswd/g" /etc/redis/redis.conf

然后,我们需要根据配置增加响应的文件夹,

mkdir -p /var/log/redis
touch /var/log/redis/redis.log
chown redis:redis -R /var/log/redis

考虑到日志切割问题,我们还建议做如下配置,

vim /etc/logrotate.d/redis

需要加入如下配置,

/var/log/redis/*.log {
    weekly
    rotate 10
    copytruncate
    delaycompress
    compress
    notifempty
    missingok
}

2.3.3 测试服务运行

runuser -u redis /usr/bin/redis-server /etc/redis/redis.conf

执行命令后,可开启另外一个终端使用如下命令确认Redis是否运行,

ss -antp | grep redis

可见如下显示,

LISTEN 0      511          0.0.0.0:6379        0.0.0.0:*     users:(("redis-server",pid=133750,fd=8))                   
LISTEN 0      511             [::]:6379           [::]:*     users:(("redis-server",pid=133750,fd=9))   

根据端口的情况,你可能还需要如下配置,

firewall-cmd --permanent --add-service redis
firewall-cmd --reload
firewall-cmd --list-all

另外,也可以使用如下命令查看服务的进程,

pgrep -u redis redis-server -a

可见如下显示,

133750 /usr/bin/redis-server *:6379 

2.3.4 创建服务控制脚本

cd ~/redis-8.4.0/
cp utils/systemd-redis_server.service /usr/lib/systemd/system/redis.service
vim /usr/lib/systemd/system/redis.service

加入如下配置,

# example systemd service unit file for redis-server
#
# In order to use this as a template for providing a redis service in your
# environment, _at the very least_ make sure to adapt the redis configuration
# file you intend to use as needed (make sure to set "supervised systemd"), and
# to set sane TimeoutStartSec and TimeoutStopSec property values in the unit's
# "[Service]" section to fit your needs.
#
# Some properties, such as User= and Group=, are highly desirable for virtually
# all deployments of redis, but cannot be provided in a manner that fits all
# expectable environments. Some of these properties have been commented out in
# this example service unit file, but you are highly encouraged to set them to
# fit your needs.
#
# Please refer to systemd.unit(5), systemd.service(5), and systemd.exec(5) for
# more information.

[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=/usr/bin/redis-server /etc/redis/redis.conf --supervised systemd --daemonize no
LimitNOFILE=10032
NoNewPrivileges=yes
#OOMScoreAdjust=-900
#PrivateTmp=yes
Type=simple
TimeoutStartSec=infinity
TimeoutStopSec=infinity
UMask=0077
User=redis
Group=redis
WorkingDirectory=/var/lib/redis

[Install]
WantedBy=multi-user.target

编辑完成后,你需要使用如下命令重载使脚本生效,

systemctl daemon-reload

配置完成后,可使用如下命令启动服务并设置自启动,

systemctl start redis.service
systemctl enable redis.service

如果启动异常,可使用如下命令检查日志,

tail -f /var/log/messages
tail -f /var/log/redis/redis.log

2.3.5 测试redis服务

redis-cli
127.0.0.1:6379> auth redispasswd
OK
127.0.0.1:6379> set foo bar
OK
127.0.0.1:6379> get foo
"bar"
127.0.0.1:6379> quit

参阅文档
===============

Redis官方文档
————-
https://redis.io/

Redis支持的指令
————-
https://redis.io/commands

Redis支持的客户端
————-
https://redis.io/clients

Redis的文档
————-
https://redis.io/documentation

Rediscli的使用
————-
https://redis.io/topics/rediscli

nextCloud and Redis
https://docs.nextcloud.com/server/20/admin_manual/configuration_server/caching_configuration.html

没有评论

发表回复

Python
如何基于CentOS 7.x编译安装Python 3.x?

1 前言 一个问题,一篇文章,一出故事。 今天遇到CentOS 7.x Python版本过旧需要安装 …

编译安装
如何基于CentOS 7.x编译安装OpenSSL?

1 前言 一个问题,一篇文章,一出故事。 今天遇到CentOS 7.x需要安装新版本的OpenSSL …

编译安装
如何CentOS 7.x编译SQLite 3.51.1?

1 前言 一个问题,一篇文章,一出故事。 今天遇到编译版本的Python SQLite版本过低问题, …