一、Redis简介

1.Redis是一个缓存数据库,主要是做缓存。什么是缓存?也就是缓冲存储,也可以理解为挡箭牌,对后面的一个保护
一般放在mysql/oracle等数据库前面,对数据库进行一个保护
用户请求数据时候,第一次先请求redis,redis里没有,就请求后台mysql数据库,返回数据时候会存放到redis里一份缓存起来,
当下一次请求同样数据时候,就可以直接请求redis拿到数据,防止了对数据库的频繁访问,减轻后台数据库的压力
2.Redis是一种内存型的nosql数据库,一般是用来缓存加速的,并且能够支持数据持久化存储的设置,可以将数据持久化到硬盘里面
3.Redis存储数据的方法是以key-value的形式
4.Redis数据类型支持字符串、列表、哈希等多种类型

二、Redis的源码编译安装

操作系统版本:Centos7
1.关闭防火墙

systemctl stop firewalld.service
systemctl disable firewalld

2.关闭selinux

sed -i 's#SELINUX=enforcing#SELINUX=disabled#g' /etc/selinux/config

3.配置yum源安装编译工具

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
yum install -y vim lrzsz gcc gcc-c++ make tar openssl openssl-devel cmake

4.编译安装redis

tar -zxf redis-6.2.1.tar.gz
cd redis-6.2.1
make -j2 && make install    #2核cpu,-j2指定2核cpu编译快
cp redis.conf /etc/

5.检查安装是否正常

ls /usr/local/bin/redis-*
redis-server -v
redis-server --version

说明:
如果系统都一样,没必要每台都编译安装,在一台上编译安装后,只需将编译好的二进制文件(/usr/local/bin/redis-*)拷贝到其他服务器即可

三、Redis6配置及启动

1.Redis配置简化

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

2.Redis配置更改

vim  /etc/redis.conf 
bind 0.0.0.0                                           #注意公网开放时候,尽量不要监听所有网卡ip,一般就配置自己业务网卡的ip
port 6379
dir /data/redis
requirepass redispwd
pidfile "redis.pid"                               #默认会在dir配置路径的当前路径下
logfile "redis.log"
daemonize yes                                  #以后台进程启动

3.启动Redis

mkdir -p /data/redis   #创建dir目录
redis-server /etc/redis.conf
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

4.查看Redis日志

tail -f /data/redis/redis.log 
17820:C 08 Nov 2022 12:17:25.366 # oO0OoO0OoO0Oo Redis is starting oO0OoO0OoO0Oo
17820:C 08 Nov 2022 12:17:25.366 # Redis version=6.2.1, bits=64, commit=00000000, modified=0, pid=17820, just started
17820:C 08 Nov 2022 12:17:25.366 # Configuration loaded
17820:M 08 Nov 2022 12:17:25.367 * Increased maximum number of open files to 10032 (it was originally set to 1024).
17820:M 08 Nov 2022 12:17:25.367 * monotonic clock: POSIX clock_gettime
17820:M 08 Nov 2022 12:17:25.368 * Running mode=standalone, port=6379.
17820:M 08 Nov 2022 12:17:25.368 # WARNING: The TCP backlog setting of 511 cannot be enforced because /proc/sys/net/core/somaxconn is set to the lower value of 128.
17820:M 08 Nov 2022 12:17:25.368 # Server initialized
17820:M 08 Nov 2022 12:17:25.368 # 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.
17820:M 08 Nov 2022 12:17:25.368 * Ready to accept connections

5.Redis启动的系统参数调整

# 调整最大文件打开数(文件句柄)
vim /etc/security/limits.conf
* - nofile 65535        #退出终端重新登录生效

#内核参数修改
vim /etc/sysctl.conf
net.core.somaxconn = 10240          #调大somaxconn参数
vm.overcommit_memory = 1           #这个如果没修改,可能会引起redis的数据丢失,参数很重要
sysctl -p

6.重启redis,查看日志,观察优化后的情况,报错告警都消失

pkill redis
redis-server /etc/redis.conf
tail -f /data/redis/redis.log

7.Systemctl管理Redis

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