Redis主从配置、数据持久化、集群

发布订阅

## subscribe

订阅一个或者多个频道

## publish

给指定的频道发送消息

## psubscribe

订阅指定模式的频道,*代表所有

## pubsub channels

查看当前活跃的频道

## pubsub numsub channels

查看当前频道的订阅人数

## redis的配置文件
```SHELL
bind 127.0.0.1 # 监听的地址
protected-mode yes # 将redis运行在安全模式下
port 6379 # 端口
tcp-backlog 511
timeout 0
tcp-keepalive 300
daemonize no # 是否以守护进程开启
supervised no
pidfile /var/run/redis_6379.pid
loglevel notice
logfile ""
databases 16
always-show-logo yes
save 900 1 # 在900秒以内有1次更新,就会持久化
save 300 10 
save 60 10000
stop-writes-on-bgsave-error yes
rdbcompression yes
rdbchecksum yes
dbfilename dump.rdb # 数据的保存文件
dir ./
replica-serve-stale-data yes
replica-read-only yes
repl-diskless-sync no
repl-diskless-sync-delay 5
repl-disable-tcp-nodelay no
replica-priority 100
lazyfree-lazy-eviction no
lazyfree-lazy-expire no
lazyfree-lazy-server-del no
replica-lazy-flush no
appendonly no
appendfilename "appendonly.aof"
appendfsync everysec
no-appendfsync-on-rewrite no
auto-aof-rewrite-percentage 100
auto-aof-rewrite-min-size 64mb
aof-load-truncated yes
aof-use-rdb-preamble yes
lua-time-limit 5000
slowlog-log-slower-than 10000
slowlog-max-len 128
latency-monitor-threshold 0
notify-keyspace-events ""
hash-max-ziplist-entries 512
hash-max-ziplist-value 64
list-max-ziplist-size -2
list-compress-depth 0
set-max-intset-entries 512
zset-max-ziplist-entries 128
zset-max-ziplist-value 64
hll-sparse-max-bytes 3000
stream-node-max-bytes 4096
stream-node-max-entries 100
activerehashing yes
client-output-buffer-limit normal 0 0 0
client-output-buffer-limit replica 256mb 64mb 60
client-output-buffer-limit pubsub 32mb 8mb 60
hz 10
dynamic-hz yes
aof-rewrite-incremental-fsync yes
rdb-save-incremental-fsync yes
requirepass foobared # 给redis设置密码
```

auth

密码认证

save

保存

flushall

清空所有的数据

redis的主从

```
只需要在从服务器上进行配置
slaveof ip地址 端口
如果有密码的话
masterauth <password>
命令行设置方式
config set masterauth password
```
- 基于异步的,平均每秒钟从服务器都会主服务器发送复制的情况
- 一个主可以多个从
- 不止主可以有从服务器,从也可以有从服务器
- 复制功能不会阻塞主服务器
- 复制功能也不会阻塞从服务器

redis的持久化

- rdb
- aof

rdb

- 优点
- 生成二进制文件
- 系统会默认的多长时间保存一次
- 直接手动保存
- 制作快照
- 可以用作备份
- 比较适合做灾难恢复
- 主进程会fork一个子进程出来,子进程用来复制保存数据
- 缺点
- 如果说数据需要尽量保存下来,则不适合实用rdb
- 在数据量庞大的时候,对系统消耗过大
```
save 900 1 # 在900秒以内有1次更新,就会持久化
save 300 10 
save 60 10000
stop-writes-on-bgsave-error yes
rdbcompression yes
rdbchecksum yes
dbfilename dump.rdb # 数据的保存文件
dir ./ # 保存目录
```

aof

- 优点
- 持久化更好
- aof将所有的操作都追加到一个文件中,redis-check-aof 
- 文件易读
- 缺点
- 文件会越来越大
- aof的速度会比rdb慢,aof 使用的是fsync
- 文件易读
```
appendonly yes
appendfilename "appendonly.aof"
appendfsync everysec
```

直接通过命令从rdb切换到aof

```
config set appendonly yes
config set save ""
```

redis的高可用

主观下线:哨兵检测不到服务

客观下线:全部哨兵同一协商

redis cluster

- 16384个槽位 
- 最少需要6台服务器

准备配置文件

```
## 6380
bind 127.0.0.1
port 6380
daemonize yes
pidfile 6380.pid
logfile 6380.log
cluster-enabled yes
cluster-config-file node-6380.conf
cluster-node-timeout 10000
## 6381
bind 127.0.0.1
port 6381
daemonize yes
pidfile 6381.pid
logfile 6381.log
cluster-enabled yes
cluster-config-file node-6381.conf
cluster-node-timeout 10000
## 6382
bind 127.0.0.1
port 6382
daemonize yes
pidfile 6382.pid
logfile 6382.log
cluster-enabled yes
cluster-config-file node-6382.conf
cluster-node-timeout 10000
## 6383
bind 127.0.0.1
port 6383
daemonize yes
pidfile 6383.pid
logfile 6383.log
cluster-enabled yes
cluster-config-file node-6383.conf
cluster-node-timeout 10000
## 6384
bind 127.0.0.1
port 6384
daemonize yes
pidfile 6384.pid
logfile 6384.log
cluster-enabled yes
cluster-config-file node-6384.conf
cluster-node-timeout 10000
## 6385
bind 127.0.0.1
port 6385
daemonize yes
pidfile 6385.pid
logfile 6385.log
cluster-enabled yes
cluster-config-file node-6385.conf
cluster-node-timeout 10000
```

环境准备

安装ruby

版本必须是2.2以上

```
wget https://cache.ruby-lang.org/pub/ruby/2.6/ruby-2.6.4.tar.gz
tar ruby-2.6.4.tar.gz
cd ruby-2.6.4
./configure --prefix=/opt/ruby
make && make install
配置环境变量
PATH=/opt/ruby/bin:$PATH
source 
```

安装ruby依赖

```
gem install redis
```

安装命令

```
./src/redis-cli --cluster help
./src/redis-cli --cluster create --cluster-replicas 1 127.0.0.1:6380 127.0.0.1:6381 127.0.0.1:6382 127.0.0.1:6383 127.0.0.1:6384 127.0.0.1:6385
create 创建集群
check 检查集群
info 查看集群信息
fix 修复集群
```

连接命令

```
redis-cli -c -p 6380
```
原文地址:https://www.cnblogs.com/sun-10387834/p/12807692.html