1.Redis集群 - 主从复制 作者:哇塞大嘴好帥(哇塞大嘴好帅)
主从复制
1.概念
讲一台Redis服务器数据复制到其他Redis服务器。被复制的服务器叫做主节点,复制被复制的服务器数据叫做从节点。主机以写为主,从机以读为主
2.作用
读写分离减少服务器压力。
1.数据yong余
2.故障恢复:当主节点出现问题时,可以由从节点提供服务,实现快速故障修复
`
3.负载均衡:配合读写分离,可以从主流点提供服务,由从节点提供读服务,分担服务器负载,
4.高可用:(集群)
3.环境配置
查看主从复制信息
127.0.0.1:6379> info replication
# Replication
role:master # 角色
connected_slaves:0 # 当前的从机数量
master_replid:36c21746381e4f54f6a66b7005e3ecdc321c41c3
master_replid2:0000000000000000000000000000000000000000
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
配置文件需要更改的位置
rdb保存文件名:
未修改
# The filename where to dump the DB
dbfilename dump.rdb
修改后
# The filename where to dump the DB
dbfilename dump6379.rdb
端口:
# Accept connections on the specified port, default is 6379 (IANA #815344).
# If port 0 is specified Redis will not listen on a TCP socket.
port 6379
日志:
# Specify the log file name. Also the empty string can be used to force
# Redis to log on the standard output. Note that if you use standard
# output for logging but daemonize, logs will be sent to /dev/null
logfile "6379.log"
pid文件:
# Creating a pid file is best effort: if Redis is not able to create it
# nothing bad happens, the server will start and run normally.
pidfile /var/run/redis_6379.pid
推荐:把数字换成端口号即可
如6379端口就把后面追加6379数字
[root@VM-0-3-centos redis-6.0.7]# ps -ef|grep redis
root 7475 7381 0 20:39 pts/0 00:00:00 redis-cli
root 17336 1 0 21:54 ? 00:00:00 redis-server *:6380
root 18090 1 0 21:59 ? 00:00:00 redis-server *:6381
root 18117 14790 0 21:59 pts/2 00:00:00 grep --color=auto redis
root 19091 1 0 Sep09 ? 00:01:45 redis-server *:6379
3.1 命令配置从机
命令配置从机是临时的,只要临机重启就会失效
设置从机的命令
[root@VM-0-3-centos redis-6.0.7]# redis-cli -p 6380 # 启动6380端口的Redis
127.0.0.1:6380> ping # 查看连接状态
PONG
127.0.0.1:6380> slaveof 127.0.0.1 6379 # 设置ip为127.0.0.1 端口为6379的服务器为主人
OK
127.0.0.1:6380> info replication # 查看Redis状态
# Replication
role:slave # 当前角色为从机
master_host:127.0.0.1 # IP
master_port:6379 #duankou
master_link_status:down
master_last_io_seconds_ago:-1
master_sync_in_progress:0
slave_repl_offset:1
master_link_down_since_seconds:1599833025
slave_priority:100
slave_read_only:1
connected_slaves:0
master_replid:133742b855fe55b7392de4ff77ee4fb5649ebe2a
master_replid2:0000000000000000000000000000000000000000
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
如果主机设置了密码
进入配置w文件
127.0.0.1:6380> vim redis6381.conf
查找/masterauth 回车搜索
masterauth 主人密码
重启对应的redis
进入主人Redis检查是否配置成功
127.0.0.1:6379> info replication
# Replication
role:master
connected_slaves:2 # 你的仆人数量
slave0:ip=127.0.0.1,port=6380,state=online,offset=196,lag=1
slave1:ip=127.0.0.1,port=6381,state=online,offset=196,lag=1
master_replid:f450fba3dd58a2374f5abee13b6b09024823328e
master_replid2:0000000000000000000000000000000000000000
master_repl_offset:196
second_repl_offset:-1
repl_backlog_active:1
repl_backlog_size:1048576
repl_backlog_first_byte_offset:1
repl_backlog_histlen:196
3.2 配置文件设置从机
在配置文件中找到*
配置文件信息
################################# REPLICATION #################################
# Master-Replica replication. Use replicaof to make a Redis instance a copy of
# another Redis server. A few things to understand ASAP about Redis replication.
#
# +------------------+ +---------------+
# | Master | ---> | Replica |
# | (receive writes) | | (exact copy) |
# +------------------+ +---------------+
#
# 1) Redis replication is asynchronous, but you can configure a master to
# stop accepting writes if it appears to be not connected with at least
# a given number of replicas.
# 2) Redis replicas are able to perform a partial resynchronization with the
# master if the replication link is lost for a relatively small amount of
# time. You may want to configure the replication backlog size (see the next
# sections of this file) with a sensible value depending on your needs.
# 3) Replication is automatic and does not need user intervention. After a
# network partition replicas automatically try to reconnect to masters
# and resynchronize with them.
# ip 端口
replicaof 127.0.0.1 6379
# 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 您的密码
#