1、注意
[root@master bin]# ./redis-cli --cluster create 192.168.xxx.21:7001 192.168.xxx.21:7002 192.168.xxx.21:7003 192.168.xxx.21:8001 192.168.xxx.21:8002 192.168.xxx.21:8003 --cluster-replicas 1 -a 123456
上面的命令只能在新创健集群的时候执行一次,目的是为了建立内部各个节点的对应关系,比如主从关系,这些关系仅且只能在一个集群中初始化时对应一次;
如果再此执行,则会出现如下错误:
[root@master bin]# ./redis-cli --cluster create 192.168.xxx.21:7001 192.168.xxx.21:7002 192.168.xxx.21:7003 192.168.xxx.21:8001 192.168.xxx.21:8002 192.168.xxx.21:8003 --cluster-replicas 1 -a 123456
Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe.
[ERR] Node 192.168.xxx.21:7001 is not empty. Either the node already knows other nodes (check with CLUSTER NODES) or contains some key in database 0.
2、集群关闭
集群关闭直接将各个节点的进程kill掉即可;
[root@master bin]# ps -ef | grep redis
root 11516 1 0 16:15 ? 00:00:10 ./redis-server 192.168.xxx.21:7002 [cluster]
root 11521 1 0 16:15 ? 00:00:09 ./redis-server 192.168.xxx.21:7003 [cluster]
root 11526 1 0 16:15 ? 00:00:10 ./redis-server 192.168.xxx.21:8001 [cluster]
root 11531 1 0 16:15 ? 00:00:10 ./redis-server 192.168.xxx.21:8002 [cluster]
root 11536 1 0 16:15 ? 00:00:10 ./redis-server 192.168.xxx.21:8003 [cluster]
root 11869 1 0 16:33 ? 00:00:07 ./redis-server 192.168.xxx.21:7001 [cluster]
root 12528 9737 0 17:39 pts/7 00:00:00 grep --color=auto redis
[root@master bin]# kill -9 11516
[root@master bin]# kill -9 11521
[root@master bin]# kill -9 11526
[root@master bin]# kill -9 11531
[root@master bin]# kill -9 11536
[root@master bin]# kill -9 11869
[root@master bin]# ps -ef | grep redis
root 12552 9737 0 17:40 pts/7 00:00:00 grep --color=auto redis
3、集群开启及连接
(1)集群开启
[root@master bin]# ./redis-server /opt/softWare/redis-cluster/redis01/redis.conf
12553:C 31 Mar 2020 17:40:59.875 # oO0OoO0OoO0Oo Redis is starting oO0OoO0OoO0Oo
12553:C 31 Mar 2020 17:40:59.875 # Redis version=5.0.5, bits=64, commit=00000000, modified=0, pid=12553, just started
12553:C 31 Mar 2020 17:40:59.875 # Configuration loaded
[root@master bin]# ./redis-server /opt/softWare/redis-cluster/redis02/redis.conf
12558:C 31 Mar 2020 17:41:03.697 # oO0OoO0OoO0Oo Redis is starting oO0OoO0OoO0Oo
12558:C 31 Mar 2020 17:41:03.697 # Redis version=5.0.5, bits=64, commit=00000000, modified=0, pid=12558, just started
12558:C 31 Mar 2020 17:41:03.697 # Configuration loaded
[root@master bin]# ./redis-server /opt/softWare/redis-cluster/redis03/redis.conf
12563:C 31 Mar 2020 17:41:06.702 # oO0OoO0OoO0Oo Redis is starting oO0OoO0OoO0Oo
12563:C 31 Mar 2020 17:41:06.702 # Redis version=5.0.5, bits=64, commit=00000000, modified=0, pid=12563, just started
12563:C 31 Mar 2020 17:41:06.702 # Configuration loaded
[root@master bin]# ./redis-server /opt/softWare/redis-cluster/redis04/redis.conf
12568:C 31 Mar 2020 17:41:09.742 # oO0OoO0OoO0Oo Redis is starting oO0OoO0OoO0Oo
12568:C 31 Mar 2020 17:41:09.742 # Redis version=5.0.5, bits=64, commit=00000000, modified=0, pid=12568, just started
12568:C 31 Mar 2020 17:41:09.742 # Configuration loaded
[root@master bin]# ./redis-server /opt/softWare/redis-cluster/redis05/redis.conf
12574:C 31 Mar 2020 17:41:12.760 # oO0OoO0OoO0Oo Redis is starting oO0OoO0OoO0Oo
12574:C 31 Mar 2020 17:41:12.760 # Redis version=5.0.5, bits=64, commit=00000000, modified=0, pid=12574, just started
12574:C 31 Mar 2020 17:41:12.760 # Configuration loaded
[root@master bin]# ./redis-server /opt/softWare/redis-cluster/redis06/redis.conf
12580:C 31 Mar 2020 17:41:16.406 # oO0OoO0OoO0Oo Redis is starting oO0OoO0OoO0Oo
12580:C 31 Mar 2020 17:41:16.406 # Redis version=5.0.5, bits=64, commit=00000000, modified=0, pid=12580, just started
12580:C 31 Mar 2020 17:41:16.406 # Configuration loaded
[root@master bin]# ps -ef | grep redis
root 12554 1 0 17:40 ? 00:00:00 ./redis-server 192.168.xxx.21:7001 [cluster]
root 12559 1 0 17:41 ? 00:00:00 ./redis-server 192.168.xxx.21:7002 [cluster]
root 12564 1 0 17:41 ? 00:00:00 ./redis-server 192.168.xxx.21:7003 [cluster]
root 12569 1 0 17:41 ? 00:00:00 ./redis-server 192.168.xxx.21:8001 [cluster]
root 12575 1 0 17:41 ? 00:00:00 ./redis-server 192.168.xxx.21:8002 [cluster]
root 12581 1 0 17:41 ? 00:00:00 ./redis-server 192.168.xxx.21:8003 [cluster]
root 12587 9737 0 17:41 pts/7 00:00:00 grep --color=auto redis
(2)集群连接
[root@master bin]# ./redis-cli -p 7001 -a 123456 -h 192.168.xxx.21 -a 123456 -c
Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe.
192.168.xxx.21:7001> DBSIZE
(integer) 2
192.168.xxx.21:7001> keys *
1) "aa"
2) "ss"
192.168.xxx.21:7001> set str STR
-> Redirected to slot [6928] located at 192.168.xxx.21:7002
OK
192.168.xxx.21:7002>