Redis 集群命令用来管理 Redis 集群。
该实例将当前 Redis 服务添加到集群中,当做 Slave 角色运行,然后运行 role 命令查看节点信息。如下:
127.0.0.1:6379> slaveof 127.0.0.1 6378 OK 127.0.0.1:6379> role 1) "slave" 2) "127.0.0.1" 3) (integer) 6378 4) "connected" 5) (integer) 904
下面列出了 redis 集群相关命令。
返回主从实例所属的角色。语法如下:
ROLE
实例:
127.0.0.1:6379> role 1) "slave" 2) "127.0.0.1" 3) (integer) 6378 4) "connected" 5) (integer) 84 127.0.0.1:6378> role 1) "master" 2) (integer) 182 3) 1) 1) "127.0.0.1" 2) "6379" 3) "182"
获取集群节点的映射数组。语法如下:
CLUSTER SLOTS
实例:
127.0.0.1:6379> CLUSTER SLOTS 1) 1) (integer) 0 2) (integer) 5460 3) 1) "127.0.0.1" 2) (integer) 30001 3) "09dbe9720cda62f7865eabc5fd8857c5d2678366" 4) 1) hostname 2) "host-1.redis.example.com" 4) 1) "127.0.0.1" 2) (integer) 30004 3) "821d8ca00d7ccf931ed3ffc7e3db0599d2271abf" 4) 1) hostname 2) "host-2.redis.example.com" 2) 1) (integer) 5461 2) (integer) 10922 3) 1) "127.0.0.1" 2) (integer) 30002 3) "c9d93d9f2c0c524ff34cc11838c2003d8c29e013" 4) 1) hostname 2) "host-3.redis.example.com" 4) 1) "127.0.0.1" 2) (integer) 30005 3) "faadb3eb99009de4ab72ad6b6ed87634c7ee410f" 4) 1) hostname 2) "host-4.redis.example.com" 3) 1) (integer) 10923 2) (integer) 16383 3) 1) "127.0.0.1" 2) (integer) 30003 3) "044ec91f325b7595e76dbcb18cc688b6a5b434a1" 4) 1) hostname 2) "host-5.redis.example.com" 4) 1) "127.0.0.1" 2) (integer) 30006 3) "58e6e48d41228013e5d9c1c37c5060693925e97e" 4) 1) hostname 2) "host-6.redis.example.com"
将当前服务器转变为指定服务器的从属服务器(slave server)。语法如下:
SLAVEOF host port
实例:
127.0.0.1:6379> slaveof 127.0.0.1 6378 OK
上面成功执行了 slaveof 命令,此时查看 Redis 的控制台,输出如下日志:
[10048] 27 Feb 13:00:50.606 * Before turning into a replica, using my master parameters to synthesize a cached master: I may be able to synchronize with the new master with just a partial transfer. [10048] 27 Feb 13:00:50.606 * REPLICAOF 127.0.0.1:6378 enabled (user request from 'id=4 addr=127.0.0.1:29091 fd=10 name= age=71 idle=0 flags=N db=0 sub=0 psub=0 multi=-1 qbuf=42 qbuf-free=32726 obl=0 oll=0 omem=0 events=r cmd=slaveof') [10048] 27 Feb 13:00:51.294 * Connecting to MASTER 127.0.0.1:6378 [10048] 27 Feb 13:00:51.294 * MASTER <-> REPLICA sync started [10048] 27 Feb 13:00:51.294 * Non blocking connect for SYNC fired the event. [10048] 27 Feb 13:00:51.294 * Master replied to PING, replication can continue... [10048] 27 Feb 13:00:51.294 * Trying a partial resynchronization (request 5bc281fbd0510b96f4a836daa8053b372dde6616:113). [10048] 27 Feb 13:00:51.309 * Full resync from master: e524ecd09eaca2a0676c03ab39480ba635aa6cdc:476 [10048] 27 Feb 13:00:51.309 * Discarding previously cached master state. ...
上面输出的日志表示该 Redis 服务成功加入到了 Redis 集群,当做 slave 角色。
用于复制功能(replication)的内部命令。语法如下:
SYNC
实例:
127.0.0.1:6378> sync Entering replica output mode... (press Ctrl-C to quit) SYNC with master, discarding 179 bytes of bulk transfer... SYNC done. Logging commands from master. "PING"