Redis单机版安装与配置指南

发表时间: 2024-08-15 11:08

1、编译安装

安装前的环境准备:
Centos7
关闭防火墙
关闭selinux

能联网的情况下
# curl -o /etc/yum.repos.d/CentOS-Base.repo http://mirrors.aliyun.com/repo/Centos7.repo
# curl -o /etc/yum.repos.d/epel.repo http://mirrors.aliyun.com/repo/epel-7.repo

离线情况wget
wget http://download.redis.io/releases/redis-6.2.1.tar.gz

# yum install -y vim lrzsz gcc gcc-c++ make tar openssl openssl-devel cmake #安装编译工具
# cd /usr/local/src
# tar -zxf redis-6.2.1.tar.gz
# cd redis-6.2.1

# make -j2 && make install
# cp redis.conf /etc/


检查安装是否正常
ls /usr/local/bin/redis-*
redis-server -v
redis-server --version

2、配置

Redis配置简化
# cp /etc/redis.conf /etc/redis.conf.bak
# sed -i '/^#/d; /^$/d' /etc/redis.conf #把配置文件中#和空格删除掉

Redis配置更改
# vim /etc/redis.conf
bind 0.0.0.0 #注意公网开放时候,尽量不要监听所有网卡ip,一般就配置自己业务网卡的ip
port 6379 #监听的端口号
dir /data/redis #持久化数据存储位置
requirepass redispwd #redis密码
pidfile "redis.pid" #默认会在dir配置路径的当前路径下
logfile "redis.log" #指定日志输出位置
daemonize yes #以后台进程启动

启动redis:
# redis-server /etc/redis.conf #启动redis


# netstat -anput |grep 6379
tcp 0 0 0.0.0.0:6379 0.0.0.0:* LISTEN 17820/redis-server

# ps -ef |grep redis
root 17820 1 0 12:17 ? 00:00:00 /usr/local/bin/redis-server 0.0.0.0:6379

#ps –T –p pid
PID SPID TTY TIME CMD
31647 31647 ? 00:00:02 redis-server 主线程
31647 31648 ? 00:00:00 bio_close_file
31647 31649 ? 00:00:00 bio_aof_fsync
31647 31650 ? 00:00:00 bio_lazy_free
31647 31651 ? 00:00:00 jemalloc_bg_thd 内存池管理

查看redis的日志:
# tail -f /data/redis/redis.log 会看到日志里有如下报错信息

* Increased maximum number of open files to 10032 (it was originally set to 1024).

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

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

Redis启动的系统参数调整
调整最大文件打开数(文件句柄)
# vi /etc/security/limits.conf
* - nofile 65535 退出终端重新登录生效

内核参数修改
# vi /etc/sysctl.conf
net.core.somaxconn = 10240 #调大somaxconn参数

Linux 操作系统全局参数,每个 TCP 监听端口的队列长度
vm.overcommit_memory = 1 #这个如果没修改,可能会引起redis的数据丢失,参数很重要
表示内核在分配内存时候做检查的方式当设这个参数值为1时,内核允许超量使用内存直到用完为止,主要用于科学计算


然后运行sysctl -p 刷新生效

重启redis,查看日志,观察优化后的情况,报错告警都消失
# pkill redis
# redis-server /etc/redis.conf
# tail -f /data/redis/redis.log


Systemctl管理Redis:
# vi /usr/lib/systemd/system/redis.service
[Unit]
Description=redis
After=network.target
[Service]
Type=forking
ExecStart=/usr/local/bin/redis-server /etc/redis.conf
[Install]
WantedBy=multi-user.target

# mkdir /data/redis -p
# systemctl daemon-reload
# systemctl start redis
# ps -ef |grep redis