3、如何配置

  配置slave服务器很简单,只需要在slave的配置文件中加入如下配置

slaveof 192.168.1.1 6379 #指定master的ip和端口

  下面我们做一个实验来演示如何搭建一个主从环境:

1.# slaveof <masterip> <masterport> 
2.slaveof localhost 6379
 
  我们在一台机器上启动主库(端口6379),从库(端口6378)

  启动后主库控制台日志如下:

1.[root@localhost redis-2.2.12]# src/redis-server redis.conf   
2.[7064] 09 Aug 20:13:12 * Server started, Redis version 2.2.12  
3.[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.  
4.[7064] 09 Aug 20:13:12 * The server is now ready to accept connections on port 6379  
5.[7064] 09 Aug 20:13:13 - 0 clients connected (0 slaves), 539512 bytes in use  
6.[7064] 09 Aug 20:13:18 - 0 clients connected (0 slaves), 539512 bytes in use  
7.[7064] 09 Aug 20:13:20 - Accepted 127.0.0.1:37789  
8.[7064] 09 Aug 20:13:20 * Slave ask for synchronization  
9.[7064] 09 Aug 20:13:20 * Starting BGSAVE for SYNC  
10.[7064] 09 Aug 20:13:20 * Background saving started by pid 7067  
11.[7067] 09 Aug 20:13:20 * DB saved on disk  
12.[7064] 09 Aug 20:13:20 * Background saving terminated with success  
13.[7064] 09 Aug 20:13:20 * Synchronization with slave succeeded  
14.[7064] 09 Aug 20:13:23 - 0 clients connected (1 slaves), 547380 bytes in use
 
  启动后从库控制台日志如下:

1.[root@localhost redis-2.2.12]# src/redis-server redis.slave   
2.[7066] 09 Aug 20:13:20 * Server started, Redis version 2.2.12  
3.[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.  
4.[7066] 09 Aug 20:13:20 * The server is now ready to accept connections on port 6378  
5.[7066] 09 Aug 20:13:20 - 0 clients connected (0 slaves), 539548 bytes in use  
6.[7066] 09 Aug 20:13:20 * Connecting to MASTER...  
7.[7066] 09 Aug 20:13:20 * MASTER <-> SLAVE sync started: SYNC sent  
8.[7066] 09 Aug 20:13:20 * MASTER <-> SLAVE sync: receiving 10 bytes from master  
9.[7066] 09 Aug 20:13:20 * MASTER <-> SLAVE sync: Loading DB in memory  
10.[7066] 09 Aug 20:13:20 * MASTER <-> SLAVE sync: Finished with success  
11.[7068] 09 Aug 20:13:20 * SYNC append only file rewrite performed  
12.[7066] 09 Aug 20:13:20 * Background append only file rewriting started by pid 7068  
13.[7066] 09 Aug 20:13:21 * Background append only file rewriting terminated with success  
14.[7066] 09 Aug 20:13:21 * Parent diff flushed into the new append log file with success (0 bytes)  
15.[7066] 09 Aug 20:13:21 * Append only file successfully rewritten.  
16.[7066] 09 Aug 20:13:21 * The new append only file was selected for future appends.  
17.[7066] 09 Aug 20:13:25 - 1 clients connected (0 slaves), 547396 bytes in use
 
  我们在主库上设置一对键值对

1.redis 127.0.0.1:6379> set name HongWan  
2.OK  
3.redis 127.0.0.1:6379>
 
  在从库上取一下这个键

1.redis 127.0.0.1:6378> get name  
2."HongWan"  
3.redis 127.0.0.1:6378>
 
  说明主从是同步正常的。

  那么我们如何判断哪个是主哪个是从呢?我们只需调用info这个命令可以得到主从的信息了,我们在从库上执行info命令

1.redis 127.0.0.1:6378> info  
2..  
3..  
4..  
5.role:slave  
6.master_host:localhost  
7.master_port:6379  
8.master_link_status:up  
9.master_last_io_seconds_ago:10  
10.master_sync_in_progress:0  
11.db0:keys=1,expires=0 
12.redis 127.0.0.1:6378>
 
  里面有一个角色标识,来判断是主库还是从库,对于本例是一个从库,同时还有一个master_link_status用于标明主从是否异步,如果此值=up,说明同步正常;如果此值=down,说明同步异步;
  db0:keys=1,expires=0,用于说明数据库有几个key,以及过期key的数量。