Redis的複製功能是支持多個資料庫之間的數據同步。一類是主資料庫(master)一類是從資料庫(slave),主資料庫可以進行讀寫操作,當發生寫操作的時候自動將數據同步到從資料庫,而從資料庫一般是只讀的,並接收主資料庫同步過來的數據,一個主資料庫可以有多個從資料庫,而一個從資料庫只能有一個主資料庫... ...
該文使用centos6.5 64位 redis3.2.8
主從複製
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 #主機密碼
![複製代碼](http://common.cnblogs.com/images/copycode.gif)
################################# 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
![複製代碼](http://common.cnblogs.com/images/copycode.gif)
啟動master伺服器:
[root@localhost bin]# ./redis-server /usr/local/redis/etc/redis.conf
查看master配置信息:127.0.0.1:6379> info
![複製代碼](http://common.cnblogs.com/images/copycode.gif)
# 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
![複製代碼](http://common.cnblogs.com/images/copycode.gif)
啟動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 jalja OK 127.0.0.1:6379>
slave:
127.0.0.1:6379> keys * (empty list or set) 127.0.0.1:6379> set name jalja OK 127.0.0.1:6379>
配置時遇到錯誤:master_link_status:down
1、確定master與slave的redis埠是開放的,未被防火牆攔截
2、修改 master redis.cnf 文件中bind 為bind 0.0.0.0