一、主从复制
1、是什么?
主机数据更新后根据配置和策略,自动同步到备机的 master / slave 机制,Master以写为主,Slave以读为主。
2、能干嘛?
- 读写分离
- 容灾恢复
3、怎么用?
1)配置从库不配主库
2)从库配置:slaveof、主库IP、主库端口
每次与master断开后,都要重新连接,除非你配置进redis.conf文件
info reputation:查案当前机器处于怎样的状态。
4、配置机器成为另一台机器的从机
slaveof IP 端口号
注意: 主机的端口要对从机开放,阿里云/腾讯云 安全组设置开放端口,bind开放以及关闭保护模式
#注意: 主机的端口要对从机开放,阿里云/腾讯云 安全组设置开放端口,bind开放以及关闭保护模式
# ~~~ WARNING ~~~ If the computer running Redis is directly exposed to the
# internet, binding to all the interfaces is dangerous and will expose the
# instance to everybody on the internet. So by default we uncomment the
# following bind directive, that will force Redis to listen only into
# the IPv4 loopback interface address (this means Redis will be able to
# accept connections only from clients running into the same computer it
# is running).
#
# IF YOU ARE SURE YOU WANT YOUR INSTANCE TO LISTEN TO ALL THE INTERFACES
# JUST COMMENT THE FOLLOWING LINE.
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
bind 0.0.0.0 #对外网开放
# Protected mode is a layer of security protection, in order to avoid that
# Redis instances left open on the internet are accessed and exploited.
#
# When protected mode is on and if:
#
# 1) The server is not binding explicitly to a set of addresses using the
# "bind" directive.
# 2) No password is configured.
#
# The server only accepts connections from clients connecting from the
# IPv4 and IPv6 loopback addresses 127.0.0.1 and ::1, and from Unix domain
# sockets.
#
# By default protected mode is enabled. You should disable it only if
# you are sure you want clients from other hosts to connect to Redis
# even if no authentication is configured, nor a specific set of interfaces
# are explicitly listed using the "bind" directive.
protected-mode no #关闭安全模式
5、主从复制的特点
- 主机支持读写,从机只支持读
- 一台机器变成从机则会全部复制主机数据
- 主机宕机重新恢复还是主机,从机宕机恢复还是从机