Linux-redis主从复制

设备:

centos7      10.0.0.7     redis-5.0.7   主节点

centos7      10.0.0.17   redis-5.0.7   从节点

centos7      10.0.0.27   redis-5.0.7   从节点

 

所欲机器的配置文件检查

配置文件检查

[root@centos7-liyj ~]#vim /usr/local/redis/etc/redis.conf
# ~~~ WARNING ~~~ If the computer running Redis is directly exposed to the
........
# IF YOU ARE SURE YOU WANT YOUR INSTANCE TO LISTEN TO ALL THE INTERFACES
# JUST COMMENT THE FOLLOWING LINE.
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
bind 0.0.0.0                 #所有主机访问,也可以是指定多个IP,以空格分隔

数据转储和文件路径

# The filename where to dump the DB
dbfilename dump.rdb          #redis数据转储文件名称

# The working directory.
#
# The DB will be written inside this directory, with the filename specified
# above using the 'dbfilename' configuration directive.
#
# The Append Only File will also be created inside this directory.
#
# Note that you must specify a directory here, not a file name.
dir /usr/local/redis/data/   #数据文件路径,此路径一般为,程序安装路径下的 /redis/data

redis 密码验证

################################## SECURITY ###################################

# Require clients to issue AUTH <PASSWORD> before processing any other
# commands.  This might be useful in environments in which you do not trust
# others with access to the host running redis-server.
#
# This should stay commented out for backward compatibility and because most
# people do not need auth (e.g. they run their own servers).
#
# Warning: since Redis is pretty fast an outside user can try up to
# 150k passwords per second against a good box. This means that you should
# use a very strong password otherwise it will be very easy to break.
#
# requirepass foobared
requirepass 123456           #操作redis需要AUTH进行密码验证

 

主节点redis配置

[root@centos7-liyj ~]#redis-cli 
127.0.0.1:6379> info replication       
NOAUTH Authentication required.           #提示没有AUTH密码校验,此处校验对比的是配置文件中 requirepass 123456
127.0.0.1:6379> keys key1
(error) NOAUTH Authentication required.
127.0.0.1:6379> auth 123456               #校验过密码,可以操作redis
OK
127.0.0.1:6379> SET key1 v1-master        #写入数据
OK
127.0.0.1:6379> keys key1
1) "key1"
127.0.0.1:6379> get key1
"v1-master"
127.0.0.1:6379> 

从节点10.0.0.17 配置

[root@centos7-liyj ~]#redis-cli 
127.0.0.1:6379> auth 123456
OK
127.0.0.1:6379> replicaof 10.0.0.7 6379        #在slave上设置master的IP和端口
127.0.0.1:6379> config set masterauth 123456   #在slave上设置master的密码,才可以同步
OK
127.0.0.1:6379> info replication
# Replication
role:slave
master_host:10.0.0.7
master_port:6379
master_link_status:up
master_last_io_seconds_ago:8
master_sync_in_progress:0
slave_repl_offset:0
slave_priority:100
slave_read_only:1
connected_slaves:0
master_replid:689c9c7f7960aa4119b3ebcd47a71807f38f6d25
master_replid2:0000000000000000000000000000000000000000
master_repl_offset:0
second_repl_offset:-1
repl_backlog_active:1
repl_backlog_size:1048576
repl_backlog_first_byte_offset:1
repl_backlog_histlen:0
127.0.0.1:6379> get key1
(nil)
127.0.0.1:6379> get key1
"v1-master"

从节点10.0.0.27 配置与17节点配置相同

[root@centos7-liyj ~]#redis-cli 
127.0.0.1:6379> auth 123456
OK
127.0.0.1:6379> replicaof 10.0.0.7 6379        #在slave上设置master的IP和端口
127.0.0.1:6379> config set masterauth 123456   #在slave上设置master的密码,才可以同步
OK
127.0.0.1:6379> info replication
# Replication
role:slave
master_host:10.0.0.7
master_port:6379
master_link_status:up
master_last_io_seconds_ago:8
master_sync_in_progress:0
slave_repl_offset:0
slave_priority:100
slave_read_only:1
connected_slaves:0
master_replid:689c9c7f7960aa4119b3ebcd47a71807f38f6d25
master_replid2:0000000000000000000000000000000000000000
master_repl_offset:0
second_repl_offset:-1
repl_backlog_active:1
repl_backlog_size:1048576
repl_backlog_first_byte_offset:1
repl_backlog_histlen:0
127.0.0.1:6379> get key1
(nil)
127.0.0.1:6379> get key1
"v1-master"

 从节点,在redis种的配置主从信息,是临时的,重启后消失

永久配置,写入配置文件

#
# replicaof <masterip> <masterport>
replicaof 10.0.0.7 6379        #主节点的IP地址,和端口

# If the master is password protected (using the "requirepass" configuration
# directive below) it is possible to tell the replica to authenticate before
# starting the replication synchronization process, otherwise the master will
# refuse the replica request.
#
# masterauth <master-password>
masterauth 123456              #mster密码验证 ,主节点的验证密码

 

 

 

 

 

 遇到的错误排查

127.0.0.1:6379> CONFIG SET masterauth 123456
OK
127.0.0.1:6379> info replication
# Replication
role:slave
master_host:10.0.0.7
master_port:6379
master_link_status:down        从节点显示down 并未同步
master_last_io_seconds_ago:-1
master_sync_in_progress:0
slave_repl_offset:1
master_link_down_since_seconds:1656322026
slave_priority:100
slave_read_only:1
connected_slaves:0
master_replid:48f7f9e999451a5f8f95262e2742e18132a413b5
master_replid2:70866bba58cc4ec49d9dfc9350ac784e2fd36cfd
master_repl_offset:0
second_repl_offset:1
repl_backlog_active:0
repl_backlog_size:1048576
repl_backlog_first_byte_offset:0
repl_backlog_histlen:0

查看从节点日志

[root@centos7-liyj ~]#tail /apps/redis/log/redis-6379.log 
5783:S 27 Jun 2022 17:54:21.881 * Connecting to MASTER 10.0.0.7:6379
5783:S 27 Jun 2022 17:54:21.881 * MASTER <-> REPLICA sync started
5783:S 27 Jun 2022 17:54:21.881 * Non blocking connect for SYNC fired the event.
5783:S 27 Jun 2022 17:54:21.881 * Master replied to PING, replication can continue...
5783:S 27 Jun 2022 17:54:21.882 * Partial resynchronization not possible (no cached master)
5783:S 27 Jun 2022 17:54:21.883 * Full resync from master: 9ebe8b1626617ac36b601af2f7d765ef9e07260d:0
5783:S 27 Jun 2022 17:54:21.941 # I/O error reading bulk count from MASTER: Resource temporarily unavailable  #是master节点有问题

查看主节点日志

6125:M 27 Jun 2022 17:54:57.270 * Replica 10.0.0.17:6379 asks for synchronization
6125:M 27 Jun 2022 17:54:57.270 * Full resync requested by replica 10.0.0.17:6379
6125:M 27 Jun 2022 17:54:57.270 * Starting BGSAVE for SYNC with target: disk
6125:M 27 Jun 2022 17:54:57.270 * Background saving started by pid 6233
6233:C 27 Jun 2022 17:54:57.270 # Failed opening the RDB file dump.rdb (in server root dir /data) for saving: Permission denied  #dump.rdb  /data路径和目录权限问题
6125:M 27 Jun 2022 17:54:57.339 # Background saving error
6125:M 27 Jun 2022 17:54:57.339 # Connection with replica 10.0.0.17:6379 lost.
6125:M 27 Jun 2022 17:54:57.340 # SYNC failed. BGSAVE child returned an error
6125:M 27 Jun 2022 17:54:57.800 * Replica 10.0.0.27:6379 asks for synchronization
6125:M 27 Jun 2022 17:54:57.800 * Full resync requested by replica 10.0.0.27:6379
6125:M 27 Jun 2022 17:54:57.800 * Starting BGSAVE for SYNC with target: disk
6125:M 27 Jun 2022 17:54:57.800 * Background saving started by pid 6234
6234:C 27 Jun 2022 17:54:57.801 # Failed opening the RDB file dump.rdb (in server root dir /data) for saving: Permission denied
6125:M 27 Jun 2022 17:54:57.842 # Background saving error
6125:M 27 Jun 2022 17:54:57.842 # Connection with replica 10.0.0.27:6379 lost.
6125:M 27 Jun 2022 17:54:57.842 # SYNC failed. BGSAVE child returned an error

查看主节点的配置文件

# The filename where to dump the DB
dbfilename dump.rdb

# The working directory.
#
# The DB will be written inside this directory, with the filename specified
# above using the 'dbfilename' configuration directive.
#
# The Append Only File will also be created inside this directory.
#
# Note that you must specify a directory here, not a file name.
dir /usr/local/redis/data/     #文件路径为 绝对路径

重启服务

systemctl  restart  redis

 

posted @ 2022-06-27 18:34  goodbay说拜拜  阅读(126)  评论(0编辑  收藏  举报