1.Redis主从同步原理

redis主服务器会周期性的把更新的数据写入磁盘或者把修改操作写入追加的记录文件,然后将数据文件同步给从服务器,从服务器加载记录文件,在内存库中更新新数据.

2.VMWare配置2台服务器

在已安装好redis服务的虚拟机,重命名为Redis-Master(主机),另外克隆一个虚拟机,重命名Redis-Slave(从机)

Redis主从环境配置-LMLPHP

查看IP:192.168.74.128;

查看IP跟主机同一网段192.168.74.129;

打开PuTTY(session1),连接主机,(session2),连接从机.

session1启动主机的Redis服务,我们可以看到现在库里没有任何key;

Redis主从环境配置-LMLPHP

主机不用怎么设置,session2从机打开redis.conf文件设置如下:

# Master-Slave replication. Use slaveof to make a Redis instance a copy of
# another Redis server. A few things to understand ASAP about Redis replication.
#
# 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 slaves.
# 2) Redis slaves 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 slaves automatically try to reconnect to masters
# and resynchronize with them.
#
# slaveof <masterip> <masterport>
slaveof 192.168.74.128 6379

如果主机设置了密码,在此处设置连接主机的密码

# If the master is password protected (using the "requirepass" configuration
# directive below) it is possible to tell the slave to authenticate before
# starting the replication synchronization process, otherwise the master will
# refuse the slave request.
#
# masterauth <master-password>
masterauth 123456

启动session2启动从机Redis服务,并用客户端连接,同样数据库为空.

Redis主从环境配置-LMLPHP

此时,session1中主机增加一个key,切换到session2,从机立刻能获得主机同步过来的数据,这样实现了主从的数据同步.

Redis主从环境配置-LMLPHP

05-08 08:27