如何编译部署Redis?

编译安装

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 准备系统环境

本章使用CentOS 8实现安装,相应的你可以参考此章节完成系统部署,

如何安装部署CentOS或RHEL 8?

2.1.2 关闭SELinux

cd ~
sed -i 's/SELINUX=enforcing/SELINUX=disabled/g' /etc/selinux/config
setenforce 0

2.1.3 准备编译环境

dnf -y install gcc gcc-c++ make

2.1.4 准备软件包

cd ~
wget https://download.redis.io/releases/redis-6.0.9.tar.gz
tar -xf redis-6.0.9.tar.gz

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

2.2 编译并安装软件包

2.2.1 编译软件包

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

2.2.2 安装软件包

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

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

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

2.2.3 确认安装

redis-server -v

可见如下提示,

Redis server v=6.0.9 sha=00000000:0 malloc=jemalloc-5.1.0 bits=64 build=8def9739b90f00b2

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-6.0.9/redis.conf /etc/redis/redis.conf
vim /etc/redis/redis.conf

修改配置如下,

supervised systemd
logfile /var/log/redis/redis.log
dir /var/lib/redis
requirepass redispasswd

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

sed -i "s/supervised no/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.2 测试服务运行

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

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

netstat -antp | grep redis

可见如下显示,

tcp        0      0 127.0.0.1:6379          0.0.0.0:*               LISTEN      109466/redis-server

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

pgrep -u redis redis-server -a

可见如下显示,

109466 /usr/bin/redis-server 127.0.0.1:6379

2.3.3 创建服务控制脚本

vim /usr/lib/systemd/system/redis.service

加入如下配置,

[Unit]
Description=Redis persistent key-value database
After=network.target

[Service]
ExecStart=/usr/bin/redis-server /etc/redis/redis.conf
ExecStop=/usr/libexec/redis-shutdown
Type=simple
User=redis
Group=redis
RuntimeDirectory=redis
RuntimeDirectoryMode=0755

[Install]
WantedBy=multi-user.target

根据配置我们需要创建如下脚本,

vim /usr/libexec/redis-shutdown

加入如下关闭redis的内容,

#!/bin/bash
#
# Wrapper to close properly redis and sentinel
test x"$REDIS_DEBUG" != x && set -x

REDIS_CLI=/usr/bin/redis-cli

# Retrieve service name
SERVICE_NAME="$1"
if [ -z "$SERVICE_NAME" ]; then
   SERVICE_NAME=redis
fi

# Get the proper config file based on service name
CONFIG_FILE="/etc/$SERVICE_NAME.conf"

# Use awk to retrieve host, port from config file
HOST=`awk '/^[[:blank:]]*bind/ { print $2 }' $CONFIG_FILE | tail -n1`
PORT=`awk '/^[[:blank:]]*port/ { print $2 }' $CONFIG_FILE | tail -n1`
PASS=`awk '/^[[:blank:]]*requirepass/ { print $2 }' $CONFIG_FILE | tail -n1`
SOCK=`awk '/^[[:blank:]]*unixsocket\s/ { print $2 }' $CONFIG_FILE | tail -n1`

# Just in case, use default host, port
HOST=${HOST:-127.0.0.1}
if [ "$SERVICE_NAME" = redis ]; then
    PORT=${PORT:-6379}
else
    PORT=${PORT:-26739}
fi

# Setup additional parameters
# e.g password-protected redis instances
[ -z "$PASS"  ] || ADDITIONAL_PARAMS="-a $PASS"

# shutdown the service properly
if [ -e "$SOCK" ] ; then
        $REDIS_CLI -s $SOCK $ADDITIONAL_PARAMS shutdown
else
        $REDIS_CLI -h $HOST -p $PORT $ADDITIONAL_PARAMS shutdown
fi

脚本创建后,需要赋予执行权限,

chmod +x /usr/libexec/redis-shutdown

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

systemctl daemon-reload

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

systemctl start redis.service
systemctl enable redis.service

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

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

如果发现如下日志,

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.

可使用如下命令处理,

sysctl vm.overcommit_memory=1
echo 'vm.overcommit_memory = 1' >> /etc/sysctl.d/99-sysctl.conf
sysctl -p

如果发现如下日志,

WARNING you have Transparent Huge Pages (THP) support enabled in your kernel. This will create latency and memory usage issues with Redis. To fix this issue run the command 'echo never > /sys/kernel/mm/transparent_hugepage/enabled' as root, and add it to your /etc/rc.local in order to retain the setting after a reboot. Redis must be restarted after THP is disabled.

可使用如下命令处理,

echo never > /sys/kernel/mm/transparent_hugepage/enabled
echo 'echo never > /sys/kernel/mm/transparent_hugepage/enabled' >> /etc/rc.d/rc.local
chmod +x /etc/rc.d/rc.local

如果发现如下日志,

WARNING: The TCP backlog setting of 511 cannot be enforced because /proc/sys/net/core/somaxconn is set to the lower value of 128.

可使用如下命令处理,

echo 511 > /proc/sys/net/core/somaxconn
echo 'echo 511 > /proc/sys/net/core/somaxconn' >> /etc/rc.d/rc.local
chmod +x /etc/rc.d/rc.local

2.3.4 测试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

没有评论

发表回复

Tomcat
如何编译安装Tomcat的Native库?

1 基础知识 Tomcat的Native库允许Tomcat使用OpenSSL作为JSSE的替代品来支 …

编译安装
如何基于Oracle Linux 9.x 编译BerkeleyDB?

1 基础知识 1.1 简介 Berkeley DB是一个开源的文件数据库,介于关系数据库与内存数据库 …

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

1 基础知识 1.1 Redis的简介 Redis是一种开放源代码(BSD许可)的运行于内存中的数据 …