redis基本操作和集群搭建

NoSQL概念

NoSQL:not only sql
特点:
1、不支持SQL语法
2、nosql中存储的数据都是KV形式
3、每种nosql数据库都有自己的api和语法,以及各自擅长的业务场景
常用的nosql:Mongodb、Redis
和SQL数据库区别:
1、适用场景不同:sql数据库适合用于关系特别复杂的数据查询场景,nosql相反
2、sql对事物的支持非常完善,而nosql基本不支持事物

redis特点

1、支持数据持久化,可以将内存中的数据保存在磁盘中,重启后可以再次加载使用
2、不仅支持简单的key-value类型,还提供list、set、zset、hash等数据结构的存储
3、支持master-slave模式的数据备份

redis优势

1、性能极高,读速度110000次/s,写速度81000次/s
2、丰富的数据类型,支持Strings、Lists、Hashes、Sets及Ordered Sets等数据类型
3、redis所有操作都是原子性的,同时redis还支持对几个操作合并后的原子性执行
4、丰富的特性,redis支持publish/subscribe、通知、key过期等

string类型

redis中的字符串除了可以存普通字符串,也可以是二进制的数据,最大容纳512M

string基本操作

1、设置键,不存在则添加,存在则修改
set name tom
2、获取某个键
get name
3、设置键值过期时间,单位是秒
setex name 10 tom
4、设置多个键值
mset name tom age 12 sex male
5、向某个键对应的值中追加,没有该键会新增
append name hello
6、获取多个键值
mget name age sex

通用键命令

1、查找键,支持正则,kes 正则,查看所有键
keys *
2、查看名称中包含a的键
keys 'a*'
3、判断键是否存在,存在返回1,不存在返回0
exists name
4、查看键对应的值的类型
type name
5、删除键值对,支持多个删除
del name age sex
6、如果没有指定过期时间,一直存在,直到使用del删除
expire name 10
7、查看键的有效时间,返回剩余时间,单位是秒
ttl key

hash类型

hash用于存储对象,对象的结构为key-value
1、设置某个键单个属性
hset user name tom
2、设置某个键多个属性
hset user2 name tom age 12
3、获取指定键的所有属性
hkeys user2
4、获取某个键的某个属性值
hget user2 name
5、获取某个键的多个属性值
hmget user2 name age
6、获取某个键的所有值
hvals user2
7、删除整个hash键和属性
del user
8、删除某个键的某个属性
hdel user name

list类型

列表的元素类型为string
按照插入顺序排序
1、左侧插入数据
lpush list1 a b c
2、显示列表值,包含开始和结束下标,0 -1表示所有
lrange list1 0 2
3、右侧插入数据
rpush list1 0 1
4、指定元素前面/后面插入元素
linsert list1 before a 100
5、设置指定位置的值,下标从0开始,支持负数,-1表示最后一个元素
lset list1 3 hello
6、删除元素,如下例子,下标为0删除所有a元素,-2从尾到头删除2个a,2从头到尾删除2个a
lrem list1 -2 a

set类型

无序集合,元素是string类型,元素唯一不重复
tip:没有修改操作
1、新增元素
sadd s1 tom lily jerry
2、获取某个集合所有元素,注意返回的数据是无序的
smembers s1
3、删除集合指定元素,可以删多个元素
srem s1 tom lily

zset类型

有序集合,元素是string类型,元素唯一不重复。每个元素都会关联一个double类型的score表示权重,通过权重将元素从小到大排序。
tip:没有修改操作
1、新增元素,每个元素之前定义权重,tom权重是4
zadd z1 4 tom 5 jerry 2 lily
2、获取元素,返回的元素按照权重从小到大排序
zrange z1 0 -1
3、获取权重区间的元素,包含区间开始和结束值
zrangebyscore z1 4 5
4、获取集合中某个元素的权重
zscore z1 tom
5、删除集合中的指定元素
zrem z1 tom
6、删除权重区间的元素,包含区间开始和结束值
zremrangebyscore z1 4 5

发布订阅模式

客户端不需要主动获取消息,只需要订阅频道,这个频道的内容就会被推送过来

#订阅消息后,会一直处于等待状态,有点像长链接
C:\Users\zh>redis-cli
127.0.0.1:6379> SUBSCRIBE py01 py02
Reading messages... (press Ctrl-C to quit)
1) "subscribe"
2) "py01"
3) (integer) 1
1) "subscribe"
2) "py02"
3) (integer) 2
1) "message"
2) "py01"
3) "hello"
1) "message"
2) "py02"
3) "hello2"
#发布消息
C:\Users\zh>redis-cli
127.0.0.1:6379> publish py01 hello
(integer) 2
127.0.0.1:6379> publish py02 hello2
(integer) 1

redis主从

  • 概念

一个主服务可以有多个从服务,一个从服务也可以拥有多个从服务,形成多级服务器集群
作用:
1、主服务用来写数据,从服务用来读数据,通过主从配置实现读写分离
2、从服务备份主服务的数据,防止数据丢失

#主服务
C:\Users\zh>redis-server -p 6379
C:\Users\zh>redis-cli -p 6379
127.0.0.1:6379> set name 'liliya'
OK
127.0.0.1:6379> get name
"liliya"
#从服务
C:\Users\zh>redis-cli -p 6378
127.0.0.1:6378> get name
"liliya"
127.0.0.1:6378> set name 'tom'
(error) READONLY You can't write against a read only replica.
  • 主从配置,没有写权限

#windows下redis服务命令:
1、使用指定路径的配置文件安装service服务
    redis-server --service-install "C:\Program Files\Redis-slave\redis.windows-service.conf" --service-name redis6379
2、启动service服务
   redis-server --service-start --service-name redis6379
3、停止命令
   redis-server --service-stop --service-name redis6379
4、卸载命令
  redis-server --service-uninstall --service-name redis6379

复制下载的Redis安装包,重命名为Redis-slave。
修改主服务目录下的redis.windows-service.conf文件,端口配置为6379
修改从服务目录下的redis.windows-service.conf文件,端口配置为6378。查看注释的配置#slaveof ,这里设置为slaveof 127.0.0.1 6379标明主服务的ip和端口

windows系统
#注册主服务
redis-server --service-install "C:\Program Files\Redis\redis.windows-service.conf" --service-name redis6379_master
#启动主服务
redis-server --service-start --service-name redis6379_master
#注册从服务
redis-server --service-install "C:\Program Files\Redis-slave\redis.windows-service.conf" --service-name redis6378_slave
#启动从服务
redis-server --service-start --service-name redis6378_slave

查看主从服务关系:

C:\Users\zh>redis-cli -h 127.0.0.1 -p 6379 info Replication
# Replication
role:master
connected_slaves:1
slave0:ip=127.0.0.1,port=6378,state=online,offset=294,lag=0
master_replid:7d664d6001925ddcb55c7c46cbfee9431d262e33
master_replid2:0000000000000000000000000000000000000000
master_repl_offset:294
second_repl_offset:-1
repl_backlog_active:1
repl_backlog_size:1048576
repl_backlog_first_byte_offset:1
repl_backlog_histlen:294

C:\Users\zh>redis-cli -h 127.0.0.1 -p 6378 info Replication
# Replication
role:slave
master_host:127.0.0.1
master_port:6379
master_link_status:up
master_last_io_seconds_ago:3
master_sync_in_progress:0
slave_repl_offset:294
slave_priority:100
slave_read_only:1
connected_slaves:0
master_replid:7d664d6001925ddcb55c7c46cbfee9431d262e33
master_replid2:0000000000000000000000000000000000000000
master_repl_offset:294
second_repl_offset:-1
repl_backlog_active:1
repl_backlog_size:1048576
repl_backlog_first_byte_offset:1
repl_backlog_histlen:294

redis集群

  • 集群概念
    一组通过网络连接的计算机,共同对外提供服务,在外面看来就像一台独立的服务器。提升了负载能力。
  • redis集群分类
    软件层面:只有一台电脑,这台电脑上启动了多个redis服务
    硬件层面:存在多台实体电脑,每台电脑上都启动一个或多个redis服务
  • 数据存储
    redis集群采用hash slot的方式分配数据,redis集群默认分配16384slot,这些槽会平均分配到集群中的主节点。设置key-value值时,根据CRC16算法得到对应的slot(CRC16(key)%16384),然后将key分配到slot对应的节点。
    redis集群把数据存储到一个master节点,然后在这个master对应的slave之间同步数据。读取数据时,也根据一致性哈希算法到对应的master节点获取数据。当一个master节点挂了,会自动对应的slave节点充当master
    tip:必须有3个或以上的主节点,否则无法创建集群。当存活的主节点数小于总节点数的一半时,整个集群就无法对外提供服务了

ubuntu下配置集群

  • 安装依赖环境:
sudo apt-get install gems
sudo gem install redis
sudo apt-get install ruby
  • 修改redis.conf文件
    /user/local/redis目录下新建6个文件夹,文件夹名称代表redis要开启的端口方便查看,拷贝/etc/redis/redis.conf到每个目录下。修改各自的配置文件,以7001端口为例:
port 7001 #端口
cluster-enabled yes #此redis实例作为集群的一个节点
cluster-node-timeout 15000 #节点能够失联的最大时间
cluster-config-file nodes-7001.conf #集群配置文件,系统自动维护,不能人工编辑,主要记录集群中有哪些节点,状态等参数
pidfile /var/run/redis_7001.pid #redis以守护进程方式运行时,系统默认会把pid写入/var/run/redis_7001.pid
  • 配置完成后,通过redis-server 配置文件路径 启动多个服务:
root@ubuntu:/usr/local/redis# redis-server ./7001/redis.conf
root@ubuntu:/usr/local/redis# redis-server ./7002/redis.conf
root@ubuntu:/usr/local/redis# redis-server ./7003/redis.conf
root@ubuntu:/usr/local/redis# redis-server ./7004/redis.conf
root@ubuntu:/usr/local/redis# redis-server ./7005/redis.conf
root@ubuntu:/usr/local/redis# redis-server ./7006/redis.conf
root@ubuntu:/usr/local/redis# ps -aux|grep redis
root       19683  0.1  0.1  55348  5076 ?        Ssl  23:06   0:00 redis-server 127.0.0.1:7001 [cluster]
root       19688  0.0  0.1  55348  5316 ?        Ssl  23:06   0:00 redis-server 127.0.0.1:7002 [cluster]
root       19693  0.1  0.1  55348  5280 ?        Ssl  23:06   0:00 redis-server 127.0.0.1:7003 [cluster]
root       19698  0.1  0.1  55348  5164 ?        Ssl  23:06   0:00 redis-server 127.0.0.1:7004 [cluster]
root       19703  0.1  0.1  55348  5232 ?        Ssl  23:06   0:00 redis-server 127.0.0.1:7005 [cluster]
root       19709  0.0  0.1  55348  5224 ?        Ssl  23:06   0:00 redis-server 127.0.0.1:7006 [cluster]
root       19714  0.0  0.0   9032   724 pts/0    S+   23:06   0:00 grep --color=auto redis

  • 创建集群
#--cluster-replicas 1 表示有一个主服务对应一个从服务
root@ubuntu:/usr/local/redis# 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
>>> Performing hash slots allocation on 6 nodes...
Master[0] -> Slots 0 - 5460
Master[1] -> Slots 5461 - 10922
Master[2] -> Slots 10923 - 16383
Adding replica 127.0.0.1:7005 to 127.0.0.1:7001
Adding replica 127.0.0.1:7006 to 127.0.0.1:7002
Adding replica 127.0.0.1:7004 to 127.0.0.1:7003
>>> Trying to optimize slaves allocation for anti-affinity
[WARNING] Some slaves are in the same host as their master
M: 9397f2a2bced9ca95cbf1e78b67df947e14c7d0c 127.0.0.1:7001
   slots:[0-5460] (5461 slots) master
M: e59d760a7e15ce1b69b531ff2d2278c7407f87c3 127.0.0.1:7002
   slots:[5461-10922] (5462 slots) master
M: 9e4710c7cada29b576efef71d3d81f56f1bd28db 127.0.0.1:7003
   slots:[10923-16383] (5461 slots) master
S: 52d4a72eb9338f9827a65f8bc02d672b23ede717 127.0.0.1:7004
   replicates 9397f2a2bced9ca95cbf1e78b67df947e14c7d0c
S: aed6634c23190f0cd6c956cfe5a7b34d66cde350 127.0.0.1:7005
   replicates e59d760a7e15ce1b69b531ff2d2278c7407f87c3
S: 2193f876372ba3a467792bee8305f195d203afcf 127.0.0.1:7006
   replicates 9e4710c7cada29b576efef71d3d81f56f1bd28db
Can I set the above configuration? (type 'yes' to accept): yes
>>> Nodes configuration updated
>>> Assign a different config epoch to each node
>>> Sending CLUSTER MEET messages to join the cluster
Waiting for the cluster to join
...
>>> Performing Cluster Check (using node 127.0.0.1:7001)
M: 9397f2a2bced9ca95cbf1e78b67df947e14c7d0c 127.0.0.1:7001
   slots:[0-5460] (5461 slots) master
   1 additional replica(s)
S: 2193f876372ba3a467792bee8305f195d203afcf 127.0.0.1:7006
   slots: (0 slots) slave
   replicates 9e4710c7cada29b576efef71d3d81f56f1bd28db
S: aed6634c23190f0cd6c956cfe5a7b34d66cde350 127.0.0.1:7005
   slots: (0 slots) slave
   replicates e59d760a7e15ce1b69b531ff2d2278c7407f87c3
M: 9e4710c7cada29b576efef71d3d81f56f1bd28db 127.0.0.1:7003
   slots:[10923-16383] (5461 slots) master
   1 additional replica(s)
S: 52d4a72eb9338f9827a65f8bc02d672b23ede717 127.0.0.1:7004
   slots: (0 slots) slave
   replicates 9397f2a2bced9ca95cbf1e78b67df947e14c7d0c
M: e59d760a7e15ce1b69b531ff2d2278c7407f87c3 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.


#查看节点信息
root@ubuntu:~# redis-cli -h 127.0.0.1 -p 7001  #可以连接任意7001~7006任意端口
127.0.0.1:7001> cluster nodes
2193f876372ba3a467792bee8305f195d203afcf 127.0.0.1:7006@17006 slave 9e4710c7cada29b576efef71d3d81f56f1bd28db 0 1602485550451 6 connected
aed6634c23190f0cd6c956cfe5a7b34d66cde350 127.0.0.1:7005@17005 slave e59d760a7e15ce1b69b531ff2d2278c7407f87c3 0 1602485549442 5 connected
9e4710c7cada29b576efef71d3d81f56f1bd28db 127.0.0.1:7003@17003 master - 0 1602485548437 3 connected 10923-16383
52d4a72eb9338f9827a65f8bc02d672b23ede717 127.0.0.1:7004@17004 slave 9397f2a2bced9ca95cbf1e78b67df947e14c7d0c 0 1602485547000 4 connected
9397f2a2bced9ca95cbf1e78b67df947e14c7d0c 127.0.0.1:7001@17001 myself,master - 0 1602485549000 1 connected 0-5460
e59d760a7e15ce1b69b531ff2d2278c7407f87c3 127.0.0.1:7002@17002 master - 0 1602485549000 2 connected 5461-10922
  • 添加节点
#添加节点7007,作为主节点7001的从节点
#创建7007文件夹,修改好配置文件
root@ubuntu:/usr/local/redis# ls
7001  7002  7003  7004  7005  7006  7007
#启动redis服务端口是7007
root@ubuntu:/usr/local/redis# redis-server ./7007/redis.conf 
#添加节点
root@ubuntu:/usr/local/redis# redis-cli --cluster add-node 127.0.0.1:7007 127.0.0.1:7001 --cluster-slave
...
#查看节点信息,7007已经是7001的从节点
127.0.0.1:7002> cluster nodes
9397f2a2bced9ca95cbf1e78b67df947e14c7d0c 127.0.0.1:7001@17001 master - 0 1602486904778 1 connected 0-5460
1584cce24eaefbed82372a61268885f6685419bc 127.0.0.1:7007@17007 slave 9397f2a2bced9ca95cbf1e78b67df947e14c7d0c 0 1602486903000 1 connected
aed6634c23190f0cd6c956cfe5a7b34d66cde350 127.0.0.1:7005@17005 slave e59d760a7e15ce1b69b531ff2d2278c7407f87c3 0 1602486901000 5 connected
9e4710c7cada29b576efef71d3d81f56f1bd28db 127.0.0.1:7003@17003 master - 0 1602486900000 3 connected 10923-16383
52d4a72eb9338f9827a65f8bc02d672b23ede717 127.0.0.1:7004@17004 slave 9397f2a2bced9ca95cbf1e78b67df947e14c7d0c 0 1602486902765 4 connected
2193f876372ba3a467792bee8305f195d203afcf 127.0.0.1:7006@17006 slave 9e4710c7cada29b576efef71d3d81f56f1bd28db 0 1602486903770 6 connected
e59d760a7e15ce1b69b531ff2d2278c7407f87c3 127.0.0.1:7002@17002 myself,master - 0 1602486902000 2 connected 5461-10922


#添加节点7008,作为主节点
#创建7008文件夹,修改好配置文件
root@ubuntu:/usr/local/redis# ls
7001  7002  7003  7004  7005  7006  7007  7008
#启动redis服务端口是7008
root@ubuntu:/usr/local/redis# redis-server ./7008/redis.conf
#添加节点,需要指定一个已存在的主节点和节点id,比如7001
root@ubuntu:/usr/local/redis# redis-cli --cluster add-node 127.0.0.1:7008 127.0.0.1:7001 --cluster-master-id 9397f2a2bced9ca95cbf1e78b67df947e14c7d0c
...
#查看节点信息,7008已经变成了主节点
aa608830d3b8f9a05bb2677aa84fdda024219244 127.0.0.1:7008@17008 master - 0 1602487284000 0 connected
9397f2a2bced9ca95cbf1e78b67df947e14c7d0c 127.0.0.1:7001@17001 master - 0 1602487283597 1 connected 0-5460
1584cce24eaefbed82372a61268885f6685419bc 127.0.0.1:7007@17007 slave 9397f2a2bced9ca95cbf1e78b67df947e14c7d0c 0 1602487282000 1 connected
aed6634c23190f0cd6c956cfe5a7b34d66cde350 127.0.0.1:7005@17005 slave e59d760a7e15ce1b69b531ff2d2278c7407f87c3 0 1602487281000 5 connected
9e4710c7cada29b576efef71d3d81f56f1bd28db 127.0.0.1:7003@17003 master - 0 1602487284606 3 connected 10923-16383
52d4a72eb9338f9827a65f8bc02d672b23ede717 127.0.0.1:7004@17004 slave 9397f2a2bced9ca95cbf1e78b67df947e14c7d0c 0 1602487282000 4 connected
2193f876372ba3a467792bee8305f195d203afcf 127.0.0.1:7006@17006 slave 9e4710c7cada29b576efef71d3d81f56f1bd28db 0 1602487282585 6 connected
e59d760a7e15ce1b69b531ff2d2278c7407f87c3 127.0.0.1:7002@17002 myself,master - 0 1602487282000 2 connected 5461-10922


#给主节点7008分配slot
root@ubuntu:/usr/local/redis# redis-cli --cluster reshard 127.0.0.1:7008
>>> Performing Cluster Check (using node 127.0.0.1:7008)
M: aa608830d3b8f9a05bb2677aa84fdda024219244 127.0.0.1:7008
   slots: (0 slots) master
S: 52d4a72eb9338f9827a65f8bc02d672b23ede717 127.0.0.1:7004
   slots: (0 slots) slave
   replicates 9397f2a2bced9ca95cbf1e78b67df947e14c7d0c
M: 9397f2a2bced9ca95cbf1e78b67df947e14c7d0c 127.0.0.1:7001
   slots:[0-5460] (5461 slots) master
   2 additional replica(s)
S: 1584cce24eaefbed82372a61268885f6685419bc 127.0.0.1:7007
   slots: (0 slots) slave
   replicates 9397f2a2bced9ca95cbf1e78b67df947e14c7d0c
S: 2193f876372ba3a467792bee8305f195d203afcf 127.0.0.1:7006
   slots: (0 slots) slave
   replicates 9e4710c7cada29b576efef71d3d81f56f1bd28db
M: 9e4710c7cada29b576efef71d3d81f56f1bd28db 127.0.0.1:7003
   slots:[10923-16383] (5461 slots) master
   1 additional replica(s)
S: aed6634c23190f0cd6c956cfe5a7b34d66cde350 127.0.0.1:7005
   slots: (0 slots) slave
   replicates e59d760a7e15ce1b69b531ff2d2278c7407f87c3
M: e59d760a7e15ce1b69b531ff2d2278c7407f87c3 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.
How many slots do you want to move (from 1 to 16384)? 800  #分配800个槽
What is the receiving node ID? aa608830d3b8f9a05bb2677aa84fdda024219244 #7008节点ID
Please enter all the source node IDs.
  Type 'all' to use all the nodes as source nodes for the hash slots.
  Type 'done' once you entered all the source nodes IDs.
Source node #1: 9397f2a2bced9ca95cbf1e78b67df947e14c7d0c #从7001节点取800个出来分配给7008,其他主节点也可以,或者all应该也行,没试过...
Source node #2: done 

Ready to move 800 slots.
  Source nodes:
    M: 9397f2a2bced9ca95cbf1e78b67df947e14c7d0c 127.0.0.1:7001
       slots:[0-5460] (5461 slots) master
       2 additional replica(s)
  Destination node:
    M: aa608830d3b8f9a05bb2677aa84fdda024219244 127.0.0.1:7008
       slots: (0 slots) master
    ...
#分配完成,查看节点信息
127.0.0.1:7002> cluster nodes
aa608830d3b8f9a05bb2677aa84fdda024219244 127.0.0.1:7008@17008 master - 0 1602488033000 7 connected 0-799  #7008槽 0~799正好800个
9397f2a2bced9ca95cbf1e78b67df947e14c7d0c 127.0.0.1:7001@17001 master - 0 1602488030586 1 connected 800-5460 #7001从800开始了
1584cce24eaefbed82372a61268885f6685419bc 127.0.0.1:7007@17007 slave 9397f2a2bced9ca95cbf1e78b67df947e14c7d0c 0 1602488031000 1 connected
aed6634c23190f0cd6c956cfe5a7b34d66cde350 127.0.0.1:7005@17005 slave e59d760a7e15ce1b69b531ff2d2278c7407f87c3 0 1602488031000 5 connected
9e4710c7cada29b576efef71d3d81f56f1bd28db 127.0.0.1:7003@17003 master - 0 1602488033616 3 connected 10923-16383
52d4a72eb9338f9827a65f8bc02d672b23ede717 127.0.0.1:7004@17004 slave 9397f2a2bced9ca95cbf1e78b67df947e14c7d0c 0 1602488032611 4 connected
2193f876372ba3a467792bee8305f195d203afcf 127.0.0.1:7006@17006 slave 9e4710c7cada29b576efef71d3d81f56f1bd28db 0 1602488031603 6 connected
e59d760a7e15ce1b69b531ff2d2278c7407f87c3 127.0.0.1:7002@17002 myself,master - 0 1602488031000 2 connected 5461-10922
  • 删除7008主节点,转移数据:
#创建一条数据,age对应的slot在7008节点上,自动转到7008节点
root@ubuntu:/usr/local/redis# redis-cli -c -p 7001
127.0.0.1:7001> set age 12
-> Redirected to slot [741] located at 127.0.0.1:7008
OK
#del-node只能删除没有分配槽的节点,否则会报错
root@ubuntu:/usr/local/redis# redis-cli --cluster del-node 127.0.0.1:7008 aa608830d3b8f9a05bb2677aa84fdda024219244 
>>> Removing node aa608830d3b8f9a05bb2677aa84fdda024219244 from cluster 127.0.0.1:7008
[ERR] Node 127.0.0.1:7008 is not empty! Reshard data away and try again.

#先转移槽中的数据
root@ubuntu:/usr/local/redis# redis-cli --cluster reshard 127.0.0.1:7008
>>> Performing Cluster Check (using node 127.0.0.1:7008)
M: aa608830d3b8f9a05bb2677aa84fdda024219244 127.0.0.1:7008
   slots:[0-799] (800 slots) master
   1 additional replica(s)
S: 52d4a72eb9338f9827a65f8bc02d672b23ede717 127.0.0.1:7004
   slots: (0 slots) slave
   replicates 9397f2a2bced9ca95cbf1e78b67df947e14c7d0c
M: 9397f2a2bced9ca95cbf1e78b67df947e14c7d0c 127.0.0.1:7001
   slots:[800-5460] (4661 slots) master
   1 additional replica(s)
S: 1584cce24eaefbed82372a61268885f6685419bc 127.0.0.1:7007
   slots: (0 slots) slave
   replicates aa608830d3b8f9a05bb2677aa84fdda024219244
S: 2193f876372ba3a467792bee8305f195d203afcf 127.0.0.1:7006
   slots: (0 slots) slave
   replicates 9e4710c7cada29b576efef71d3d81f56f1bd28db
M: 9e4710c7cada29b576efef71d3d81f56f1bd28db 127.0.0.1:7003
   slots:[10923-16383] (5461 slots) master
   1 additional replica(s)
S: aed6634c23190f0cd6c956cfe5a7b34d66cde350 127.0.0.1:7005
   slots: (0 slots) slave
   replicates e59d760a7e15ce1b69b531ff2d2278c7407f87c3
M: e59d760a7e15ce1b69b531ff2d2278c7407f87c3 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.
How many slots do you want to move (from 1 to 16384)? 800 #7008节点工800个槽
What is the receiving node ID? 9397f2a2bced9ca95cbf1e78b67df947e14c7d0c #指定7001节点接收7008节点数据
Please enter all the source node IDs.
  Type 'all' to use all the nodes as source nodes for the hash slots.
  Type 'done' once you entered all the source nodes IDs.
Source node #1: aa608830d3b8f9a05bb2677aa84fdda024219244  #7008节点ID
Source node #2: done

Ready to move 800 slots.
  Source nodes:
    M: aa608830d3b8f9a05bb2677aa84fdda024219244 127.0.0.1:7008
       slots:[0-799] (800 slots) master
       1 additional replica(s)
  Destination node:
    M: 9397f2a2bced9ca95cbf1e78b67df947e14c7d0c 127.0.0.1:7001
       slots:[800-5460] (4661 slots) master
       1 additional replica(s)
  Resharding plan:
    Moving slot 0 from aa608830d3b8f9a05bb2677aa84fdda024219244
    Moving slot 1 from aa608830d3b8f9a05bb2677aa84fdda024219244
    ...
Do you want to proceed with the proposed reshard plan (yes/no)? yes
...
#转移完成,再删除7008端口的节点
root@ubuntu:/usr/local/redis# redis-cli --cluster del-node 127.0.0.1:7008 aa608830d3b8f9a05bb2677aa84fdda024219244 
>>> Removing node aa608830d3b8f9a05bb2677aa84fdda024219244 from cluster 127.0.0.1:7008
>>> Sending CLUSTER FORGET messages to the cluster...
>>> SHUTDOWN the node.
  • 删除所有节点,丢弃数据:
    如果集群环境搭建错误,需要删除所有节点并丢弃数据
#删除.rdb数据文件,删除节点对应的conf文件
root@ubuntu:/var/lib/redis# ls
dump.rdb  nodes-7001.conf  nodes-7002.conf  nodes-7003.conf  nodes-7004.conf  nodes-7005.conf  nodes-7006.conf  nodes-7007.conf  nodes-7008.conf
root@ubuntu:/var/lib/redis# rm -f *
#删除节点
root@ubuntu:/var/lib/redis# redis-cli --cluster del-node 127.0.0.1:7001 9397f2a2bced9ca95cbf1e78b67df947e14c7d0c
>>> Removing node 9397f2a2bced9ca95cbf1e78b67df947e14c7d0c from cluster 127.0.0.1:7001
>>> Sending CLUSTER FORGET messages to the cluster...
>>> SHUTDOWN the node.
#再删除其他节点
...此处省略...
  • 关闭redis服务
root@ubuntu:~#redis-cli -h 127.0.0.1 -p 7001 shutdown
或者暴力关闭:kill -9 pid

你可能感兴趣的:(redis基本操作和集群搭建)