redis主从和哨兵搭建
redis端口为 8331
sentinel端口为 8332
参考连接
https://www.cnblogs.com/MYue/p/8855888.html
1、搭建主从 没有密码
redis.conf
注意 :从节点读写权限,自己设定是只读还是读写权限。
哨兵注意的写主节点的IP和端口
从节点配置
启用主从模式只有Redis Slave 添加一行,启动服务主从就配置好了。
slaveof 1.1.1.1 8331
2、redis有密码
redis.conf 注意
requirepass "admin.123" #设置redis登录密码
masterauth "admin.123" #主从认证密码,否则主从不能同步
vim sentinel.conf #在redis的跟目录下
port 8332
daemonize yes
protected-mode no #保护模式如果开启只接受回环地址的ipv4和ipv6地址链接,拒绝外部链接,而且正常应该配置多个哨兵,避免一个哨兵出现独裁情况,如果配置多个哨兵那如果开启也会拒绝其他sentinel的连接。导致哨兵配置无法生效。
logfile "/data/redis/logs/sentinel.log" #指明日志文件
dir "/data/redis/sentinel"
sentinel monitor mymaster 1.1.1.1 8331 2 #哨兵监控的master。
sentinel down-after-milliseconds mymaster 5000 #master或者slave多少时间(默认30秒)不能使用标记为down状态。
sentinel failover-timeout mymaster 9000 #若哨兵在配置值内未能完成故障转移操作,则任务本次故障转移失败。
sentinel auth-pass mymaster redispass #如果redis配置了密码,那这里必须配置认证,否则不能自动切换
/data/redis/src/redis-sentinel /data/redis/sentinel.conf #启动服务
如果有下面报错 启动命令后面要加上 --sentinel
*** FATAL CONFIG FILE ERROR ***
Reading the configuration file, at line 6
>>> 'sentinel monitor mymaster 192.168.198.131 6379 1'
sentinel directive while not in sentinel mode
自己总结的配置文件总结献上
主节点Master端口为
vi redis.conf
daemonize yes pidfile "/home/ap/aipf/datainsight/redis-3.0.6/8331/redis.pid" port 8331 tcp-backlog 511 timeout 0 tcp-keepalive 0 loglevel notice logfile "/home/ap/aipf/datainsight/redis-3.0.6/8331/8331.log" databases 16 save 900 1 save 300 10 save 60 10000 stop-writes-on-bgsave-error yes rdbcompression yes rdbchecksum yes dbfilename "dump-8331.rdb" dir "/home/ap/aipf/datainsight/redis-3.0.6/8331/" slave-serve-stale-data yes repl-diskless-sync no repl-diskless-sync-delay 5 repl-disable-tcp-nodelay no slave-priority 100 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 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-entries 512 list-max-ziplist-value 64 set-max-intset-entries 512 zset-max-ziplist-entries 128 zset-max-ziplist-value 64 hll-sparse-max-bytes 3000 activerehashing yes client-output-buffer-limit normal 0 0 0 client-output-buffer-limit slave 256mb 64mb 60 client-output-buffer-limit pubsub 32mb 8mb 60 hz 10 aof-rewrite-incremental-fsync yes
vi sentinel.conf
port 8332 dir "/home/ap/aipf/datainsight/redis-3.0.6/8332/" logfile "/home/ap/aipf/datainsight/redis-3.0.6/8332/8332.log" pidfile "/home/ap/aipf/datainsight/redis-3.0.6/8332/sentinel.pid" daemonize yes sentinel monitor mymaster 128.196.124.194 8331 2 sentinel down-after-milliseconds mymaster 30000 sentinel parallel-syncs mymaster 1 sentinel failover-timeout mymaster 180000
所有从节点配置都一样,添加监听主节点的IP和端口
daemonize yes
pidfile "/home/ap/aipf/datainsight/redis-3.0.6/8331/redis.pid"
port 8331
tcp-backlog 511
timeout 0
tcp-keepalive 0
loglevel notice
logfile "/home/ap/aipf/datainsight/redis-3.0.6/8331/8331.log"
databases 16
save 900 1
save 300 10
save 60 10000
stop-writes-on-bgsave-error yes
rdbcompression yes
rdbchecksum yes
dbfilename "dump-8331.rdb"
dir "/home/ap/aipf/datainsight/redis-3.0.6/8331/"
slaveof 128.196.124.194 8331
slave-serve-stale-data yes
repl-diskless-sync no
repl-diskless-sync-delay 5
repl-disable-tcp-nodelay no
slave-priority 100
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
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-entries 512
list-max-ziplist-value 64
set-max-intset-entries 512
zset-max-ziplist-entries 128
zset-max-ziplist-value 64
hll-sparse-max-bytes 3000
activerehashing yes
client-output-buffer-limit normal 0 0 0
client-output-buffer-limit slave 256mb 64mb 60
client-output-buffer-limit pubsub 32mb 8mb 60
hz 10
aof-rewrite-incremental-fsync yes
vi sentinel.conf
port 8332 dir "/home/ap/aipf/datainsight/redis-3.0.6/8332/" logfile "/home/ap/aipf/datainsight/redis-3.0.6/8332/8332.log" pidfile "/home/ap/aipf/datainsight/redis-3.0.6/8332/sentinel.pid" daemonize yes sentinel monitor mymaster 128.196.124.194 8331 2 sentinel down-after-milliseconds mymaster 30000 sentinel parallel-syncs mymaster 1 sentinel failover-timeout mymaster 180000