redis主从复制搭建

Posted 站点可靠性工程师

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了redis主从复制搭建相关的知识,希望对你有一定的参考价值。

redis主从架构

1、环境

系统centos7.6
redis版本 5.0.8
主192.168.47.188 从192.168.47.189

2、分别部署redis

我已经提前准备好了,具体步骤看我另一个博客

https://www.cnblogs.com/hsyw/p/13254117.html

3、主从配置文件更改

3.1、在188上更改配置文件(新增的,其他的都没改)

#从服务器连接密码
masterauth "tzh"
#cli连接密码
requirepass 123456

3.2、在189上操作(新增的,其他的都没改)

#配置主服务器的ip 端口
slaveof 192.168.47.188 6379
#配置主服务器的密码
masterauth 123456

4、启动

#启动主服务器
[root@t1 bin]# pwd
/app/redis/bin
[root@t1 bin]# ./redis-server ../conf/redis.conf
#启动主服务器
[root@t2 bin]# pwd
/app/redis/bin
[root@t2 bin]# ./redis-server ../conf/redis.conf

5、验证测试

5.1、验证

#在主服务器上,输入连接密码
[root@t1 bin]# ./redis-cli
127.0.0.1:6379> AUTH 123456
OK
127.0.0.1:6379> info replication
# Replication
role:master  #这是主服务器master
connected_slaves:1  #成功连接slave服务器的数量
slave0:ip=192.168.47.189,port=6379,state=online,offset=1691,lag=1  #slave服务器的信息
master_replid:f60d4b3d8b8f295491af42a3f57a7e59a8f60f7e
master_replid2:0000000000000000000000000000000000000000
master_repl_offset:1691
second_repl_offset:-1
repl_backlog_active:1
repl_backlog_size:1048576
repl_backlog_first_byte_offset:1
repl_backlog_histlen:169

#在从服务器连接
127.0.0.1:6379> INFO replication
# Replication
role:slave  #这是一台slave服务器
master_host:192.168.47.188   #主服务器端ip地址
master_port:6379 #主服务器端口号
master_link_status:up #连接状态
master_last_io_seconds_ago:7
master_sync_in_progress:0
slave_repl_offset:1943
slave_priority:100
slave_read_only:1 #成为slave服务器后,只读。不能set 只能get
connected_slaves:0
master_replid:f60d4b3d8b8f295491af42a3f57a7e59a8f60f7e
master_replid2:0000000000000000000000000000000000000000
master_repl_offset:1943
second_repl_offset:-1
repl_backlog_active:1
repl_backlog_size:1048576
repl_backlog_first_byte_offset:1
repl_backlog_histlen:1943

5.2、测试

#在主服务器上
127.0.0.1:6379> set test 123
OK
#在从服务器上
127.0.0.1:6379> get test
"123"
#说明可以主从搭建成功。有空在更新个哨兵模式,也很简单。在再一台服务器,增加个哨兵就可以了。

6、配置文件展示

#主配置文件
bind 0.0.0.0
protected-mode yes
port 6379
tcp-backlog 511
timeout 0
tcp-keepalive 300
daemonize yes
supervised no
pidfile /app/redis/pid/redis_6379.pid
loglevel notice
logfile "/app/redis/logs/redis-6379.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-6379.rdb
dir /app/redis/data
masterauth "tzh"
replica-serve-stale-data yes
replica-read-only yes
repl-diskless-sync no
repl-diskless-sync-delay 5
repl-disable-tcp-nodelay no
replica-priority 100
requirepass 123456
lazyfree-lazy-eviction no
lazyfree-lazy-expire no
lazyfree-lazy-server-del no
replica-lazy-flush no
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
aof-use-rdb-preamble 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-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
stream-node-max-bytes 4096
stream-node-max-entries 100
activerehashing yes
client-output-buffer-limit normal 0 0 0
client-output-buffer-limit replica 256mb 64mb 60
client-output-buffer-limit pubsub 32mb 8mb 60
hz 10
dynamic-hz yes
aof-rewrite-incremental-fsync yes
rdb-save-incremental-fsync yes
#从配置文件
bind 127.0.0.1
protected-mode yes
port 6379
tcp-backlog 511
timeout 0
tcp-keepalive 300
daemonize yes
supervised no
pidfile /app/redis/pid/redis_6379.pid
loglevel notice
logfile "/app/redis/logs/redis-6379.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-6379.rdb
dir /app/redis/data
slaveof 192.168.47.188 6379
masterauth 123456
replica-serve-stale-data yes
replica-read-only yes
repl-diskless-sync no
repl-diskless-sync-delay 5
repl-disable-tcp-nodelay no
replica-priority 100
lazyfree-lazy-eviction no
lazyfree-lazy-expire no
lazyfree-lazy-server-del no
replica-lazy-flush no
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
aof-use-rdb-preamble 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-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
stream-node-max-bytes 4096
stream-node-max-entries 100
activerehashing yes
client-output-buffer-limit normal 0 0 0
client-output-buffer-limit replica 256mb 64mb 60
client-output-buffer-limit pubsub 32mb 8mb 60
hz 10
dynamic-hz yes
aof-rewrite-incremental-fsync yes
rdb-save-incremental-fsync yes

7、配置文件详解

https://www.cnblogs.com/hsyw/p/13258587.html

以上是关于redis主从复制搭建的主要内容,如果未能解决你的问题,请参考以下文章

Redis学习--Redis主从复制与Redis集群搭建

Redis环境搭建创建服务搭建主从复制-Windows版本

Redis主从集群搭建及主从复制原理解析

NoSQL 之Redis主从复制哨兵和集群介绍及详细搭建步骤

Redis 进阶 -- 搭建主从复制及哨兵模式集群

Redis主从复制搭建