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
[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
[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
127.0.0.1:6379> keys * (empty list or set) 127.0.0.1:6379> set name jalja OK 127.0.0.1:6379>
127.0.0.1:6379> keys * (empty list or set) 127.0.0.1:6379> set name jalja OK 127.0.0.1:6379>
1、确定master与slave的redis端口是开放的,未被防火墙拦截
2、修改 master redis.cnf 文件中bind 为bind 0.0.0.0