Redis 主从配置

一: redis 主从复制过程

当配置好slave 后,slave 与master 建立连接,然后发送sync 命令。无论是第一次连接还是 重新连接,master 都会启动一个后台进程,将数据库快照保存到文件中,同时master 主进
程会开始收集新的写命令并缓存。后台进程完成写文件后,master 就发送文件给slave,slave 将文件保存到硬盘上,再加载到内存中,接着master 就会把缓存的命令转发给slave,后续
master 将收到的写命令发送给slave。如果master 同时收到多个slave 发来的同步连接命令, master 只会启动一个进程来写数据库镜像,然后发送给所有的slave。

二: 如何配置
配置slave 服务器很简单,只需要在slave 的配置文件中加入如下配置
slaveof 192.168.1.1 6379 #指定master 的ip 和端口
下面我们做一个实验来演示如何搭建一个主从环境:
# slaveof
slaveof localhost 6379
我们在一台机器上启动主库(端口6379),从库(端口6378)
启动后主库控制台日志如下:
[root@localhost redis-2.2.12]# src/redis-server redis.conf
[7064] 09 Aug 20:13:12 * Server started, Redis version 2.2.12
[7064] 09 Aug 20:13:12 # WARNING overcommit_memory is set to 0! Background save may fail under
low memory condition. To fix this issue add 'vm.overcommit_memory = 1' to /etc/sysctl.conf and
then reboot or run the command 'sysctl vm.overcommit_memory=1' for this to take effect.
[7064] 09 Aug 20:13:12 * The server is now ready to accept connections on port 6379
[7064] 09 Aug 20:13:13 - 0 clients connected (0 slaves), 539512 bytes in use
[7064] 09 Aug 20:13:18 - 0 clients connected (0 slaves), 539512 bytes in use
[7064] 09 Aug 20:13:20 - Accepted 127.0.0.1:37789
[7064] 09 Aug 20:13:20 * Slave ask for synchronization
[7064] 09 Aug 20:13:20 * Starting BGSAVE for SYNC
[7064] 09 Aug 20:13:20 * Background saving started by pid 7067
[7067] 09 Aug 20:13:20 * DB saved on disk
[7064] 09 Aug 20:13:20 * Background saving terminated with success
[7064] 09 Aug 20:13:20 * Synchronization with slave succeeded
[7064] 09 Aug 20:13:23 - 0 clients connected (1 slaves), 547380 bytes in use
启动后从库控制台日志如下:
[root@localhost redis-2.2.12]# src/redis-server redis.slave
[7066] 09 Aug 20:13:20 * Server started, Redis version 2.2.12
[7066] 09 Aug 20:13:20 # WARNING overcommit_memory is set to 0! Background save may fail under
low memory condition. To fix this issue add 'vm.overcommit_memory = 1' to /etc/sysctl.conf and
then reboot or run the command 'sysctl vm.overcommit_memory=1' for this to take effect.
[7066] 09 Aug 20:13:20 * The server is now ready to accept connections on port 6378
[7066] 09 Aug 20:13:20 - 0 clients connected (0 slaves), 539548 bytes in use
[7066] 09 Aug 20:13:20 * Connecting to MASTER...

三: redis 主从复制特点:
(1)、master 可以拥有多个slave
(2)、多个slave 可以连接同一个master 外,还可以连接到其他slave
(3)、主从复制不会阻塞master,在同步数据时,master 可以继续处理client 请求
(4)、提高系统的伸缩性


来自 “ ITPUB博客 ” ,链接:http://blog.itpub.net/28624388/viewspace-1288682/,如需转载,请注明出处,否则将追究法律责任。

转载于:http://blog.itpub.net/28624388/viewspace-1288682/

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值