• 每天进步一点点!

文章分类

推荐网站

常用手册

Redis的主从复制【原创】

<<返回

2015-04-01 17:46:16

在Redis中使用复制功能非常容易,只需要在从数据库的配置文件中加入“slaveof 主数据库IP 主数据库端口”即可,如果主数据库需要授权密码,还需要在从数据库的配置文件中加入主数据库的授权密码"masterauth 主数据库密码",主数据库无需进行任何配置。

 

原理


当一个从数据库启动后,会向主数据库发送SYNC命令,主数据库接收到SYNC命令后会开始在后台保存快照(即RDB持久化的过程),并将保存期间接收到的命令缓存起来。当快照完成后,Redis会将快照文件和所有缓存的命令发送给从数据库。从数据库收到后,会载入快照文件并执行收到的缓存的命令。当主从数据库断开重连后会重新执行上述操作,不支持断点续传。

 

拓扑图

 

  • 主数据库A:192.168.1.110:6379 
  • 从数据库B:192.168.1.112:6379 
  • 从数据库C:192.168.1.113:6379 

 

  1. 启动主数据库A
/usr/local/redis-stable/bin/redis-server /usr/local/redis-stable/etc/redis.conf  &

 

  1. 进入主数据库A,为了方便测试,这里用flushall命令清除所有数据。

 

[root@localhost ~]# /usr/local/redis-stable/bin/redis-cli  -a 123456
127.0.0.1:6379> flushall
[2801] 05 Apr 02:06:26.944 * DB saved on disk
OK
127.0.0.1:6379> keys *
(empty list or set)
127.0.0.1:6379> 

 

  1. 配置从数据库B,修改配置文件redis.conf,配置slaveof为主数据库A的IP地址和端口,masterauth设置成主数据库A的授权密码。
vim /usr/local/redis-stable/etc/redis.conf 

205 # slaveof
206 slaveof 192.168.1.110 6379
207
208 # If the master is password protected (using the "requirepass" configuration
209 # directive below) it is possible to tell the slave to authenticate before
210 # starting the replication synchronization process, otherwise the master will
211 # refuse the slave request.
212 #
213 # masterauth
214 masterauth 123456

 

  1. 启动从数据库B
/usr/local/redis-stable/bin/redis-server /usr/local/redis-stable/etc/redis.conf  &

 

启动时,会在屏幕上打印出如下内容,说明主数据A和从数据库B的主从配置时成功的,并且已经及时生效,完成了同步

 

[root@localhost ~]# 14467:S 06 Apr 04:07:32.466 * Connecting to MASTER 192.168.1.110:6379
14467:S 06 Apr 04:07:32.467 * MASTER <-> SLAVE sync started
14467:S 06 Apr 04:07:32.470 * Non blocking connect for SYNC fired the event.
14467:S 06 Apr 04:07:32.472 * Master replied to PING, replication can continue...
14467:S 06 Apr 04:07:32.474 * Partial resynchronization not possible (no cached master)
14467:S 06 Apr 04:07:32.475 * Full resync from master: 014caf11ed2406507d726d80a16c76127fb265cd:1
14467:S 06 Apr 04:07:32.600 * MASTER <-> SLAVE sync: receiving 18 bytes from master
14467:S 06 Apr 04:07:32.600 * MASTER <-> SLAVE sync: Flushing old data
14467:S 06 Apr 04:07:32.601 * MASTER <-> SLAVE sync: Loading DB in memory
14467:S 06 Apr 04:07:32.601 * MASTER <-> SLAVE sync: Finished with success

 

  1. 进入从数据库B,并用keys * 命令查看从数据B上的键。
/usr/local/redis-stable/bin/redis-cli -a 123456

 

[root@localhost ~]# /usr/local/redis-stable/bin/redis-cli -a 123456
127.0.0.1:6379> keys *
(empty list or set)
127.0.0.1:6379>

 

  1. 下面开始测试,看一下主从同步的效果。

 

主数据库A:

127.0.0.1:6379> set name fisher
OK
127.0.0.1:6379> set age 1
OK
127.0.0.1:6379> set sex 1
OK
127.0.0.1:6379> keys *
1) "sex"
2) "name"
3) "age"
127.0.0.1:6379>

 

从数据库B:

127.0.0.1:6379> keys *
1) "name"
2) "sex"
3) "age"
127.0.0.1:6379> get name
"fisher"
127.0.0.1:6379> get sex
"1"
127.0.0.1:6379> get age
"1"
127.0.0.1:6379>

 

可见主从同步成功了,主数据库上的写操作都已经同步到了从数据库上了。

 

  1. 新增一个节点从数据库C,配置C上的redis.conf,配置与配置从数据库B的方法相同
vim /usr/local/redis-stable/etc/redis.conf

 

205 # slaveof
206 slaveof 192.168.1.110 6379
207
208 # If the master is password protected (using the "requirepass" configuration
209 # directive below) it is possible to tell the slave to authenticate before
210 # starting the replication synchronization process, otherwise the master will
211 # refuse the slave request.
212 #
213 # masterauth
214 masterauth 123456

 

  1. 启动从数据库C
/usr/local/redis-stable/bin/redis-server /usr/local/redis-stable/etc/redis.conf  &

 

屏幕上打印出如下内容:

 

[root@localhost ~]# 14475:S 06 Apr 04:27:37.001 * Connecting to MASTER 192.168.1.110:6379
14475:S 06 Apr 04:27:37.001 * MASTER <-> SLAVE sync started
14475:S 06 Apr 04:27:37.003 * Non blocking connect for SYNC fired the event.
14475:S 06 Apr 04:27:37.004 * Master replied to PING, replication can continue...
14475:S 06 Apr 04:27:37.004 * Partial resynchronization not possible (no cached master)
14475:S 06 Apr 04:27:37.005 * Full resync from master: 014caf11ed2406507d726d80a16c76127fb265cd:1797
14475:S 06 Apr 04:27:37.040 * MASTER <-> SLAVE sync: receiving 47 bytes from master
14475:S 06 Apr 04:27:37.041 * MASTER <-> SLAVE sync: Flushing old data
14475:S 06 Apr 04:27:37.041 * MASTER <-> SLAVE sync: Loading DB in memory
14475:S 06 Apr 04:27:37.041 * MASTER <-> SLAVE sync: Finished with success

 

可以看到主从同步已完成

 

  1. 进入从数据库C,用keys * 命令查看,是否已将主数据库A上的数据同步过来
/usr/local/redis-stable/bin/redis-cli -a 123456

[root@localhost ~]# /usr/local/redis-stable/bin/redis-cli -a 123456
127.0.0.1:6379> keys *
1) "sex"
2) "age"
3) "name"
127.0.0.1:6379> get sex
"1"
127.0.0.1:6379> get age
"1"
127.0.0.1:6379> get name
"fisher"
127.0.0.1:6379>

 

可见主数据库A上的数据已经成功同步到了新增的从数据库C上。同步是在启动C时开始的,主数据库不需要停止redis,也无需清空数据。

 

图结构

 

从数据库不仅可以接收主数据库的同步数据,自己也可以同时作为主数据库存在,形成类似图的结构,如下图所示,数据库A的数据会同步到B和C中,而B中的数据会同步到D和E中。向B中写入数据不会同步到A或C中,只会同步到D和E中。

文章评论

  • 暂无评论

发表评论

昵称:

内容:

发表评论