Redis数据库 
 的复制功能是支持多个数据库之间的数据同步。一类是主数据库(master 
 
)一类是从数据库( 
 slave 
 ),主数据库可以进行读写操作,当发生写操作的时候自动将数据同步到从数据库,而从数据库一般是只读的,并接收主数据库同步过来的数据,一个主数据库可以有多个从数据库,而一个从数据库只能有一个主数据库。通过 
 redis 
 的复制功能可以很好的实现数据库的读写分离,提高服务器的负载能力。主数据库主要进行写操作,而从数据库负责读操作。 
  
 
   Redis 
 主从复制:主从复制可以允许多个 
 slave server  
 拥有和 
 master server 
 相同的数据库副本 
 
 
   1 
 、 
 Redis 
 主从复制的特点: 
 
 
   a 
 、  
 master  
 可以有多个 
 slave 
 
 
   b 
 、 多个 
 slave  
 可以链接同一个 
 master 
 外,还可以链接其他 
 slave 
 
 
   c 
 、 主从复制不会阻塞 
 master 
 ,在数据同步的时候, 
 master 
 可以继续处理 
 client 
 请求 
 
 
   d 
 、 提高系统的伸缩性 
 
 
   2 
 、 
 Redis 
 主从复制的过程: 
 
 
   a 
 、  
 slave 
 与 
 master 
 建立链接,发送 
 sync 
 同步请求。 
 
 
   b 
 、  
 master 
 会启动一个后台进程,将数据库快照保存到文件中,同时 
 master 
 主进程会开始收集新的写命令并缓存。 
 
 
   c 
 、 后台完成保存后,就将此文件发送给 
 slave 
 
 
   d 
 、  
 Slave 
 将此文件保存到硬盘上。 
 
 
   3 
 、 
 Redis  
 主从复制操作步骤 
 
 
    
 环境: 
 
 
   Redis 
 主从结构支持一主多从(所有从节点的配置都一样) 
 
 
   master 
 : 
 192.168.6.190 
 
 
   slave 
 : 
 192.168.6.191 
 
 
    
 配置: 
 
 
   配置slave 
 服务器,在 
 slave 
 服务器的配置文件中加入一下代码 
 
 
   slaveof 192.168.222.1 6379 # 
 指定 
 master 
 的 
 ip 
 和端口 
 
  Masterauth jalja # 
 主机密码 
 
  ################################# REPLICATION ################################# 
 
  # 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.6.190 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 jalja 
 
 
    
 启动master服务器: 
 
 
   [root@localhost bin]# ./redis-server /usr/local/redis/etc/redis.conf 
 
 
   查看master 
 配置信息: 
 127.0.0.1:6379> info 
 
  # Replication 
 
  role:master 
 
  connected_slaves:1 
 
  slave0:ip=192.168.6.191,port=6379,state=online,offset=141,lag=0 
 
  master_repl_offset:141 
 
  repl_backlog_active:1 
 
  repl_backlog_size:1048576 
 
  repl_backlog_first_byte_offset:2 
 
  repl_backlog_histlen:140 
 
 
    
 启动slave服务器: 
 
 
   [root@localhost bin]# ./redis-server /usr/local/redis/etc/redis.conf 
 
 
   查看slave 
 配置信息: 
 127.0.0.1:6379> info 
 
  # Replication 
 
  role:slave 
 
  master_host:192.168.6.190 
 
  master_port:6379 
 
  master_link_status:up 
 
  master_last_io_seconds_ago:1 
 
  master_sync_in_progress:0 
 
  slave_repl_offset:99 
 
  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 
 
 
    
 测试: 
 
 
    
 master: 
 
 
   127.0.0.1:6379>  
 keys * 
 
 
   ( 
 empty  
 list  
 or  
 set) 
 
 
   127.0.0.1:6379>  
 set  
 name  
 jaljaOK 
 
 
   127.0.0.1:6379> 
 
 
    
 slave: 
 
 
   127.0.0.1:6379>  
 keys * 
 
 
   ( 
 empty  
 list  
 or  
 set) 
 
 
   127.0.0.1:6379>  
 set  
 name  
 jaljaOK 
 
 
   127.0.0.1:6379> 
 
 
    
 配置时遇到错误:master_link_status:down 
 
 
   1 
 、确定 
 master 
 与 
 slave 
 的 
 redis 
 端口是开放的,未被防火墙拦截 
 
 
   2 
 、修改  
 master redis.cnf  
 文件中 
 bind  
 为 
 bind 0.0.0.0 
 
 
 
  来源: 
 博客园