注意:一定要进入700X目录中启动,因为它会自动在当前目录中创建cluster-config,即nodes.conf。因为你在哪个目录下执行这个命令,则node.conf文件就会在该目录下创建。
但是若是在配置文件中配置dir为其他目录,则该文件会创建在dir指定目录下
或者把redis-server程序文件依次拷贝到每个700x目录下,然后使用不同目录的redis-server程序文件启动
关闭脚本:
# vim /usr/local/src/redis-5.0.4/cluster/shutdown_cluster.sh #!/bin/bash pgrep redis-server | xargs -exec kill -9 # chmod +x shutdown_cluster.sh执行启动脚本批量启动:
# ./start_cluster.sh 5.7.4 执行创建集群命令进入到redis源码存放目录/usr/local/src/redis-5.0.4/src下,把redis-cli文件拷贝到/usr/local/bin/目录下
cd /usr/local/src/redis-5.0.4/cluster /usr/local/src/redis-5.0.4/src/redis-cli --cluster create 127.0.0.1:7001 127.0.0.1:7002 127.0.0.1:7003 127.0.0.1:7004 127.0.0.1:7005 127.0.0.1:7006 --cluster-replicas 1 # --cluster-replicas 1 表示主从配置比,1表示的是1:1,前三个是主,后三个是从 # 若配置文件中设置的密码,则还需要加上-a passwod ...... Can I set the above configuration? (type \'yes\' to accept): yes ...... [OK] All nodes agree about slots configuration. >>> Check for open slots... >>> Check slots coverage... [OK] All 16384 slots covered. 5.7.5 查询集群信息注意:查询集群信息需要加上-c参数
或者直接执行命令查询,如下面所示;或者登陆到其中一个集群节点,使用命令cluster nodes进行查询
# /usr/local/src/redis-5.0.4/src/redis-cli -c -h 127.0.0.1 -p 7001 cluster nodes 7d388cc31df969d3e1715ce9644318bfd48317b1 127.0.0.1:7004@17004 slave 59b6597448b668a355d85dcc7a0623bc36263e5f 0 1564918712465 4 connected bbe8b7035bfd31c47bec7d612acc112cd2869368 127.0.0.1:7003@17003 master - 0 1564918714483 3 connected 10923-16383 456921ae96af71d8183101f798cf5ceda4b0381e 127.0.0.1:7005@17005 slave bbe8b7035bfd31c47bec7d612acc112cd2869368 0 1564918713000 5 connected 5612ffbb0407dbda50828b505a16b39ede51168b 127.0.0.1:7006@17006 slave 4dad696ede24995a57c5fd790faa95c72c187a22 0 1564918713474 6 connected 4dad696ede24995a57c5fd790faa95c72c187a22 127.0.0.1:7001@17001 myself,master - 0 1564918713000 1 connected 0-5460 59b6597448b668a355d85dcc7a0623bc36263e5f 127.0.0.1:7002@17002 master - 0 1564918712000 2 connected 5461-10922参数说明:
-c:表示以集群方式连接惹redis
-h:指定IP地址
-p:指定端口
cluster nodes:查询集群节点信息
cluster info:查询集群状态信息
其他查询方法:
# /usr/local/src/redis-5.0.4/src/redis-cli --cluster check 127.0.0.1:7001 127.0.0.1:7001 (4dad696e...) -> 0 keys | 5461 slots | 1 slaves. 127.0.0.1:7003 (bbe8b703...) -> 0 keys | 5461 slots | 1 slaves. 127.0.0.1:7002 (59b65974...) -> 0 keys | 5462 slots | 1 slaves. [OK] 0 keys in 3 masters. 0.00 keys per slot on average. >>> Performing Cluster Check (using node 127.0.0.1:7001) M: 4dad696ede24995a57c5fd790faa95c72c187a22 127.0.0.1:7001 slots:[0-5460] (5461 slots) master 1 additional replica(s) S: 7d388cc31df969d3e1715ce9644318bfd48317b1 127.0.0.1:7004 slots: (0 slots) slave replicates 59b6597448b668a355d85dcc7a0623bc36263e5f M: bbe8b7035bfd31c47bec7d612acc112cd2869368 127.0.0.1:7003 slots:[10923-16383] (5461 slots) master 1 additional replica(s) S: 456921ae96af71d8183101f798cf5ceda4b0381e 127.0.0.1:7005 slots: (0 slots) slave replicates bbe8b7035bfd31c47bec7d612acc112cd2869368 S: 5612ffbb0407dbda50828b505a16b39ede51168b 127.0.0.1:7006 slots: (0 slots) slave replicates 4dad696ede24995a57c5fd790faa95c72c187a22 M: 59b6597448b668a355d85dcc7a0623bc36263e5f 127.0.0.1:7002 slots:[5461-10922] (5462 slots) master 1 additional replica(s) [OK] All nodes agree about slots configuration. >>> Check for open slots... >>> Check slots coverage... [OK] All 16384 slots covered. # /usr/local/src/redis-5.0.4/src/redis-cli --cluster info 127.0.0.1:7001 127.0.0.1:7001 (4dad696e...) -> 0 keys | 5461 slots | 1 slaves. 127.0.0.1:7003 (bbe8b703...) -> 0 keys | 5461 slots | 1 slaves. 127.0.0.1:7002 (59b65974...) -> 0 keys | 5462 slots | 1 slaves. [OK] 0 keys in 3 masters. 0.00 keys per slot on average. 5.7.6 redis cluster集群重新启动因为集群已经创建过了,若是关机等原因关闭了redis服务,则只需要执行批量启动脚本即可,就会自动开启集群,因为有node.conf文件的存在
5.7.7 redis-cli集群命令帮助 # /usr/local/src/redis-5.0.4/src/redis-cli --cluster help Cluster Manager Commands: create host1:port1 ... hostN:portN --cluster-replicas <arg> check host:port --cluster-search-multiple-owners info host:port fix host:port --cluster-search-multiple-owners reshard host:port --cluster-from <arg> --cluster-to <arg> --cluster-slots <arg> --cluster-yes --cluster-timeout <arg> --cluster-pipeline <arg> --cluster-replace rebalance host:port --cluster-weight <node1=w1...nodeN=wN> --cluster-use-empty-masters --cluster-timeout <arg> --cluster-simulate --cluster-pipeline <arg> --cluster-threshold <arg> --cluster-replace add-node new_host:new_port existing_host:existing_port --cluster-slave --cluster-master-id <arg> del-node host:port node_id call host:port command arg arg .. arg set-timeout host:port milliseconds import host:port --cluster-from <arg> --cluster-copy --cluster-replace help For check, fix, reshard, del-node, set-timeout you can specify the host and port of any working node in the cluster. 5.8 集群管理 5.8.1 添加新主节点 redis-cli --cluster add-node new_host:new_port existing_host:existing_port --cluster-master-id node_id 5.8.2 hash槽重新分配