redis 基础、持久化、主从、哨兵

mkdir -p /data/redis_cluster/redis_6379

mkdir -p /opt/redis_cluster/redis_6379/{conf,pid,logs}

cd /data/soft/

wget http://download.redis.io/releases/redis_3.2.9.tar.gz

tar zxf redis-3.2.9.tar.gz -C /opt/redis_cluster/

ln -s /opt/redis_cluster/redis-3.2.9 /opt/redis_cluster/redis

cd /opt/redis_cluster/redis

make && make install

 

################

daemonize yes    #以守护模式启动

bind 10.0.0.51

port 6379

pidfile /opt/redis_cluster/redis_6379/pid/redis_6379.pid

logfile /opt/redis_cluster/redis_6379/logs/redis_6379.log

databases 16     #设置数据库的数量,默认0

dbfilename redis_6379.rdb   #本地持久化文件名,默认dump.rdb

dir /data/redis_cluster/redis_6379    #本地数据库的目录

###################

 

启动

redis-server /opt/redis_cluster/redis_6379/conf/redis_6379.conf

关闭

redis-cli -h 127.0.0.1 shutdown

 

########字符串#######

set key value

get key

TYPE key   查看类型

 

INCR key  天然计数器 +1,incr一次 加一次,仅限数字

INCRBY key 100   当前数字上加100

 

MSET k1 v1 k2 v2 k3 v3 k4 v4    批量赋值,可覆盖已存在的key

MGET k1 k2 k3 k4  批量获取

 

EXISTS  k5 判断是否存在(1表示存在)

DEL k1 删除k1

 

ttl k2    查看过期时间,(-1用不过期,-2已经过期删除)

expire k2 10    设置十秒过期

persist k2 取消他的过期时间,设置为-1

########

主从模式下扩展新的从库接入

1、从库向主库发送同步请求

2、主库做bgsave ,然后将rdb发送给从库

3、从库清空旧数据,然后通过rdb 文件导入

slaveof ip 端口号  #从库上指定主库端口号接入

slaveof no one     #将自己设置为主库

##############

主库挂掉恢复:

从库使用slaveof no one 命令升级为主库

其他从库使用 slaveof  从库ip port 指定新主库

############################

持久化RDB
save 同步保存
#save 20 1 20秒内有1个key变化就保存
bgsave 异步保存
#bgsave 执行命令保存,注意pkill redis 时执行了bgsave+shutdown 两个操作,所以pkill redis进程不掉数据

持久化AOF
appendonly yes # 开启aof
appendfilename aof_louie.aof # aof 日志文件名
appendfsync everysec # 每秒记录一次日志,建议everysec
no-appendfsync-on-rewrite yes # 重写过程中是否向日志文件写入,yes 代表rewrite过程中,不向aof文件中追加信息,rewrite结束后再写入,no 代表rewrite执行的同时,也向aof追加信息
auto-aof-rewrite-percentage 100 # 触发重写文件增长百分比 默认100%
auto-aof-rewrite-min-size 64mb # 触发重写最小aof文件尺寸

关于aof重写
小于64M 不重写
大于64M时,第一次重写,压缩至33M
aof增长到66M 时,达到增长100%,并且大于64M,自动重写压缩至50M
类推,下次达到100M是,大于64M,再次重写,压缩。
重写过程会抵消 创建和删除key的命令 以及过期数据,起到压缩aof 文件作用

####################################################################

三台主从(7001,,7002,7003)

主(7001)

bind 0.0.0.0
protected-mode no
port 7001
daemonize yes
pidfile /opt/redis_cluster/redis_7001/pid/redis_7001.pid
dir /home/redis_cluster/redis_7001
logfile "/opt/redis_cluster/redis_7001/logs/redis.log"
appendonly yes
requirepass plefan
masterauth plefan

 

从(7002)

bind 0.0.0.0
protected-mode no
port 7002

daemonize yes
pidfile /opt/redis_cluster/redis_7002/pid/redis_7002.pid
dir /home/redis_cluster/redis_7002
logfile "/opt/redis_cluster/redis_7002/logs/redis.log"
replicaof 127.0.0.1 7001
masterauth plefan
appendonly yes
requirepass plefan

 

从(7003)

bind 0.0.0.0
protected-mode no
port 7003

daemonize yes
pidfile /opt/redis_cluster/redis_7003/pid/redis_7003.pid
dir /home/redis_cluster/redis_7003
logfile "/opt/redis_cluster/redis_7003/logs/redis.log"
replicaof 127.0.0.1 7001
masterauth plefan
appendonly yes
requirepass plefan

################################################################

哨兵集群 3台  (27001,27002,27003)

cat >cp /opt/redis_cluster/redis_7001/conf/sentinel_27001.conf<<EOF
port 27001
daemonize yes
bind 127.0.0.1
logfile "/opt/redis_cluster/redis_7001/logs/sentinel_27001.log"
dir "/home/redis_cluster/redis_7001"
sentinel monitor s1 127.0.0.1 7001 2
sentinel auth-pass s1 plefan #s1 为sentinel集群名,plefan 为redis集群验证账号
sentinel down-after-milliseconds s1 30000
sentinel parallel-syncs s1 1
sentinel failover-timeout s1 180000
EOF

cp /opt/redis_cluster/redis_7001/conf/sentinel_27001.conf /opt/redis_cluster/redis_7002/conf/sentinel_27002.conf
sed -i 's#27001#27002#g' /opt/redis_cluster/redis_7002/conf/sentinel_27002.conf
sed -i 's#redis_7001#redis_7002#g' /opt/redis_cluster/redis_7002/conf/sentinel_27002.con

cp /opt/redis_cluster/redis_7001/conf/sentinel_27001.conf /opt/redis_cluster/redis_7003/conf/sentinel_27003.conf
sed -i 's#27001#27003#g' /opt/redis_cluster/redis_7003/conf/sentinel_27003.conf
sed -i 's#redis_7001#redis_7003#g' /opt/redis_cluster/redis_7002/conf/sentinel_27002.con

redis-sentinel /opt/redis_cluster/redis_7001/conf/sentinel_27001.conf
redis-sentinel /opt/redis_cluster/redis_7002/conf/sentinel_27002.conf
redis-sentinel /opt/redis_cluster/redis_7003/conf/sentinel_27003.conf

启动后会生成一段配置,如果配置中出现了问题,要修改的话,清空配置文件,再写入。
例如sentinels=1的问题。

###################################################################

哨兵故障恢复

1.先启动7001

redis-server /opt/redis_cluster/redis_7001/conf/redis_7001.conf

2.启动哨兵

redis-sentinel /opt/redis_cluster/redis_27001/conf/redis_27001.conf

3.设置权重

7001调小权重值

CONFIG SET slave-priority 50

4.重新发起选举

在7001上的27001节点执行一次,master变成7001

redis-cli -h 127.0.0.1 -p 27001 Sentinel failover s1

5.观察主从复制是否正常

redis-cli

CONFIG GET slaveof

6.7001恢复权重 CONFIG SET slave-priority 100

 

查看权重 redis-cli -p 7001 CONFIG get slave-priority

查看当前主 redis-cli -p 7001 CONFIG GET slaveof

 

posted @ 2020-10-20 23:56  乌鸦yy  阅读(85)  评论(0编辑  收藏  举报