Redis數(shù)據(jù)庫(kù)
的復(fù)制功能是支持多個(gè)數(shù)據(jù)庫(kù)之間的數(shù)據(jù)同步。一類是主數(shù)據(jù)庫(kù)(master
)一類是從數(shù)據(jù)庫(kù)(
slave
),主數(shù)據(jù)庫(kù)可以進(jìn)行讀寫操作,當(dāng)發(fā)生寫操作的時(shí)候自動(dòng)將數(shù)據(jù)同步到從數(shù)據(jù)庫(kù),而從數(shù)據(jù)庫(kù)一般是只讀的,并接收主數(shù)據(jù)庫(kù)同步過來的數(shù)據(jù),一個(gè)主數(shù)據(jù)庫(kù)可以有多個(gè)從數(shù)據(jù)庫(kù),而一個(gè)從數(shù)據(jù)庫(kù)只能有一個(gè)主數(shù)據(jù)庫(kù)。通過
redis
的復(fù)制功能可以很好的實(shí)現(xiàn)數(shù)據(jù)庫(kù)的讀寫分離,提高服務(wù)器的負(fù)載能力。主數(shù)據(jù)庫(kù)主要進(jìn)行寫操作,而從數(shù)據(jù)庫(kù)負(fù)責(zé)讀操作。
Redis
主從復(fù)制:主從復(fù)制可以允許多個(gè)
slave server
擁有和
master server
相同的數(shù)據(jù)庫(kù)副本
1
、
Redis
主從復(fù)制的特點(diǎn):
a
、
master
可以有多個(gè)
slave
b
、 多個(gè)
slave
可以鏈接同一個(gè)
master
外,還可以鏈接其他
slave
c
、 主從復(fù)制不會(huì)阻塞
master
,在數(shù)據(jù)同步的時(shí)候,
master
可以繼續(xù)處理
client
請(qǐng)求
d
、 提高系統(tǒng)的伸縮性
2
、
Redis
主從復(fù)制的過程:
a
、
slave
與
master
建立鏈接,發(fā)送
sync
同步請(qǐng)求。
b
、
master
會(huì)啟動(dòng)一個(gè)后臺(tái)進(jìn)程,將數(shù)據(jù)庫(kù)快照保存到文件中,同時(shí)
master
主進(jìn)程會(huì)開始收集新的寫命令并緩存。
c
、 后臺(tái)完成保存后,就將此文件發(fā)送給
slave
d
、
Slave
將此文件保存到硬盤上。
3
、
Redis
主從復(fù)制操作步驟
環(huán)境:
Redis
主從結(jié)構(gòu)支持一主多從(所有從節(jié)點(diǎn)的配置都一樣)
master
:
192.168.6.190
slave
:
192.168.6.191
配置:
配置slave
服務(wù)器,在
slave
服務(wù)器的配置文件中加入一下代碼
slaveof 192.168.222.1 6379 #
指定
master
的
ip
和端口
Masterauth jalja #
主機(jī)密碼
################################# 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
啟動(dòng)master服務(wù)器:
[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
啟動(dòng)slave服務(wù)器:
[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
測(cè)試:
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>
配置時(shí)遇到錯(cuò)誤:master_link_status:down
1
、確定
master
與
slave
的
redis
端口是開放的,未被防火墻攔截
2
、修改
master redis.cnf
文件中
bind
為
bind 0.0.0.0
來源:
博客園