日期:2014-05-16  浏览次数:20469 次

8: redis的主从复制

1: redis主从复制

?

redis的主从复制相对来说很简单,一台机子上开两个reids服务

?

1: 主redis.conf ?2:copy一份 位 redis_slave.conf

?

redis.conf配置:

bind 127.0.0.1

logfile /tmp/redis.log

?

从: redis_slave.conf

pidfile /var/run/redis_slave.pid

bind 127.0.0.1

logfile /tmp/redis_slave.log

dbfilename dump_slave.rdb

slaveof 127.0.0.1  6379

?

运行:ubuntu需要root权限运行, 不然没有权限生成快照

ljq@ubuntu:/usr/local/redis$sudo ./redis-server /etc/redis.conf
ljq@ubuntu:/usr/local/redis$ sudo ./redis-server /etc/redis_slave.conf

?

查看是否成功

tail -f /tmp/redis.log

?

内容:

[4508] 21 Sep 06:34:01.666 # Server started, Redis version 2.6.16
[4508] 21 Sep 06:34:01.666 # 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.
[4508] 21 Sep 06:34:01.666 * The server is now ready to accept connections on port 6379
[4508] 21 Sep 06:34:07.690 * Slave ask for synchronization
[4508] 21 Sep 06:34:07.690 * Starting BGSAVE for SYNC
[4508] 21 Sep 06:34:07.690 * Background saving started by pid 4516
[4516] 21 Sep 06:34:07.779 * DB saved on disk
[4516] 21 Sep 06:34:07.780 * RDB: 0 MB of memory used by copy-on-write
[4508] 21 Sep 06:34:07.816 * Background saving terminated with success
[4508] 21 Sep 06:34:07.816 * Synchronization with slave succeeded

?

测试主从复制:

ljq@ubuntu:/usr/local/redis$ sudo ./redis-cli -p 6379
redis 127.0.0.1:6379> keys *
(empty list or set)
redis 127.0.0.1:6379> set myset a b
(error) ERR syntax error
redis 127.0.0.1:6379> set myset a
OK
redis 127.0.0.1:6379> get myset 
"a"

?

ljq@ubuntu:/usr/local/redis$ sudo ./redis-cli -p 6378
[sudo] password for ljq: 
redis 127.0.0.1:6378> keys *
1) "myset"
redis 127.0.0.1:6378> get myset 
"a"

?

这个是挤压rdp快照的主从,对主写,从读,但是很大程度上会弄的主IO颈瓶,这样以来主实例在持久化数据到硬盘的过程中,势必会造成磁盘的I/O等待,经过实际测试,这个持久化写硬盘的过程给应用程序带来的影响无法忍受;因而在大多数场景下,会考虑把持久化配置在从实例上,当主实例宕机后,通过手动或者自动的方式将从实例提升为主实例,继续提供服务?

1 楼 iluoxuan 2013-09-21  
挤出点时间更新啦