Skip to content

Redis各模式搭建到放弃

CentOS7编译安装Redis5

shell
#!/bin/bash
# 安装路径: /usr/local/redis
cd /usr/local
wget http://download.redis.io/releases/redis-5.0.0.tar.gz
tar -xzvf redis-5.0.0.tar.gz
cd redis-5.0.0
make
make install PREFIX=/usr/local/redis
cp redis.conf sentinel.conf /usr/local/redis/
cd /usr/local/redis
mkdir data log var

Redis单主

万一db文件有损,直接凉凉

text
# 常用配置项
bind 0.0.0.0
port 6379
daemonize no
pidfile /usr/local/redis/var/redis_6379.pid
logfile "/usr/local/redis/log/redis_6379.log"
dir "/usr/local/redis/data/"
dbfilename dump_6379.rdb
maxclients 30000
appendonly no
#requirepass 访问密码
shell
# 启动脚本
/usr/local/redis/bin/redis-server /usr/local/redis/redis.conf &

Redis主从

故障手动切换,有数据备份

text
# Master 192.168.60.110
bind 0.0.0.0
port 6379
daemonize no
pidfile /usr/local/redis/var/redis_6379.pid
logfile "/usr/local/redis/log/redis_6379.log"
dir "/usr/local/redis/data/"
dbfilename dump_6379.rdb
maxclients 30000
appendonly no
#requirepass 访问密码
 
# Slave 192.168.60.111
bind 0.0.0.0
port 6379
daemonize no
pidfile /usr/local/redis/var/redis_6379.pid
logfile "/usr/local/redis/log/redis_6379.log"
dir "/usr/local/redis/data/"
dbfilename dump_6379.rdb
maxclients 30000
appendonly no
#masterauth 访问密码
slaveof 192.168.60.110 6379
shell
# 启动脚本
/usr/local/redis/bin/redis-server /usr/local/redis/redis.conf &

Redis一主两从

故障手动切换,有数据备份

text
# Master 192.168.60.110
bind 0.0.0.0
port 6379
daemonize no
pidfile /usr/local/redis/var/redis_6379.pid
logfile "/usr/local/redis/log/redis_6379.log"
dir "/usr/local/redis/data/"
dbfilename dump_6379.rdb
maxclients 30000
appendonly no
#requirepass 访问密码
 
# Slave1 192.168.60.111
bind 0.0.0.0
port 6379
daemonize no
pidfile /usr/local/redis/var/redis_6379.pid
logfile "/usr/local/redis/log/redis_6379.log"
dir "/usr/local/redis/data/"
dbfilename dump_6379.rdb
maxclients 30000
appendonly no
#masterauth 访问密码
slaveof 192.168.60.110 6379
 
# Slave2 192.168.60.112
bind 0.0.0.0
port 6379
daemonize no
pidfile /usr/local/redis/var/redis_6379.pid
logfile "/usr/local/redis/log/redis_6379.log"
dir "/usr/local/redis/data/"
dbfilename dump_6379.rdb
maxclients 30000
appendonly no
#masterauth 访问密码
slaveof 192.168.60.110 6379
shell
# 启动脚本
/usr/local/redis/bin/redis-server /usr/local/redis/redis.conf &

Redis一主二从三哨兵

有数据备份,自动漂移切换节点,需要代码支持

text
# Master 192.168.60.110
bind 0.0.0.0
port 6379
daemonize no
pidfile /usr/local/redis/var/redis_6379.pid
logfile "/usr/local/redis/log/redis_6379.log"
dir "/usr/local/redis/data/"
dbfilename dump_6379.rdb
maxclients 30000
appendonly no
#requirepass 访问密码
#masterauth 访问密码
 
# Slave1 192.168.60.111
bind 0.0.0.0
port 6379
daemonize no
pidfile /usr/local/redis/var/redis_6379.pid
logfile "/usr/local/redis/log/redis_6379.log"
dir "/usr/local/redis/data/"
dbfilename dump_6379.rdb
maxclients 30000
appendonly no
#requirepass 访问密码
#masterauth 访问密码
slaveof 192.168.60.110 6379
 
# Slave2 192.168.60.112
bind 0.0.0.0
port 6379
daemonize no
pidfile /usr/local/redis/var/redis_6379.pid
logfile "/usr/local/redis/log/redis_6379.log"
dir "/usr/local/redis/data/"
dbfilename dump_6379.rdb
maxclients 30000
appendonly no
#requirepass 访问密码
#masterauth 访问密码
slaveof 192.168.60.110 6379

sentinel.conf

text
# Master 192.168.60.110
bind 0.0.0.0
port 26379
protected-mode no
## 这个ID得不一样
sentinel myid a77c390022ef2aed3a25ec8cb46b81776a7d079a
## 监听master节点
sentinel monitor test-sentinel 192.168.60.110 6379 2
# 设定5秒内没有响应,说明服务器挂了
sentinel down-after-milliseconds test-sentinel 5000
# 设定15秒内master没有活起来,就重新选举主
sentinel failover-timeout test-sentinel 15000
sentinel parallel-syncs test-sentinel 2
## sentinel auth-pass test-sentinel 访问密码
logfile "/usr/local/redis/log/sentinel_26379.log"
dir "/usr/local/redis"
 
# Slave1 192.168.60.111
bind 0.0.0.0
port 26379
protected-mode no
## 这个ID得不一样
sentinel myid a77c390022ef2aed3a25ec8cb46b81776a7d079b
## 监听master节点
sentinel monitor test-sentinel 192.168.60.110 6379 2
# 设定5秒内没有响应,说明服务器挂了
sentinel down-after-milliseconds test-sentinel 5000
# 设定15秒内master没有活起来,就重新选举主
sentinel failover-timeout test-sentinel 15000
sentinel parallel-syncs test-sentinel 2
## sentinel auth-pass test-sentinel 访问密码
logfile "/usr/local/redis/log/sentinel_26379.log"
dir "/usr/local/redis"
 
# Slave2 192.168.60.112
bind 0.0.0.0
port 26379
protected-mode no
## 这个ID得不一样
sentinel myid a77c390022ef2aed3a25ec8cb46b81776a7d079c
## 监听master节点
sentinel monitor test-sentinel 192.168.60.110 6379 2
# 设定5秒内没有响应,说明服务器挂了
sentinel down-after-milliseconds test-sentinel 5000
# 设定15秒内master没有活起来,就重新选举主
sentinel failover-timeout test-sentinel 15000
sentinel parallel-syncs test-sentinel 2
## sentinel auth-pass test-sentinel 访问密码
logfile "/usr/local/redis/log/sentinel_26379.log"
dir "/usr/local/redis"

启动顺序

text
所有Master->所有Slave->所有Sentinel

启动脚本

shell
# redis-server通用
/usr/local/redis/bin/redis-server /usr/local/redis/conf/redis.conf &
 
# redis-sentinel通用
/usr/local/redis/bin/redis-sentinel /usr/local/redis/conf/sentinel.conf &

Redis三主三从集群

text
# 请务必保证有6个节点,无论怎么弄,一定需要6个节点
# 如果在同一台机器上搞集群,请保证端口和相关文件名各不相同,请不要让我看扁你
 
# 通用配置
bind 0.0.0.0
port 6379
daemonize no
pidfile /usr/local/redis/var/redis_6379.pid
logfile "/usr/local/redis/log/redis_6379.log"
dir "/usr/local/redis/data/"
dbfilename dump_6379.rdb
maxclients 30000
appendonly no
# masterauth 访问密码
# 开启集群模式
cluster-enabled yes
# 每一个节点需要有一个配置文件,需要6份。每个节点处于集群的角色都需要告知其他所有节点,彼此知道,这个文件用于存储集群模式下的集群状态等信息,这个文件是由redis自己维护,我们不用管。如果你要重新创建集群,那么把这个文件删了就行
cluster-config-file /usr/local/redis/nodes-6379.conf
# 超时时间,超时则认为master宕机,随后主备切换
cluster-node-timeout 5000
# 开启AOF
appendonly yes
# aof文件名
appendfilename "appendonly_6379.aof"
# 删除工作空间中rdb和aof文件
# 分别启动6台redis,然后再任意一台节点机上在执行以下命令,执行完会出现日志,6台的主从关系都会显示
shell
# 创建集群
/usr/local/redis/bin/redis-cli --cluster create ip1:port1 ip2:port2 ip3:port3 ip4:port4 ip5:port5 ip6:port6 --cluster-replicas 1
 
# 测试节点
/usr/local/redis/bin/redis-cli -a imooc --cluster check ip1:6379