您的位置:首页 > 数据库 > Redis

Redis主从复制

2016-05-19 16:39 441 查看
  Redis主从复制。可以允许多个 slave server 拥有和master server 相同的数据库副本。同时通过读写分离(写主redis,读从redis)提高redis服务的性能和可用性。

一、redis 主从复制特点

二、redis 主从复制过程

三、redis 主从复制配置

四、测试以及监控

一、redis 主从复制特点

  1)、master 可以拥有多个 slave
  2)、多个 slave 可以连接同一个 master 外,还可以连接到其他 slave
  3)、主从复制不会阻塞 master,在同步数据时,master 可以继续处理 client 请求
  4)、提高系统的伸缩性

二、redis 主从复制过程

    当配置好 slave 后,slave 与 master 建立连接,然后发送 sync 命令。无论是第一次连接还是重新连接,master 都会启动一个后台进程,将数据库快照保存到文件,同 时 master 主进程会开始收集新的写命令并缓存。后台进程完成写文件后, master 就发送文件给 slave, slave将文件保存到硬盘上,再加载到内存中,接着 master就会把 缓存的命令转发给 slave,后续master 将收到的写命令发送给 slave。如果 master 同时收到多个 slave 发来的同步连接命令,master 只会启动一个进程来写数据库镜像, 然后发送给所有的 slave。

三、redis 主从复制配置

  1)开启master主redis的远程连接(默认redis是只允许localhost登录的),开启方式参考文章《Redis开启远程登录连接》。

  2)编辑redis.conf

  [root@root3 bin]# vim /etc/redis.conf


  #slaveof <masterip> <masterport>
  slaveof 192.168.1.1 6379   #指定 master 的 ip 和端口


  3) 如果主masterRedis服务器开启了安全验证,需要修改参数如下:

  # masterauth <master-password>
  masterauth 123456  #其中123456是主Redis服务器的安全验证密码


四、测试及监控

  1)启动后主库控制台日志如下:

  [7064] 09 Aug 20:13:20 * Slave ask for synchronization
  [7064] 09 Aug 20:13:20 * Starting BGSAVE for SYNC
  [7064] 09 Aug 20:13:20 * Background saving started by pid 7067
  [7067] 09 Aug 20:13:20 * DB saved on disk
  [7064] 09 Aug 20:13:20 * Background saving terminated with success
  [7064] 09 Aug 20:13:20 * Synchronization with slave succeeded
  [7064] 09 Aug 20:13:23 - 0 clients connected (1 slaves), 547380 bytes in use


  2) 从slaveRedis服务器控制台如下:

  5398:S 19 May 23:15:25.268 * Connecting to MASTER 192.168.0.16:6379
  5398:S 19 May 23:15:25.285 * MASTER <-> SLAVE sync started
  5398:S 19 May 23:15:25.285 * Non blocking connect for SYNC fired the event.
  5398:S 19 May 23:15:25.310 * Master replied to PING, replication can continue...
  5398:S 19 May 23:15:25.340 * Partial resynchronization not possible (no cached master)
  5398:S 19 May 23:15:25.358 * Full resync from master: 5c4fe75f10db8d1dfcab671245d5852119db046f:1
  5398:S 19 May 23:15:25.429 * MASTER <-> SLAVE sync: receiving 76 bytes from master
  5398:S 19 May 23:15:25.429 * MASTER <-> SLAVE sync: Flushing old data
  5398:S 19 May 23:15:25.429 * MASTER <-> SLAVE sync: Loading DB in memory
  5398:S 19 May 23:15:25.437 * MASTER <-> SLAVE sync: Finished with success


  3)我们在主库上设置一对键值对

  redis 127.0.0.1:6379> set name HongWan
  OK
  redis 127.0.0.1:6379>


  4)在从库上取一下这个键

  redis 127.0.0.1:6378> get name
  "HongWan"
  redis 127.0.0.1:6378>


  说明主从是同步正确。

  5)判断服务器主从,使用info命令

  127.0.0.1:6379> info
  ...
  # Replication
  role:master
  connected_slaves:1
  slave0:ip=192.168.0.18,port=6379,state=online,offset=6263,lag=1
  master_repl_offset:6263
  repl_backlog_active:1
  repl_backlog_size:1048576
  repl_backlog_first_byte_offset:2
  repl_backlog_histlen:6262
  ...


  

  127.0.0.1:6379> info
  ...
  # Replication
  role:slave
  master_host:192.168.0.16
  master_port:6379
  master_link_status:up
  master_last_io_seconds_ago:3
  master_sync_in_progress:0
  slave_repl_offset:6417
  slave_priority:100
  slave_read_only:1
  connected_slaves:0
  master_repl_offset:0
  repl_backlog_active:0
  repl_backlog_size:1048576
  repl_backlog_first_byte_offset:0
  repl_backlog_histlen:0
  ...


  6)从服务器中的info信息的master_link_status:up

  如果=up说明同步正常,=down说明同步异常。

参考文章:

    1.http://blog.csdn.net/yangzhenzhen/article/details/8512292

    2.http://blog.csdn.net/yangzhenzhen/article/details/8512292

    3.Redis实战《红丸出品》
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: