主Redis写入数据时,从Redis会通过Redis Sync机制,同步数据,确保数据一致。一般为主写入,从读取。并且Redis有哨兵(Sentinel)机制,Redis主挂掉会自动帮我们提升从为主。哨兵监控redis进行状态,包括maste和slave,master宕机后,自动主从切换,主从切换后,配置文件会发生改变。
# 外部访问绑定
bind 0.0.0.0
protected-mode yes
# 端口
port 6379
tcp-backlog 511
# 超时时间
timeout 0
tcp-keepalive 300
# 守护进程
daemonize yes
supervised no
# 进程id存储
pidfile "/var/run/redis_6379.pid"
# 日志级别
loglevel notice
# 日志文件存储位置
logfile "/tmp/redis-log.log"
# 数据库的数量是可以配置的,默认情况下是16个
databases 16
always-show-logo yes
save 900 1
save 300 10
save 60 10000
stop-writes-on-bgsave-error yes
rdbcompression yes
rdbchecksum yes
# 持久化文件名称
dbfilename "dump.rdb"
# 持久化数据文件存放的路径
dir "/root/application/program/redis/datas"
# 主redis连接密码
masterauth "NMHmmgwauALmOk5TIt5kqYiLnugcMn7V"
slave-serve-stale-data no
slave-read-only yes
repl-diskless-sync no
repl-diskless-sync-delay 5
repl-disable-tcp-nodelay no
slave-priority 90
# redis数据库密码设置
requirepass "NMHmmgwauALmOk5TIt5kqYiLnugcMn7V"
lazyfree-lazy-eviction no
lazyfree-lazy-expire no
lazyfree-lazy-server-del no
slave-lazy-flush no
appendonly yes
appendfilename "appendonly.aof"
appendfsync always
no-appendfsync-on-rewrite no
auto-aof-rewrite-percentage 100
auto-aof-rewrite-min-size 64mb
aof-load-truncated yes
aof-use-rdb-preamble no
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
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
别的配置名称自己查注释 我懒
从配置的注释同主基本相同
只加了一个slaveof x.x.x.x 6379
bind 0.0.0.0
protected-mode yes
port 6379
tcp-backlog 511
timeout 0
tcp-keepalive 300
daemonize yes
supervised no
pidfile "/var/run/redis_6379.pid"
loglevel notice
logfile "/tmp/redis-log.log"
databases 16
always-show-logo yes
save 900 1
save 300 10
save 60 10000
stop-writes-on-bgsave-error yes
rdbcompression yes
rdbchecksum yes
dbfilename "dump.rdb"
dir "/root/redis-4.0.6"
masterauth "NMHmmgwauALmOk5TIt5kqYiLnugcMn7V"
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 90
requirepass "NMHmmgwauALmOk5TIt5kqYiLnugcMn7V"
lazyfree-lazy-eviction no
lazyfree-lazy-expire no
lazyfree-lazy-server-del no
slave-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 no
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
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
# 主redis的ip端口
slaveof x.x.x.x 6379
配置完成后启动主从redis服务
[root@iob6byZ redis-4.0.6]# ./src/redis-server redis.conf
启动完成后
启动redis-cli
输入密码 auth NMHmmgwauALmOk5TIt5kqYiLnugcMn7V
输入info
下边显示
#Replication
role:master # 说明这台是主
connected_slaves:2 # 说明有2个从
至此主从配置成功
[root@iZuf6yZ redis-4.0.6]# ./src/redis-cli
127.0.0.1:6379> auth NMHmmgwauALmOk5TIt5kqYiLnugcMn7V
OK
127.0.0.1:6379> info
# Server
redis_version:4.0.6
redis_git_sha1:00000000
redis_git_dirty:0
redis_build_id:59cbfe6ee7cf4162
redis_mode:standalone
os:Linux 2.6.32-696.6.3.el6.x86_64 x86_64
arch_bits:64
multiplexing_api:epoll
atomicvar_api:sync-builtin
gcc_version:4.4.7
process_id:31568
run_id:0a37d3489f3b607d485fc75ceb33ca35afe5cdfb
tcp_port:6379
uptime_in_seconds:3701
uptime_in_days:0
hz:10
lru_clock:10089267
executable:/root/redis-4.0.6/./src/redis-server
config_file:/root/redis-4.0.6/redis.conf
# Clients
connected_clients:7
client_longest_output_list:0
client_biggest_input_buf:0
blocked_clients:0
# Memory
used_memory:2106410
used_memory_human:2.01M
used_memory_rss:3170304
used_memory_rss_human:3.02M
used_memory_peak:2170898
used_memory_peak_human:2.07M
used_memory_peak_perc:97.03%
used_memory_overhead:2101254
used_memory_startup:835404
used_memory_dataset:5156
used_memory_dataset_perc:0.41%
total_system_memory:4153835520
total_system_memory_human:3.87G
used_memory_lua:37888
used_memory_lua_human:37.00K
maxmemory:0
maxmemory_human:0B
maxmemory_policy:noeviction
mem_fragmentation_ratio:1.50
mem_allocator:libc
active_defrag_running:0
lazyfree_pending_objects:0
# Persistence
loading:0
rdb_changes_since_last_save:0
rdb_bgsave_in_progress:0
rdb_last_save_time:1553591087
rdb_last_bgsave_status:ok
rdb_last_bgsave_time_sec:0
rdb_current_bgsave_time_sec:-1
rdb_last_cow_size:258048
aof_enabled:1
aof_rewrite_in_progress:0
aof_rewrite_scheduled:0
aof_last_rewrite_time_sec:0
aof_current_rewrite_time_sec:-1
aof_last_bgrewrite_status:ok
aof_last_write_status:ok
aof_last_cow_size:135168
aof_current_size:0
aof_base_size:0
aof_pending_rewrite:0
aof_buffer_length:0
aof_rewrite_buffer_length:0
aof_pending_bio_fsync:0
aof_delayed_fsync:0
# Stats
total_connections_received:19
total_commands_processed:16528
instantaneous_ops_per_sec:6
total_net_input_bytes:828448
total_net_output_bytes:4946090
instantaneous_input_kbps:0.29
instantaneous_output_kbps:2.70
rejected_connections:0
sync_full:2
sync_partial_ok:0
sync_partial_err:2
expired_keys:0
evicted_keys:0
keyspace_hits:0
keyspace_misses:0
pubsub_channels:1
pubsub_patterns:0
latest_fork_usec:183
migrate_cached_sockets:0
slave_expires_tracked_keys:0
active_defrag_hits:0
active_defrag_misses:0
active_defrag_key_hits:0
active_defrag_key_misses:0
# Replication
role:master
connected_slaves:2
slave0:ip=x.x.x.x,port=6379,state=online,offset=516742,lag=1
slave1:ip=x.x.x.x,port=6379,state=online,offset=516881,lag=0
master_replid:06280106d8770bb4f1db7fcf9d7835024322f094
master_replid2:fbdf1e7e73552d9ff2f8fbeee2908f0d30f7dcae
master_repl_offset:517161
second_repl_offset:23600
repl_backlog_active:1
repl_backlog_size:1048576
repl_backlog_first_byte_offset:1
repl_backlog_histlen:517161
# CPU
used_cpu_sys:2.10
used_cpu_user:1.63
used_cpu_sys_children:0.00
used_cpu_user_children:0.00
# Cluster
cluster_enabled:0
# Keyspace
添加配置文件sentinel.conf,启动哨兵即可。
sentinel监听端口,默认是26379,可以修改。
# 我的sentinel.conf文件配置
# 后台守护进程
daemonize yes
# 关闭protected-mode模式,此时外部网络可以直接访问
protected-mode no
# log日志
logfile /tmp/sentinel.log
# 端口
port 26379
dir "/tmp"
# sentinel monitor 告诉sentinel去监听地址为ip:port的一个master,这里的master-name可以自定义,quorum是一个数字,指明当有多少个sentinel认为一个master失效时,master才算真正失效。master-name只能包含英文字母,数字,和“.-_”这三个字符需要注意的是master-ip 要写真实的ip地址而不要用回环地址(127.0.0.1)。
# x.x.x.x为master的ip
sentinel monitor mymaster x.x.x.x 6379 1
# sentinel down-after-milliseconds 这个配置项指定了需要多少失效时间,一个master才会被这个sentinel主观地认为是不可用的。 单位是毫秒,默认为30秒,我设置的5秒
sentinel down-after-milliseconds mymaster 5000
# sentinel failover-timeout failover-timeout 可以用在以下这些方面:同一个sentinel对同一个master两次failover之间的间隔时间。当一个slave从一个错误的master那里同步数据开始计算时间。直到slave被纠正为向正确的master那里同步数据时。当想要取消一个正在进行的failover所需要的时间。 当进行failover时,配置所有slaves指向新的master所需的最大时间。不过,即使过了这个超时,slaves依然会被正确配置为指向master,但是就不按parallel-syncs所配置的规则来了。
sentinel failover-timeout mymaster 9000
# sentinel parallel-syncs 这个配置项指定了在发生failover主备切换时最多可以有多少个slave同时对新的master进行 同步,这个数字越小,完成failover所需的时间就越长,但是如果这个数字越大,就意味着越 多的slave因为replication而不可用。可以通过将这个值设为 1 来保证每次只有一个slave 处于不能处理命令请求的状态。
sentinel parallel-syncs mymaster 1
# sentinel auth-pass 设置连接master和slave时的密码,注意的是sentinel不能分别为master和slave设置不同的密码,因此master和slave的密码应该设置相同。
sentinel auth-pass mymaster NMHmmgwauALmOk5TIt5kqYiLnugcMn7V
然后就是启动主从redis服务的哨兵redis-sentinel sentinel.conf 后面是应用的配置
[root@instqe redis-4.0.6]# ./src/redis-sentinel sentinel.conf
主从启动完成后
随便打开一台输入redis-cli -p 26379
进入交互界面输入 info sentinel 查看哨兵的信息
master0:name=mymaster,status=ok,address=x.x.x.x:6379,slaves=2,sentinels=4
status=ok 状态ok
slaves=2表示2个从
name=mymaster 主redis的名字
address=x.x.x.x:6379 主redis的ip端口
[root@insqe redis-4.0.6]# redis-cli -p 26379
127.0.0.1:26379> info sentinel
# Sentinel
sentinel_masters:1
sentinel_tilt:0
sentinel_running_scripts:0
sentinel_scripts_queue_length:0
sentinel_simulate_failure_flags:0
master0:name=mymaster,status=ok,address=x.x.x.x:6379,slaves=2,sentinels=4
127.0.0.1:26379>
pythonQQ交流群:785239887