一、Redis理论部分 1 NoSQL 1.1 NoSQL的介绍 NoSQL = Not Only SQL ,意思是:不止用 SQL,也不局限于关系型数据库 。
它是非关系型数据库 的统称,用来解决传统关系型数据库(MySQL/Oracle)在高并发、海量数据、灵活结构 上的短板。
NoSQL :是 “Not Only SQL” 的缩写,并非 “不用 SQL”,而是突破关系模型限制 的一类数据库的统称,核心目标是解决 RDBMS 在高并发、海量数据、灵活结构场景下的短板 (比如电商秒杀、社交平台动态存储),牺牲部分严格一致性换取性能和扩展性。
RDBMS与NoSQL区别表s
维度 RDBMS(关系型) NoSQL(非关系型) 数据模型 固定 Schema(表结构),强关系(外键、JOIN) 灵活 Schema(无需预定义),弱 / 无关系 一致性保障 强一致性(ACID 事务) 最终一致性(BASE 理论),部分支持弱事务 查询能力 强大(SQL 支持联表、聚合、子查询、索引) 简单(基于键 / 属性查询),部分支持专用检索(如 ES 全文检索) 存储方式 磁盘为主(持久化优先) 内存 / 磁盘结合(Redis 内存、MongoDB 磁盘) 扩展性 垂直扩展为主(升级服务器),水平扩展复杂(分库分表) 水平扩展为主(分片 / 集群),易扩容 数据完整性 内置约束(主键、外键、唯一索引、非空) 无内置强约束,需业务层保证 典型代表 MySQL、Oracle、PostgreSQL、SQL Server Redis(键值)、MongoDB(文档)、ES(检索)、Cassandra(列族)
1.2 NoSQL出现的原因 传统关系型数据库(RDBMS)的痛点:
表结构固定,改字段麻烦 高并发扛不住 海量数据水平扩展难 不适合存非结构化数据(日志、JSON、图片信息等) NoSQL 就是为互联网高并发、大数据、灵活业务 而生的。
1.3 NoSQL 四大主流类型 NoSQL 类型 代表产品 核心特点 典型应用场景 键值数据库(Key-Value) Redis 读写性能极高,数据结构简单(key-value 键值对),支持多种数据类型 缓存、计数器、用户会话存储、分布式锁 文档数据库 MongoDB 存储 JSON/BSON 格式文档,结构灵活,无固定表结构,支持复杂查询 用户数据、订单管理、内容存储、爬虫数据存储 列族数据库 HBase、Cassandra 按列族存储数据,适配海量数据存储,高可用、高扩展,读写吞吐量大 大数据分析、日志存储、用户行为数据存储、时序数据 图数据库 Neo4j 专注存储实体间的关系,以图结构(节点、边)表达数据,擅长关系查询 社交关系网络、知识图谱、智能推荐系统、路径分析
1.4 NoSQL 的优缺点 维度 内容 NoSQL 核心特点 1. 灵活 Schema:无需提前建表,字段可随意添加2. 高并发、高性能3. 易水平扩展:通过增加机器实现扩容4. 适合海量、半结构化 / 非结构化数据5. 大多追求最终一致性,性能优先于强一致 NoSQL 缺点 1. 事务能力弱:大多不支持强事务与 ACID,不适合金融、支付等强一致场景2. 查询能力有限:一般不支持 JOIN、复杂子查询、多表关联3. 一致性较弱:多为最终一致性,可能出现短暂数据不一致4. 生态不如关系型数据库成熟:工具、运维、文档、人才较少5. 学习成本高:不同产品语法、架构差异大,学习与维护成本高
2 Redis 2.1 Redis介绍
Redis = Remote Dictionary Server
是一款开源、基于内存、高性能的键值(Key-Value)NoSQL 数据库 。
REmote DIctionary Server(Redis) 是一个由 Salvatore Sanfilippo 写的 key-value 存储系统,是跨平台的非关系型 数据库。
Redis 是一个开源的使用 ANSI C 语言编写、遵守 BSD 协议、支持网络、可基于内存、分布式、可选持久性的键值对(Key-Value )存储数据库,并提供多种语言的 API。
Redis 通常被称为数据结构服务器,因为值(value)可以是字符串(String) 、哈希(Hash) 、列表(list) 、集合(sets) 和有序集合(sorted sets) 等类型。
MySQL与Redis区别表
维度 MySQL Redis 存储类型 磁盘存储(持久化) 内存存储(可持久化到磁盘) 数据模型 关系型(表、行、列、约束) 非关系型(键值对,支持 5 + 数据结构) 读写性能 读尚可,写较慢(需磁盘 IO) 极高(内存操作,百万级 QPS) 事务支持 完整 ACID 事务 弱事务(仅支持简单事务、乐观锁) 查询能力 强大(SQL、联表、聚合、索引) 简单(仅基于键查询,支持简单过滤) 数据可靠性 高(binlog、redo log、主从) 中等(依赖持久化策略,内存易失) 适用数据规模 海量结构化数据(TB/PB 级) 少量热点数据(GB 级,受内存限制)
二、Redis集群实验 1 集群环境搭建 主机名 Redis1 Redis2 Redis3 IP 172.25.254.10 172.25.254.11 172.25.254.12
1.1 安装依赖 1 [root@Redis1 ~]# dnf install make gcc initscripts -y
1.2 下载源码包 1 [root@Redis1 ~]# wget https://download.redis.io/releases/redis-7.4.8.tar.gz
1.3 源码编译 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 [root@Redis1 ~]# tar zxf redis-7.4.8.tar.gz [root@Redis1 ~]# cd redis-7.4.8/ [root@Redis1 ~]# make && make install [root@Redis1 redis-7.4.8]# cd utils/ [root@Redis1 utils]# vim install_server.sh [root@Redis1 utils]# ./install_server.sh Welcome to the redis service installer This script will help you easily set up a running redis server Please select the redis port for this instance: [6379] Selecting default: 6379 Please select the redis config file name [/etc/redis/6379.conf] Selected default - /etc/redis/6379.conf Please select the redis log file name [/var/log/redis_6379.log ] Selected default - /var/log/redis_6379.log Please select the data directory for this instance [/var/lib/redis/6379] Selected default - /var/lib/redis/6379 Please select the redis executable path [/usr/local/bin/redis-server] Selected config: Port : 6379 Config file : /etc/redis/6379.conf Log file : /var/log/redis_6379.log Data dir : /var/lib/redis/6379 Executable : /usr/local/bin/redis-server Cli Executable : /usr/local/bin/redis-cli Is this ok? Then press ENTER to go on or Ctrl-C to abort. Copied /tmp/6379.conf => /etc/init.d/redis_6379 Installing service... Successfully added to chkconfig! Successfully added to runlevels 345! Starting Redis server... Installation successful!
1.4 环境检查 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 [root@Redis1 utils]# /etc/init.d/redis_6379 status Redis is running (35631) [root@Redis1 utils]# chkconfig redis_6379 off [root@Redis1 utils]# systemctl daemon-reload [root@Redis1 utils]# systemctl status redis_6379.service ○ redis_6379.service - LSB: start and stop redis_6379 Loaded: loaded (/etc/rc.d/init.d/redis_6379; generated) Active: inactive (dead) Docs: man:systemd-sysv-generator(8) [root@Redis1 utils]# systemctl start redis_6379.service [root@Redis1 utils]# systemctl status redis_6379.service ● redis_6379.service - LSB: start and stop redis_6379 Loaded: loaded (/etc/rc.d/init.d/redis_6379; generated) Active: active (exited) since Thu 2026-03-12 20:26:03 CST; 2s ago Docs: man:systemd-sysv-generator(8) Process: 36092 ExecStart=/etc/rc.d/init.d/redis_6379 start (code=exited, st> CPU: 2ms [root@Redis1 utils]# netstat -antlupe | grep redis tcp 0 0 127.0.0.1:6379 0.0.0.0:* LISTEN 0 67992 35631/redis-server tcp6 0 0 ::1:6379 :::* LISTEN 0 67993 35631/redis-server
从机部署同以上方式
2 基础操作命令 通用键
1 2 3 4 5 6 7 8 9 10 11 12 # 进入redis redis- cli # 删除键 DEL key:1 # 判断键是否存在 EXISTS key# 设置键过期时间(秒) EXPIRE key 1800 # 查看键剩余过期时间 TTL key # 查看键对应值的数据类型 TYPE key:1
字符串 (String)
1 2 3 4 5 6 7 8 9 10 11 # 此处key= user # 设置键值对 (会覆盖已有值) SET user "zhangsan"# 获取键的值 GET user # 仅键不存在时设置 (分布式锁) SETNX user 1 # 数值自增 1 (计数场景) INCR user :count 数值自增指定数 INCRBY user :count 5
哈希值(Hash)
1 2 3 4 5 6 7 8 9 10 11 # 此处key= user # 设置哈希字段值 HSET key:1 age 20 # 获取哈希字段值 HGET user :1 age # 获取哈希所有字段和值 HGETALL user :1 # 批量设置哈希字段 HMSET user :1 name 张三 gender 男 # 删除哈希指定字段 HDEL user :1 gender
列表(list)
1 2 3 4 5 6 7 8 9 10 11 # 此处key= msg, val= queue # 从列表左侧添加元素 LPUSH msg:queue "消息1" # 从列表右侧添加元素 RPUSH msg:queue "消息2" # 从列表左侧弹出元素 LPOP msg:queue # 从列表右侧弹出元素 RPOP msg:queue # 获取列表所有元素 LRANGE msg:queue 0 -1
集合(Set)
1 2 3 4 5 6 7 8 9 10 11 # 此处key= user , mem= tags # 向集合添加元素(去重) SADD user :1 :tags 技术 运动 # 获取集合所有元素 SMEMBERS user :1 :tags # 判断元素是否在集合中 SISMEMBER user :1 :tags 技术 # 获取两个集合的交集(共同好友) SINTER user :1 :tags user :2 :tags # 获取两个集合的并集 SUNION user :1 :tags user :2 :tags
有序集合(ZSet)
1 2 3 4 5 6 7 8 9 10 11 # 此处key= user , mem= tags #添加元素并指定分数 ZADD rank:score 95 张三 90 李四 #升序查所有元素(带分数) ZRANGE rank:score 0 -1 WITHSCORES #降序查所有元素(带分数) ZREVRANGE rank:score 0 -1 WITHSCORES #获取元素的分数 ZSCORE rank:score 张三 #给元素分数增加指定值 ZINCRBY rank:score 5 李四
3 主从复制 主节点(Redis1)配置
1 2 3 4 5 6 7 [root@Redis1 ~]# vim /etc/redis/6379.conf 89 90 bind * -::* 114 protected-mode no [root@Redis1 ~]# systemctl restart redis_6379.service
从节点(Redis2+3)配置
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 [root@Redis2 ~]# vim /etc/redis/6379.conf 89 90 bind * -::* 114 protected-mode no 541 replicaof 172.25.254.10 6379 [root@Redis2 ~]# systemctl restart redis_6379.service [root@Redis3 ~]# vim /etc/redis/redis.conf 89 90 bind * -::* 114 protected-mode no 541 replicaof 172.25.254.10 6379 [root@Redis3 ~]# systemctl restart redis_6379.service
查看状态并测试
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 [root@Redis1 ~]# redis-cli 127.0.0.1:6379> info replication role:master connected_slaves:2 slave0:ip=172.25.254.11,port=6379,state=online,offset=238,lag=1 slave1:ip=172.25.254.12,port=6379,state=online,offset=238,lag=0 master_failover_state:no-failover master_replid:6dabd4deb482a7c7541efee49ecb006eef4f0a59 master_replid2:0000000000000000000000000000000000000000 master_repl_offset:238 second_repl_offset:-1 repl_backlog_active:1 repl_backlog_size:1048576 repl_backlog_first_byte_offset:1 repl_backlog_histlen:238 [root@Redis2 ~]# redis-cli 127.0.0.1:6379> info replication role:slave master_host:172.25.254.10 master_port:6379 master_link_status:up master_last_io_seconds_ago:2 master_sync_in_progress:0 slave_read_repl_offset:294 slave_repl_offset:294 slave_priority:100 slave_read_only:1 replica_announced:1 connected_slaves:0 master_failover_state:no-failover master_replid:6dabd4deb482a7c7541efee49ecb006eef4f0a59 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
测试数据同步性
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 [root@Redis1 ~]# redis-cli 127.0.0.1:6379> set name hua OK 127.0.0.1:6379> get name "hua" [root@Redis2 ~]# redis-cli 127.0.0.1:6379> get name "hua" [root@Redis3 ~]# redis-cli 127.0.0.1:6379> get name "hua" 127.0.0.1:6379> set test 123 (error) READONLY You can't write against a read only replica.
4 哨兵模式 主节点(Redis1)
1 2 3 4 5 6 7 8 9 10 11 12 [root@Redis1 ~]# cd redis-7.4.8/ [root@Redis1 redis-7.4.8]# cp -p sentinel.conf /etc/redis/ [root@Redis1 redis-7.4.8]#vim /etc/redis/sentinel.conf protected-mode no port 26379 daemonize no pidfile /var/run/redis-sentinel.pid loglevel notice sentinel monitor mymaster 172.25.254.10 6379 2 sentinel down-after-milliseconds mymaster 10000 sentinel parallel-syncs mymaster 1 sentinel failover-timeout mymaster 180000
从节点(Redis2+3)
1 2 3 4 [root@Redis2+3 ~]# vim /etc/redis/redis.conf protected-mode no [root@Redis2+3 ~]# systemctl restart redis_6379.service
主节点(Redis1)
1 2 3 4 5 6 7 8 [root@Redis1 redis-7.4.8]# scp /etc/redis/sentinel.conf root@172.25.254.11:/etc/redis sentinel.conf 100% 14KB 13.2MB/s 00:00 [root@Redis1 redis-7.4.8]# scp /etc/redis/sentinel.conf root@172.25.254.12:/etc/redis sentinel.conf 100% 14KB 15.3MB/s 00:00
所有节点
1 [root@Redis1~3 ~]# redis-sentinel /etc/redis/sentinel.conf
测试故障切换
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 [root@Redis1 ~]# redis-cli 127.0.0.1:6379> shutdown (0.83s) not connected> quit 1843:X 13 Mar 2026 15:17:53.477 [root@Redis2 ~]# redis-cli 127.0.0.1:6379> info replication role:slave master_host:172.25.254.12 master_port:6379 master_link_status:up master_last_io_seconds_ago:1 master_sync_in_progress:0 slave_read_repl_offset:94712 slave_repl_offset:94712 slave_priority:100 slave_read_only:1 replica_announced:1 connected_slaves:0 master_failover_state:no-failover master_replid:0818916f28b559507f540b4806020de013246672 master_replid2:3cebba5b1695c243e04eb22855b8d8e4ab357f1b master_repl_offset:94712 second_repl_offset:47429 repl_backlog_active:1 repl_backlog_size:1048576 repl_backlog_first_byte_offset:15 repl_backlog_histlen:94698 [root@Redis3 ~]# redis-cli 127.0.0.1:6379> info replication role:master connected_slaves:1 slave0:ip=172.25.254.11,port=6379,state=online,offset=85773,lag=1 master_failover_state:no-failover master_replid:0818916f28b559507f540b4806020de013246672 master_replid2:3cebba5b1695c243e04eb22855b8d8e4ab357f1b master_repl_offset:85914 second_repl_offset:47429 repl_backlog_active:1 repl_backlog_size:1048576 repl_backlog_first_byte_offset:15 repl_backlog_histlen:85900 [root@Redis1 ~]# /etc/init.d/redis_6379 start Starting Redis server... [root@Redis2 ~]# redis-cli 127.0.0.1:6379> info replication role:master connected_slaves:2 slave0:ip=172.25.254.11,port=6379,state=online,offset=112204,lag=1 slave1:ip=172.25.254.10,port=6379,state=online,offset=112486,lag=0 master_failover_state:no-failover master_replid:0818916f28b559507f540b4806020de013246672 master_replid2:3cebba5b1695c243e04eb22855b8d8e4ab357f1b master_repl_offset:112486 second_repl_offset:47429 repl_backlog_active:1 repl_backlog_size:1048576 repl_backlog_first_byte_offset:15 repl_backlog_histlen:112472
5 cluster集群 5.1 修改配置文件 1 2 3 4 5 6 7 8 9 10 [root@Redis1 ~]# vim /etc/redis/6379.conf bind 0.0.0.0 protected-mode no masterauth "123456" requirepass "123456" cluster-enabled yes cluster-config-file nodes-6379.conf cluster-node-timeout 15000 [root@Redis1 ~]# /etc/init.d/redis_6379 restart
5.2 启动集群 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 [root@Redis1 ~]# for i in 20 30 40 50 60; do scp /etc/redis/6379.conf root@172.25.254.$i :/etc/redis/; sh root@172.25.254.$i /etc/init.d/redis_6379 restart; done ; [root@Redis1 ~]# redis-cli --cluster create 172.25.254.10:6379 172.25.254.20:6379 172.25.254.30:6379 172.25.254.40:6379 172.25.254.50:6379 172.25.254.60:6379 --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 172.25.254.50:6379 to 172.25.254.10:6379 Adding replica 172.25.254.60:6379 to 172.25.254.20:6379 Adding replica 172.25.254.40:6379 to 172.25.254.30:6379 M: aa9e388510945c464eb596286b8dbde99caba7df 172.25.254.10:6379 slots:[0-5460] (5461 slots) master M: b841f4df08d81539c07576262a18856f38e8463e 172.25.254.20:6379 slots:[5461-10922] (5462 slots) master M: 418cabd57a6c47531f903d79cd7298e312378dfb 172.25.254.30:6379 slots:[10923-16383] (5461 slots) master S: 20ac5939668bd0ea34eb5a04208f3a6448f88706 172.25.254.40:6379 replicates 418cabd57a6c47531f903d79cd7298e312378dfb S: 49b0dcce0e1d4713a72a312edaf68c65b967563d 172.25.254.50:6379 replicates aa9e388510945c464eb596286b8dbde99caba7df S: 1555786e5f2f1cb87ba30c2335f058940f6aa971 172.25.254.60:6379 replicates b841f4df08d81539c07576262a18856f38e8463e 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 172.25.254.10:6379) M: aa9e388510945c464eb596286b8dbde99caba7df 172.25.254.10:6379 slots:[0-5460] (5461 slots) master 1 additional replica(s) S: 1555786e5f2f1cb87ba30c2335f058940f6aa971 172.25.254.60:6379 slots: (0 slots) slave replicates b841f4df08d81539c07576262a18856f38e8463e S: 49b0dcce0e1d4713a72a312edaf68c65b967563d 172.25.254.50:6379 slots: (0 slots) slave replicates aa9e388510945c464eb596286b8dbde99caba7df M: 418cabd57a6c47531f903d79cd7298e312378dfb 172.25.254.30:6379 slots:[10923-16383] (5461 slots) master 1 additional replica(s) S: 20ac5939668bd0ea34eb5a04208f3a6448f88706 172.25.254.40:6379 slots: (0 slots) slave replicates 418cabd57a6c47531f903d79cd7298e312378dfb M: b841f4df08d81539c07576262a18856f38e8463e 172.25.254.20:6379 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@Redis1 ~]# redis-cli --cluster info 172.25.254.10:6379 172.25.254.10:6379 (aa9e3885...) -> 0 keys | 5461 slots | 1 slaves. 172.25.254.30:6379 (418cabd5...) -> 0 keys | 5461 slots | 1 slaves. 172.25.254.20:6379 (b841f4df...) -> 0 keys | 5462 slots | 1 slaves. [OK] 0 keys in 3 masters. 0.00 keys per slot on average. [root@Redis1 ~]# redis-cli cluster info cluster_state:ok cluster_slots_assigned:16384 cluster_slots_ok:16384 cluster_slots_pfail:0 cluster_slots_fail:0 cluster_known_nodes:6 cluster_size:3 cluster_current_epoch:6 cluster_my_epoch:1 cluster_stats_messages_ping_sent:78 cluster_stats_messages_pong_sent:80 cluster_stats_messages_sent:158 cluster_stats_messages_ping_received:75 cluster_stats_messages_pong_received:78 cluster_stats_messages_meet_received:5 cluster_stats_messages_received:158 total_cluster_links_buffer_limit_exceeded:0 [root@Redis1 ~]# redis-cli --cluster check 172.25.254.10:6379 172.25.254.10:6379 (aa9e3885...) -> 0 keys | 5461 slots | 1 slaves. 172.25.254.30:6379 (418cabd5...) -> 0 keys | 5461 slots | 1 slaves. 172.25.254.20:6379 (b841f4df...) -> 0 keys | 5462 slots | 1 slaves. [OK] 0 keys in 3 masters. 0.00 keys per slot on average. >>> Performing Cluster Check (using node 172.25.254.10:6379) M: aa9e388510945c464eb596286b8dbde99caba7df 172.25.254.10:6379 slots:[0-5460] (5461 slots) master 1 additional replica(s) S: 1555786e5f2f1cb87ba30c2335f058940f6aa971 172.25.254.60:6379 slots: (0 slots) slave replicates b841f4df08d81539c07576262a18856f38e8463e S: 49b0dcce0e1d4713a72a312edaf68c65b967563d 172.25.254.50:6379 slots: (0 slots) slave replicates aa9e388510945c464eb596286b8dbde99caba7df M: 418cabd57a6c47531f903d79cd7298e312378dfb 172.25.254.30:6379 slots:[10923-16383] (5461 slots) master 1 additional replica(s) S: 20ac5939668bd0ea34eb5a04208f3a6448f88706 172.25.254.40:6379 slots: (0 slots) slave replicates 418cabd57a6c47531f903d79cd7298e312378dfb M: b841f4df08d81539c07576262a18856f38e8463e 172.25.254.20:6379 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.
5.3 集群扩容 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 [root@Redis1 ~]# redis-cli --cluster add-node 172.25.254.70:6379 172.25.254.10:6379 [root@Redis1 ~]# redis-cli --cluster check 172.25.254.10:6379 172.25.254.10:6379 (8db833f3...) -> 0 keys | 5461 slots | 1 slaves. 172.25.254.70:6379 (dfabfe07...) -> 0 keys | 0 slots | 0 slaves. 172.25.254.30:6379 (d9300173...) -> 0 keys | 5461 slots | 1 slaves. 172.25.254.20:6379 (ca599940...) -> 1 keys | 5462 slots | 1 slaves. [OK] 1 keys in 4 masters. 0.00 keys per slot on average. >>> Performing Cluster Check (using node 172.25.254.10:6379) M: 8db833f3c3bc6b8f93e87111f13f56d366f833a0 172.25.254.10:6379 slots:[0-5460] (5461 slots) master 1 additional replica(s) M: dfabfe07170ac9b5d20a5a7a70c836877bd64504 172.25.254.70:6379 slots: (0 slots) master S: c939a04358edc1ce7a1c1a44561d77fb402025fd 172.25.254.60:6379 slots: (0 slots) slave replicates ca599940209f55c07d06951480703bb0a5d8873a M: d9300173b75149d3056f0ee3edec063f8ec66e9a 172.25.254.30:6379 slots:[10923-16383] (5461 slots) master 1 additional replica(s) M: ca599940209f55c07d06951480703bb0a5d8873a 172.25.254.20:6379 slots:[5461-10922] (5462 slots) master 1 additional replica(s) S: ba6ef067c63d30c213493eb48d43427015018898 172.25.254.50:6379 slots: (0 slots) slave replicates 8db833f3c3bc6b8f93e87111f13f56d366f833a0 S: 32d797eb30094b77edb896abcc0b0fc91ccdb4fd 172.25.254.40:6379 slots: (0 slots) slave replicates d9300173b75149d3056f0ee3edec063f8ec66e9a [OK] All nodes agree about slots configuration. >>> Check for open slots... >>> Check slots coverage... [OK] All 16384 slots covered. >>> Performing Cluster Check (using node 172.25.254.10:6379) M: 8db833f3c3bc6b8f93e87111f13f56d366f833a0 172.25.254.10:6379 slots:[0-5460] (5461 slots) master 1 additional replica(s) M: dfabfe07170ac9b5d20a5a7a70c836877bd64504 172.25.254.70:6379 slots: (0 slots) master S: c939a04358edc1ce7a1c1a44561d77fb402025fd 172.25.254.60:6379 slots: (0 slots) slave replicates ca599940209f55c07d06951480703bb0a5d8873a M: d9300173b75149d3056f0ee3edec063f8ec66e9a 172.25.254.30:6379 slots:[10923-16383] (5461 slots) master 1 additional replica(s) M: ca599940209f55c07d06951480703bb0a5d8873a 172.25.254.20:6379 slots:[5461-10922] (5462 slots) master 1 additional replica(s) S: ba6ef067c63d30c213493eb48d43427015018898 172.25.254.50:6379 slots: (0 slots) slave replicates 8db833f3c3bc6b8f93e87111f13f56d366f833a0 S: 32d797eb30094b77edb896abcc0b0fc91ccdb4fd 172.25.254.40:6379 slots: (0 slots) slave replicates d9300173b75149d3056f0ee3edec063f8ec66e9a [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)? 4096 What is the receiving node ID? dfabfe07170ac9b5d20a5a7a70c836877bd64504 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 Ready to move 4096 slots. [root@Redis1 ~]# redis-cli --cluster add-node 172.25.254.80:6379 172.25.254.10:6379 --cluster-slave --cluster-master-id dfabfe07170ac9b5d20a5a7a70c836877bd64504 [root@Redis1 ~]# redis-cli --cluster check 172.25.254.10:6379 172.25.254.10:6379 (8db833f3...) -> 0 keys | 4096 slots | 1 slaves. 172.25.254.70:6379 (dfabfe07...) -> 1 keys | 4096 slots | 1 slaves. 172.25.254.30:6379 (d9300173...) -> 0 keys | 4096 slots | 1 slaves. 172.25.254.20:6379 (ca599940...) -> 0 keys | 4096 slots | 1 slaves. [OK] 1 keys in 4 masters. 0.00 keys per slot on average. >>> Performing Cluster Check (using node 172.25.254.10:6379) M: 8db833f3c3bc6b8f93e87111f13f56d366f833a0 172.25.254.10:6379 slots:[1365-5460] (4096 slots) master 1 additional replica(s) S: 1176ee294e6b5071ca57e93374d04ac22028daed 172.25.254.80:6379 slots: (0 slots) slave replicates dfabfe07170ac9b5d20a5a7a70c836877bd64504 M: dfabfe07170ac9b5d20a5a7a70c836877bd64504 172.25.254.70:6379 slots:[0-1364],[5461-6826],[10923-12287] (4096 slots) master 1 additional replica(s) S: c939a04358edc1ce7a1c1a44561d77fb402025fd 172.25.254.60:6379 slots: (0 slots) slave replicates ca599940209f55c07d06951480703bb0a5d8873a M: d9300173b75149d3056f0ee3edec063f8ec66e9a 172.25.254.30:6379 slots:[12288-16383] (4096 slots) master 1 additional replica(s) M: ca599940209f55c07d06951480703bb0a5d8873a 172.25.254.20:6379 slots:[6827-10922] (4096 slots) master 1 additional replica(s) S: ba6ef067c63d30c213493eb48d43427015018898 172.25.254.50:6379 slots: (0 slots) slave replicates 8db833f3c3bc6b8f93e87111f13f56d366f833a0 S: 32d797eb30094b77edb896abcc0b0fc91ccdb4fd 172.25.254.40:6379 slots: (0 slots) slave replicates d9300173b75149d3056f0ee3edec063f8ec66e9a [OK] All nodes agree about slots configuration. >>> Check for open slots... >>> Check slots coverage... [OK] All 16384 slots covered.
5.4 集群缩容 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 [root@redis-node1 ~]# redis-cli --cluster reshard 172.25.254.10:6379 >>> Performing Cluster Check (using node 172.25.254.10:6379) M: 8db833f3c3bc6b8f93e87111f13f56d366f833a0 172.25.254.10:6379 slots:[1365-5460] (4096 slots) master 1 additional replica(s) S: 1176ee294e6b5071ca57e93374d04ac22028daed 172.25.254.80:6379 slots: (0 slots) slave replicates dfabfe07170ac9b5d20a5a7a70c836877bd64504 M: dfabfe07170ac9b5d20a5a7a70c836877bd64504 172.25.254.70:6379 slots:[0-1364],[5461-6826],[10923-12287] (4096 slots) master 1 additional replica(s) S: c939a04358edc1ce7a1c1a44561d77fb402025fd 172.25.254.60:6379 slots: (0 slots) slave replicates ca599940209f55c07d06951480703bb0a5d8873a M: d9300173b75149d3056f0ee3edec063f8ec66e9a 172.25.254.30:6379 slots:[12288-16383] (4096 slots) master 1 additional replica(s) M: ca599940209f55c07d06951480703bb0a5d8873a 172.25.254.20:6379 slots:[6827-10922] (4096 slots) master 1 additional replica(s) S: ba6ef067c63d30c213493eb48d43427015018898 172.25.254.50:6379 slots: (0 slots) slave replicates 8db833f3c3bc6b8f93e87111f13f56d366f833a0 S: 32d797eb30094b77edb896abcc0b0fc91ccdb4fd 172.25.254.40:6379 slots: (0 slots) slave replicates d9300173b75149d3056f0ee3edec063f8ec66e9a [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)? 4096 What is the receiving node ID? 8db833f3c3bc6b8f93e87111f13f56d366f833a0 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 Source node [root@redis-node1 ~]# redis-cli --cluster del-node 172.25.254.10:6379 dfabfe07170ac9b5d20a5a7a70c836877bd64504 >>> Removing node dfabfe07170ac9b5d20a5a7a70c836877bd64504 from cluster 172.25.254.10:6379 >>> Sending CLUSTER FORGET messages to the cluster... >>> Sending CLUSTER RESET SOFT to the deleted node. [root@redis-node1 ~]# redis-cli --cluster del-node 172.25.254.10:6379 1176ee294e6b5071ca57e93374d04ac22028daed >>> Removing node 1176ee294e6b5071ca57e93374d04ac22028daed from cluster 172.25.254.10:6379 >>> Sending CLUSTER FORGET messages to the cluster... >>> Sending CLUSTER RESET SOFT to the deleted node. [root@redis-node1 ~]# redis-cli --cluster check 172.25.254.10:6379 172.25.254.10:6379 (8db833f3...) -> 1 keys | 8192 slots | 1 slaves. 172.25.254.30:6379 (d9300173...) -> 0 keys | 4096 slots | 1 slaves. 172.25.254.20:6379 (ca599940...) -> 0 keys | 4096 slots | 1 slaves. [OK] 1 keys in 3 masters. 0.00 keys per slot on average. >>> Performing Cluster Check (using node 172.25.254.10:6379) M: 8db833f3c3bc6b8f93e87111f13f56d366f833a0 172.25.254.10:6379 slots:[0-6826],[10923-12287] (8192 slots) master 1 additional replica(s) S: c939a04358edc1ce7a1c1a44561d77fb402025fd 172.25.254.60:6379 slots: (0 slots) slave replicates ca599940209f55c07d06951480703bb0a5d8873a M: d9300173b75149d3056f0ee3edec063f8ec66e9a 172.25.254.30:6379 slots:[12288-16383] (4096 slots) master 1 additional replica(s) M: ca599940209f55c07d06951480703bb0a5d8873a 172.25.254.20:6379 slots:[6827-10922] (4096 slots) master 1 additional replica(s) S: ba6ef067c63d30c213493eb48d43427015018898 172.25.254.50:6379 slots: (0 slots) slave replicates 8db833f3c3bc6b8f93e87111f13f56d366f833a0 S: 32d797eb30094b77edb896abcc0b0fc91ccdb4fd 172.25.254.40:6379 slots: (0 slots) slave replicates d9300173b75149d3056f0ee3edec063f8ec66e9a [OK] All nodes agree about slots configuration. >>> Check for open slots... >>> Check slots coverage... [OK] All 16384 slots covered.