redis安装

最近在学习redis的安装和使用,用博客记录一下自己安装redis的过程,以下是环境准备

单机版安装

gcc环境安装

查看gcc版本号

1
gcc -v

安装命令

1
yum install -y gcc gcc-c++

升级命令

1
2
3
yum -y install centos-release-scl
yum -y install devtoolset-9-gcc devtoolset-9-gcc-c++ devtoolset-9-binutils
scl enable devtoolset-9 bash

注意: redis6需要5.3以上版本的gcc

解压上传好安装包

1
tar -zxvf redis-6.0.9.tar.gz

进入到解压目录

1
cd redis-6.0.9/

编译

1
make

编译成功后进入src目录下

1
cd srs/

安装redis

1
2
# PREFIX: 安装目录
make PREFIX=/usr/local/redis install

创建数据文件夹和日志文件

1
2
mkdir /usr/local/redis/data/
touch /usr/local/redis/redis.log

编辑配置文件

1
2
3
# 拷贝一份redis.conf
cp /home/wx/Downloads/redis-6.0.9/redis.conf /etc/redis.conf
vi /etc/redis.conf

conf配置

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
#指定 redis 只接收来自于该IP地址的请求,如果不进行设置,那么将处理所有请求
bind 0.0.0.0
#是否开启保护模式,默认开启。要是配置里没有指定bind和密码。开启该参数后,redis只会本地进行访问,拒绝外部访问。要是开启了密码和bind,可以开启。否则最好关闭,设置为no
protected-mode yes
#指定了记录日志的文件。空字符串的话,日志会打印到标准输出设备。后台运行的redis标准输出是/dev/null
logfile /usr/local/redis/redis.log
#数据目录,数据库的写入会在这个目录。rdb、aof文件也会写在这个目录
dir /usr/local/redis/data/

#RDB核心规则配置 save <指定时间间隔> <执行指定次数更新操作>,满足条件就将内存中的数据同步到硬盘中。官方出厂配置默认是 900秒内有1个更改,300秒内有10个更改以及60秒内有10000个更改,则将内存中的数据快照写入磁盘。若不想用RDB方案,可以把 save "" 的注释打开,下面三个注释
# save ""
save 900 1
save 300 10
save 60 10000
#设置密码
requirepass 123456

将redis做成系统服务

1
vi /etc/systemd/system/redis.service

复制以下内容

1
2
3
4
5
6
7
8
9
10
[Unit]
Description=redis Server
After=network.target

[Service]
ExecStart=/usr/local/redis/bin/redis-server /etc/redis.conf
ExecStop=/usr/local/redis/bin/redis-cli -h 127.0.0.1 -p 6379 -a 123456 shutdown

[Install]
WantedBy=mutli-user.target

redis的一些命令

1
2
3
4
5
6
systemctl daemon-reload
systemctl start redis
systemctl status redis
systemctl stop redis
systemctl enable redis
systemctl is-enabled redis

主从复制安装

服务器准备

服务器 端口 说明
192.168.72.128 6379 主节点
192.168.72.129 6379 从节点
192.168.72.130 6379 从节点

将三台服务器都安装上redis

修改两个从节点的配置

1
vi /etc/redis.conf

conf配置

1
2
3
4
# 主节点地址
slaveof 192.168.72.128 6379
# 主节点密码
masterauth 123456

依次启动主节点和两个从节点

哨兵模式安装

服务器准备

服务器 端口 说明
192.168.72.128 6379 主节点
192.168.72.129 6379 从节点
192.168.72.130 6379 从节点
192.168.72.128 26379 哨兵1
192.168.72.129 26379 哨兵2
192.168.72.130 26379 哨兵3

先在三台服务器上安装好主从架构

分别为每台服务器安装哨兵

进入到src目录下安装sentinel

1
make PREFIX=/usr/local/sentinel install

创建存放哨兵日志的文件夹和文件

1
2
mkdir /usr/local/sentinel/data/
touch /usr/local/sentinel/redis.log

编辑哨兵配置文件

1
2
cp /home/wx/Downloads/redis-6.0.9/redis.conf /etc/sentinel.conf
vi /etc/sentinel.conf

哨兵配置文件

1
2
3
4
5
6
7
8
9
10
11
12
bind 0.0.0.0
port 26379
protected-mode yes
logfile /usr/local/sentinel/redis.log
dir /usr/local/sentinel/data
requirepass 123456

# 配置主服务器
sentinel monitor master 192.168.72.128 6379 2
sentinel auth-pass master 123456
sentinel failover-timeout master 10000
sentinel down-after-milliseconds master 3000

创建哨兵的启动服务

1
vi /etc/systemd/system/sentinel.service

复制以下内容

1
2
3
4
5
6
7
8
9
10
[Unit]
Description=Sentinel Server
After=network.target

[Service]
ExecStart=/usr/local/sentinel/bin/redis-server /etc/sentinel.conf --sentinel
ExecStop=/usr/local/sentinel/bin/redis-cli -h 127.0.0.1 -p 6379 -a 123456 shutdown

[Install]
WantedBy=mutli-user.target

依次启动哨兵,并作成开机启动

1
2
3
4
systemctl daemon-reload
systemctl start sentinel
systemctl status sentinel
systemctl enable sentinel

集群模式安装

服务器准备

服务器 端口 说明
192.168.186.128 6379
192.168.186.131 6379
192.168.186.132 6379
192.168.186.133 6379
192.168.186.134 6379
192.168.186.135 6379

为每台服务器安装上redis(参考单机),区别是添加以下配置

1
2
3
4
5
6
#开启集群模式
cluster-enabled yes
# 该文件中包含集群信息
cluster-config-file /etc/nodes-6379.conf
# 主节点密码
masterauth 123456

将每个节点做成系统服务,依次启动

创建集群,进入到任意节点下,执行

1
2
3
cd /home/wx/Downloads/redis-6.0.9/src/
# --cluster-replicas 参数为数字,1表示每个主节点需要1个从节点
./redis-cli -a 123456 --cluster create 192.168.186.128:6379 192.168.186.131:6379 192.168.186.132:6379 192.168.186.133:6379 192.168.186.134:6379 192.168.186.135:6379 --cluster-replicas 1

集群命令

1
2
3
4
cluster info
cluster slots
cluster nodes

集群故障

  1. 如果集群任意主节点挂掉,且没有从节点.集群进入fail状态,也可以理解成集群的slot映射[0-16383]不完成时进入fail状态.
  2. 如果集群超过半数以上主节点挂掉,无论是否有从节点,集群都进入fail状态.
  3. 当集群不可用时,所有对集群的操作做都不可用,收到((error) CLUSTERDOWN The cluster is down)错误。

注意点

  • 生产环境为了安全性,务必设置redis的密码,将认证密码设置复杂点,避免被黑客的爆破字典试出来
  • bind生产环境不建议设置为0.0.0.0,一般只开放使用服务器的IP即可
  • 建议不要使用通用的端口6379,换成其他端口
  • 不用用root用户运行redis,为redis单独设置运行账号
  • 创建一个只读的redis用户给开发人员
    1
    acl setuser username on >password ~* +@connection +@read +info
文章目录
  1. 1. 单机版安装
    1. 1.1. gcc环境安装
      1. 1.1.1. 查看gcc版本号
      2. 1.1.2. 安装命令
      3. 1.1.3. 升级命令
    2. 1.2. 解压上传好安装包
    3. 1.3. 进入到解压目录
    4. 1.4. 编译
    5. 1.5. 编译成功后进入src目录下
    6. 1.6. 安装redis
    7. 1.7. 创建数据文件夹和日志文件
    8. 1.8. 编辑配置文件
    9. 1.9. 将redis做成系统服务
    10. 1.10. redis的一些命令
  2. 2. 主从复制安装
    1. 2.1. 服务器准备
    2. 2.2. 将三台服务器都安装上redis
    3. 2.3. 修改两个从节点的配置
    4. 2.4. 依次启动主节点和两个从节点
  3. 3. 哨兵模式安装
    1. 3.1. 服务器准备
    2. 3.2. 先在三台服务器上安装好主从架构
    3. 3.3. 分别为每台服务器安装哨兵
      1. 3.3.1. 进入到src目录下安装sentinel
      2. 3.3.2. 创建存放哨兵日志的文件夹和文件
      3. 3.3.3. 编辑哨兵配置文件
      4. 3.3.4. 创建哨兵的启动服务
      5. 3.3.5. 依次启动哨兵,并作成开机启动
  4. 4. 集群模式安装
    1. 4.1. 服务器准备
    2. 4.2. 为每台服务器安装上redis(参考单机),区别是添加以下配置
    3. 4.3. 将每个节点做成系统服务,依次启动
    4. 4.4. 创建集群,进入到任意节点下,执行
    5. 4.5. 集群命令
    6. 4.6. 集群故障
  5. 5. 注意点
|