Redis5.0版本以上安装Redis单机多实例集群

1.下载地址: http://www.redis.cn/download.html 2. 解压:

tar -zxvf redis-5.0.5.tgz 

cd redis-5.0.5/

mkdir redis-cluster

cd redis-cluster

mkdir 7001 7002 7003 7004 7005 7006

3.修改配置文件

port 7001
cluster-enabled yes
cluster-config-file 7001.conf
cluster-node-timeout 5000
appendonly yes

修改好一个配置文件将其copy到 7001 —— 7006的每一个文件中,注意:修改每一个文件的端口号

4. 启动实例 :

vim redis-cluster-start.sh
redis-server /usr/local/redis-cluster/7001/redis.conf
redis-server /usr/local/redis-cluster/7002/redis.conf
redis-server /usr/local/redis-cluster/7003/redis.conf
redis-server /usr/local/redis-cluster/7004/redis.conf
redis-server /usr/local/redis-cluster/7005/redis.conf
redis-server /usr/local/redis-cluster/7006/redis.conf

设置可执行权限,并执行 

chmod 777 redis-cluster-start.sh 

./redis-cluster-start.sh

5. 创建集群 

redis-cli --cluster create 192.168.75.130:7001 192.168.75.130:7002 192.168.75.130:7003 192.168.75.130:7004 192.168.75.130:7005  192.168.75.130:7006 --cluster-replicas 1

 6.连接集群

root@ubuntu:/home/song# redis-cli -c -h 192.168.75.130 -p 7001
192.168.75.130:7001> 

7.查看集群节点:

192.168.75.130:7001> cluster nodes
d76081af88b93f8a0fac9aa7d0b6ffd17b6b828a 192.168.75.130:7001@17001 myself,slave 1e1dd73067d05da1c0f0e21dbfd8d6b159a939ed 0 1569243825000 1 connected
3ea5e4740f60d3a98fc212ca07e76387c6781de1 192.168.75.130:6379@16379 slave 2f5915eeebec20192de7ce855d7fb9babcad369b 0 1569243824000 2 connected
1e1dd73067d05da1c0f0e21dbfd8d6b159a939ed 192.168.75.130:7004@17004 master - 0 1569243824000 7 connected 0-5460
9b614e352a24b51c9f196159e5e42718f82472d9 192.168.75.130:7006@17006 slave f5e00df5c63181ecc911ea8e457f7f58b661178e 0 1569243824000 6 connected
2f5915eeebec20192de7ce855d7fb9babcad369b 192.168.75.130:7002@17002 master - 0 1569243825558 2 connected 5461-10922
f5e00df5c63181ecc911ea8e457f7f58b661178e 192.168.75.130:7003@17003 master - 0 1569243823749 3 connected 10923-16383
2d267501b538930e3577f63e0e7e1a4b674caabc 192.168.75.130:7005@17005 slave 2f5915eeebec20192de7ce855d7fb9babcad369b 0 1569243824754 5 connected
192.168.75.130:7001> 

 8. 集群操作: cluster

  • CLUSTER INFO 打印集群的信息  
  • CLUSTER NODES 列出集群当前已知的所有节点(node),以及这些节点的相关信息

节点(node)  

  • CLUSTER MEET   将 ip 和 port 所指定的节点添加到集群当中,让它成为集群的一份子。  
  • CLUSTER FORGET <node_id> 从集群中移除 node_id 指定的节点。  
  • CLUSTER REPLICATE <node_id> 将当前节点设置为 node_id 指定的节点的从节点。  
  • CLUSTER SAVECONFIG 将节点的配置文件保存到硬盘里面。    

槽(slot)  

  • CLUSTER ADDSLOTS  [slot ...] 将一个或多个槽(slot)指派(assign)给当前节点。  
  • CLUSTER DELSLOTS  [slot ...] 移除一个或多个槽对当前节点的指派。  
  • CLUSTER FLUSHSLOTS 移除指派给当前节点的所有槽,让当前节点变成一个没有指派任何槽的节点。  
  • CLUSTER SETSLOT  NODE <node_id> 将槽 slot 指派给 node_id 指定的节点,如果槽已经指派给另一个节点,那么先让另一个节点删除该槽>,然后再进行指派。  
  • CLUSTER SETSLOT  MIGRATING <node_id> 将本节点的槽 slot 迁移到 node_id 指定的节点中。  
  • CLUSTER SETSLOT  importING <node_id> 从 node_id 指定的节点中导入槽 slot 到本节点。  
  • CLUSTER SETSLOT  STABLE 取消对槽 slot 的导入(import)或者迁移(migrate)。 

键 (key)  

  • CLUSTER KEYSLOT  计算键 key 应该被放置在哪个槽上。  
  • CLUSTER COUNTKEYSINSLOT  返回槽 slot 目前包含的键值对数量。  
  • CLUSTER GETKEYSINSLOT   返回 count 个 slot 槽中的键。  

示例:

       例如将 6379的节点添加到集群之中的操作如下: 

 1. 配置redis 6379的集群配置

protected-mode yes

# Accept connections on the specified port, default is 6379 (IANA #815344).
# If port 0 is specified Redis will not listen on a TCP socket.

port 6379



cluster-enabled yes

# Every cluster node has a cluster configuration file. This file is not
# intended to be edited by hand. It is created and updated by Redis nodes.
# Every Redis Cluster node requires a different cluster configuration file.
# Make sure that instances running in the same system do not have
# overlapping cluster configuration file names.

 cluster-config-file nodes-6379.conf

# Cluster node timeout is the amount of milliseconds a node must be unreachable
# for it to be considered in failure state.
# Most other internal time limits are multiple of the node timeout.
#

 cluster-node-timeout 15000

  2. 开启实例

redis-server /usr/local/redis-5.0.5/redis.conf 

  3. 连接集群

root@ubuntu:/home/song# redis-cli -c -h 192.168.75.130 -p 7001
192.168.75.130:7001> 

4. 添加从节点:

192.168.75.130:7001> CLUSTER MEET 192.168.75.130 6379
OK
192.168.75.130:7001> 

192.168.75.130:7001> cluster nodes

 

 5.删除从节点:

192.168.75.130:7001> CLUSTER FORGET 3ea5e4740f60d3a98fc212ca07e76387c6781de1
OK

 

设置当前节点为master:

 

文章转自:Redis5.0版本以上安装Redis单机多实例集群_PHP-考高分网 (kaotop.com)

上一篇:关于数据库的基本语句


下一篇:第三本书第三章Apache的管理及优化