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

Redis主从复制配置以及容灾测试

2013-01-21 19:50 751 查看
一、Redis的Replication:

这里首先需要说明的是,在Redis中配置Master-Slave模式真是太简单了。相信在阅读完这篇Blog之后你也可以轻松做到。这里我们还是先列出一些理论性的知识,后面给出实际操作的案例。

下面的列表清楚的解释了Redis Replication的特点和优势。

1). 同一个Master可以同步多个Slaves。

2). Slave同样可以接受其它Slaves的连接和同步请求,这样可以有效的分载Master的同步压力。因此我们可以将Redis的Replication架构视为图结构。

3). Master Server是以非阻塞的方式为Slaves提供服务。所以在Master-Slave同步期间,客户端仍然可以提交查询或修改请求。

4). Slave Server同样是以非阻塞的方式完成数据同步。在同步期间,如果有客户端提交查询请求,Redis则返回同步之前的数据。

5). 为了分载Master的读操作压力,Slave服务器可以为客户端提供只读操作的服务,写服务仍然必须由Master来完成。即便如此,系统的伸缩性还是得到了很大的提高。

6). Master可以将数据保存操作交给Slaves完成,从而避免了在Master中要有独立的进程来完成此操作。

二、Replication的工作原理:

在Slave启动并连接到Master之后,它将主动发送一个SYNC命令。此后Master将启动后台存盘进程,同时收集所有接收到的用于修改数据集的命令,在后台进程执行完毕后,Master将传送整个数据库文件到Slave,以完成一次完全同步。而Slave服务器在接收到数据库文件数据之后将其存盘并加载到内存中。此后,Master继续将所有已经收集到的修改命令,和新的修改命令依次传送给Slaves,Slave将在本次执行这些数据修改命令,从而达到最终的数据同步。

如果Master和Slave之间的链接出现断连现象,Slave可以自动重连Master,但是在连接成功之后,一次完全同步将被自动执行。

三、准备环境:

Master:10.20.189.218 Centos 5.6

Slave: 10.20.189.219 Centos 5.6

四、如何配置Replication

主控:

1.启动服务

[root@localhost ~]# redis-server /etc/redis_persistent_2.6.conf

2.查看主控配置

[root@localhost redis]# cat /etc/redis_persistent_2.6.conf |grep -v "^#"|grep -v "^$"

daemonize yes #redis 以后台进程运行,默认为NO

pidfile /var/run/redis_persistent.pid

port 6379

timeout 0 #保持客户端长连接

loglevel notice #日志级别,分为debug,verbose,notice,waring

logfile /data/logs/redis/redis_persitent.log

databases 16

save 900 1

save 300 10

save 60 10000

stop-writes-on-bgsave-error yes

rdbcompression yes #存储到本地数据库是否压缩,默认为yes

rdbchecksum yes

dbfilename dump.rdb

dir /data/redis

slave-serve-stale-data yes

slave-read-only yes

slave-priority 100

requirepass Kingsoft_Kss2012

maxclients 200

appendonly no

appendfilename redis_persistent.aof

appendfsync everysec

no-appendfsync-on-rewrite no

auto-aof-rewrite-percentage 100

auto-aof-rewrite-min-size 64mb

lua-time-limit 5000

slowlog-log-slower-than 100000

slowlog-max-len 128

hash-max-ziplist-entries 512

hash-max-ziplist-value 64

list-max-ziplist-entries 512

list-max-ziplist-value 64

set-max-intset-entries 512

zset-max-ziplist-entries 128

zset-max-ziplist-value 64

activerehashing yes

client-output-buffer-limit normal 0 0 0

client-output-buffer-limit slave 256mb 64mb 60

client-output-buffer-limit pubsub 32mb 8mb 60

从控:

3.把以上配置文件拷贝,然后加入2行即可启动服务:

slaveof 10.20.189.218 6379

masterauth Kingsoft_Kss2012

[root@localhost ~]# redis-server /etc/redis_persistent_2.6.conf

五、应用示例:

主控:




从控:





额外的命令:

#清空当前数据库中的所有Keys。

redis 127.0.0.1:6379> flushdb

OK

#删除其中一个测试Key,并查看删除后的结果。

redis 127.0.0.1:6379> del mykey

(integer) 1

[root@localhost redis]# redis-cli -h 10.20.189.218 -a Kingsoft_Kss2012 info |grep -A 3 'Replication'

# Replication

role:master

connected_slaves:1

slave0:10.20.189.219,6379,online
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: