1 master/slave server 启动
===========================
利用默认redis.conf配置文件启动master server,端口6379 。
[7304] 29 Aug 09:57:26 - 0 clients connected (0 slaves), 673996 bytes in use
[7304] 29 Aug 09:57:31 - DB 0: 15 keys (0 volatile) in 16 slots HT.
[7304] 29 Aug 09:57:31 - 0 clients connected (0 slaves), 673996 bytes in use
[7304] 29 Aug 09:57:36 - DB 0: 15 keys (0 volatile) in 16 slots HT.
[7304] 29 Aug 09:57:36 - 0 clients connected (0 slaves), 673996 bytes in use
[7304] 29 Aug 09:57:41 - DB 0: 15 keys (0 volatile) in 16 slots HT.
修改redis配置文件, 主要修改端口为6380,不是默认的6379, 然后修改slaveof字段,设置主master为127.0.0.7 6379
启动后如下
[1704] 29 Aug 10:03:16 - 1 clients connected (0 slaves), 681872 bytes in use
[1704] 29 Aug 10:03:21 - DB 0: 15 keys (0 volatile) in 16 slots HT.
[1704] 29 Aug 10:03:21 - 1 clients connected (0 slaves), 681872 bytes in use
[1704] 29 Aug 10:03:26 - DB 0: 15 keys (0 volatile) in 16 slots HT.
[1704] 29 Aug 10:03:26 - 1 clients connected (0 slaves), 681872 bytes in use
[1704] 29 Aug 10:03:31 - DB 0: 15 keys (0 volatile) in 16 slots HT.
[1704] 29 Aug 10:03:31 - 1 clients connected (0 slaves), 681872 bytes in use
[1704] 29 Aug 10:03:36 - DB 0: 15 keys (0 volatile) in 16 slots HT.
[1704] 29 Aug 10:03:36 - 1 clients connected (0 slaves), 681872 bytes in use
[1704] 29 Aug 10:03:41 - DB 0: 15 keys (0 volatile) in 16 slots HT.
[1704] 29 Aug 10:03:41 - 1 clients connected (0 slaves), 681872 bytes in use
[1704] 29 Aug 10:03:46 - DB 0: 15 keys (0 volatile) in 16 slots HT.
[1704] 29 Aug 10:03:46 - 1 clients connected (0 slaves), 681872 bytes in use
[1704] 29 Aug 10:03:51 - DB 0: 15 keys (0 volatile) in 16 slots HT.
[1704] 29 Aug 10:03:51 - 1 clients connected (0 slaves), 681872 bytes in use
[1704] 29 Aug 10:03:56 - DB 0: 15 keys (0 volatile) in 16 slots HT.
[1704] 29 Aug 10:03:56 - 1 clients connected (0 slaves), 681872 bytes in use
[1704] 29 Aug 10:04:01 - DB 0: 15 keys (0 volatile) in 16 slots HT.
[1704] 29 Aug 10:04:01 - 1 clients connected (0 slaves), 681872 bytes in use
主master已经检测到有一个slave连接
[7304] 29 Aug 09:58:31 - 0 clients connected (1 slaves), 681960 bytes in use
[7304] 29 Aug 09:58:36 - DB 0: 15 keys (0 volatile) in 16 slots HT.
[7304] 29 Aug 09:58:36 - 0 clients connected (1 slaves), 681960 bytes in use
[7304] 29 Aug 09:58:41 - DB 0: 15 keys (0 volatile) in 16 slots HT.
[7304] 29 Aug 09:58:41 - 0 clients connected (1 slaves), 681960 bytes in use
[7304] 29 Aug 09:58:46 - DB 0: 15 keys (0 volatile) in 16 slots HT.
[7304] 29 Aug 09:58:46 - 0 clients connected (1 slaves), 681960 bytes in use
[7304] 29 Aug 09:58:51 - DB 0: 15 keys (0 volatile) in 16 slots HT.
[7304] 29 Aug 09:58:51 - 0 clients connected (1 slaves), 681960 bytes in use
[7304] 29 Aug 09:58:56 - DB 0: 15 keys (0 volatile) in 16 slots HT.
[7304] 29 Aug 09:58:56 - 0 clients connected (1 slaves), 681960 bytes in use
[7304] 29 Aug 09:59:01 - DB 0: 15 keys (0 volatile) in 16 slots HT.
[7304] 29 Aug 09:59:01 - 0 clients connected (1 slaves), 681960 bytes in use
[7304] 29 Aug 09:59:06 - DB 0: 15 keys (0 volatile) in 16 slots HT.
[7304] 29 Aug 09:59:06 - 0 clients connected (1 slaves), 681960 bytes in use
客户端如果连接到slave,则slave如下显示:
[1704] 29 Aug 10:05:52 - 2 clients connected (0 slaves), 689700 bytes in use
[1704] 29 Aug 10:05:57 - DB 0: 15 keys (0 volatile) in 16 slots HT.
[1704] 29 Aug 10:05:57 - 2 clients connected (0 slaves), 689700 bytes in use
[1704] 29 Aug 10:06:02 - DB 0: 15 keys (0 volatile) in 16 slots HT.
[1704] 29 Aug 10:06:02 - 2 clients connected (0 slaves), 689700 bytes in use
如果连接到master,则结果如下
[7304] 29 Aug 10:06:53 - Accepted 127.0.0.1:65311
[7304] 29 Aug 10:06:57 - DB 0: 15 keys (0 volatile) in 16 slots HT.
[7304] 29 Aug 10:06:57 - 1 clients connected (1 slaves), 689788 bytes in use
[7304] 29 Aug 10:07:02 - DB 0: 15 keys (0 volatile) in 16 slots HT.
[7304] 29 Aug 10:07:02 - 1 clients connected (1 slaves), 689788 bytes in use
2 Replication测试
在master存贮一个值,如下
redis 127.0.0.1:6379> SET company alu
OK
redis 127.0.0.1:6379> GET company alu
(error) ERR wrong number of arguments for 'get' command
redis 127.0.0.1:6379> GET company
"alu"
redis 127.0.0.1:6379>
连接到slave查询,已经自动复制。如下
redis 127.0.0.1:6380> GET company
"alu"
redis 127.0.0.1:6380> SET company asb
OK
redis 127.0.0.1:6380>
slave我这里默认是读写都可以的,一般应该默认是只读的,这个可以再配置文件修改!
读写分离,类似于mysql cluster提供的读写分离,机制是一样的。