首页 > 其他 > 详细

centos部署redis主从

时间:2018-11-15 10:45:05      阅读:138      评论:0      收藏:0      [点我收藏+]

安装环境

CentOS 6.5 、CentOS 7.4

 主Redis:10.159.44.175

从Redis: 10.159.44.176、10.159.44.177

 

Redis下载和安装

在3台机器上分别安装redis

 

#添加yum仓库

yum install epel-release -y

#安装redis

yum install redis -y

程序文件说明

安装完毕后有以下几个文件位于/usr/bin目录:

redis-server:Redis服务器的daemon启动程序

redis-cli:Redis命令行操作工具,也可以用telnet根据其纯文本协议来操作

redis-benchmark:Redis性能测试工具,测试Redis在当前系统及配置下的读写性能

redis-check-aof:更新日志检查

redis-check-dump:用于本地数据库检查

 

iptables策略配置

如果iptable处于启用状态,且INPUT链默认规则为drop,则需要配置开放端口

firewall-cmd --zone=public --add-port=4100/tcp --permanent 

firewall-cmd --zone=public --add-port=4200/tcp --permanent

firewall-cmd --reload     #重新加载生效

**以上为端口根据实际情况添加,默认端口为:6370、26379

 

Redis主从配置

主redis的配置

在主Redis上编辑配置文件

# cat /etc/redis.conf

 

daemonize yes
pidfile "/var/run/redis/redis.pid"
port 4100
tcp-backlog 511
timeout 0
tcp-keepalive 0
loglevel notice
logfile "/var/log/redis/redis.log"
databases 16
save 900 1
save 300 10
save 60 10000
maxmemory 10g
stop-writes-on-bgsave-error yes
rdbcompression yes
rdbchecksum yes
dbfilename "dump.rdb"
dir "/var/lib/redis"
slave-serve-stale-data yes
slave-read-only yes
repl-diskless-sync no
repl-diskless-sync-delay 5
repl-disable-tcp-nodelay no
slave-priority 100
appendonly yes
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
masterauth "LQi#czFe$!"
requirepass "LQi#czFe$!"
# Generated by CONFIG REWRITE
protected-mode no

 

从redis配置

在2台从服务器上编辑Redis的配置文件

以上面主redis的配置文件为基准,添加一行

slaveof  <主redis服务器IP>  <端口>

即完成 从redis的配置。

 

如:slaveof 10.159.44.175 4100  # 指向10.159.44.175:4100作为master服务器

 

Sentinel的配置

在3台服务器上分别编辑redis-sentinel的配置文件

vi /etc/redis-sentinel.conf

port 4200       # 默认使用4200端口
daemonize yes
protected-mode no
logfile "/var/log/redis/sentinel.log"
pidfile "/var/run/redis/sentinel.pid"
dir "/tmp


sentinel monitor mymaster 10.159.44.175 4100 1      # 监视一个名为 mymaster 的主服务器, 这个主服务器的 IP 地址为 127.0.0.1 , 端口号为 4100 , 而将这个主服务器判断为失效至少需要 2 个 Sentinel 同意 (只要同意 Sentinel 的数量不达标,自动故障迁移就不会执行)。
sentinel down-after-milliseconds mymaster 5000       # 指定了 Sentinel 认为服务器已经断线所需的毫秒数。
sentinel config-epoch mymaster 10                            # 配置纪元,可以理解为配置的版本号,因为Sentinel会主动自动修改redis.conf和自己的redis-sentinel.conf来保持主从同步,此项数值可随意设置,但当Sentinel做过自动的主从切换以后,数值会加1。 

sentinel parallel-syncs mymaster 1                             # 在执行故障转移时, 最多可以有多少个从服务器同时对新的主服务器进行同步, 这个数字越小, 完成故障转移所需的时间就越长,但越大就意味着越多的从服务器因为复制而不可用。可以通过将这个值设为 1 来保证每次只有一个从服务器处于不能处理命令请求的状态。

注意:/etc/redis-sentinel.conf 必须要有写入权限。

常用命令

Redis的启动,停止,重启:

systemctl start redis.service

systemctl stop redis.service

systemctl restart redis.service

 

Redis Sentinel的启动,停止,重启:

systemctl start redis-sentinel.service

systemctl stop redis-sentinel.service

systemctl restart redis-sentinel.service

 

使用redis客户端连接服务初始化已经确认状态

redis-cli -p 4100 -h 127.0.0.1  # 使用参数指定:-p 端口 -h 服务器地址

127.0.0.1:6379> auth passwd                                #使用密码登录

OK

127.0.0.1:6379> config set protected-mode "no"   #修改redis的保护模式为no,不启用。

OK

127.0.0.1:6379> info                                              #查看状态

centos部署redis主从

原文:https://www.cnblogs.com/shawhe/p/redis.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!