redis 学习20 ---集群

在/usr/local/bin目录下创建一个文件夹

mkdir redis_cluster

然后在redis_cluster 文件夹下分别创建6个目录

mkdir 7000 7001 7002 7003 7004 7005

然后将redis.conf分别cp到各个文件夹

[root@localhost redis_cluster]# cp /usr/local/bin/redis.conf ./7000
[root@localhost redis_cluster]# cp /usr/local/bin/redis.conf ./7001
[root@localhost redis_cluster]# cp /usr/local/bin/redis.conf ./7002
[root@localhost redis_cluster]# cp /usr/local/bin/redis.conf ./7003
[root@localhost redis_cluster]# cp /usr/local/bin/redis.conf ./7004
[root@localhost redis_cluster]# cp /usr/local/bin/redis.conf ./7005

然后分别修改配置文件,当然了,我这里应该先只复制一个的,不然等下一个一个改有点麻烦=-=

修改哪些内容呢?

如果你是多服务器集群那么你需要把保护模式关闭

protected-mode yes #修改成no

然后修改端口号

port 7000

daemonize yes #开启后台运行模式

logfile "7000.log" #日志文件

dbfilename dump7000.rdb #rdb文件

pidfile /var/run/redis_7000.pid #pid进程文件

开启集群模式

cluster-enabled yes

cluster-config-file nodes-7000.conf

cluster-node-timeout 5000

这里官方还推荐开启aof持久化模式,这里我就不开了,rdb持久化够用了

appendonly yes

然后设置密码

masterauth 123456 #集群密码

requirepass 12345678 #redis密码,保持每个redis密码一致吧

这种就都配置好了

vim 替换 :%s/7000/7001/g

/usr/local/bin/redis-server ./7000/redis.conf 

配置文件修改好后,启动6个redis服务

然后通过redis-cli 创建集群

[root@localhost redis_cluster]# /usr/local/bin/redis-cli --cluster create -a 12345678 127.0.0.1:7000 127.0.0.1:7001 127.0.0.1:7002 127.0.0.1:7003 127.0.0.1:7005 127.0.0.1:7006 --cluster-replicas 1
Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe.
>>> 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:7000
Adding replica 127.0.0.1:7006 to 127.0.0.1:7001
Adding replica 127.0.0.1:7003 to 127.0.0.1:7002
>>> Trying to optimize slaves allocation for anti-affinity
[WARNING] Some slaves are in the same host as their master
M: 1ee27f35e0472d161087f33e257299a5a8bad4ca 127.0.0.1:7000
   slots:[0-5460] (5461 slots) master
M: 6c13e1821e3c35cf4dfc462a2d35e962509f787a 127.0.0.1:7001
   slots:[5461-10922] (5462 slots) master
M: 37234477b5159c11f261feeadc4ff20c62f7671c 127.0.0.1:7002
   slots:[10923-16383] (5461 slots) master
S: 665a1afb95ecc0bd74b7f4864d97a2b9a1c2c00a 127.0.0.1:7003
   replicates 37234477b5159c11f261feeadc4ff20c62f7671c
S: c94ff6fb3ad99b24ff4b61069890e4834fb287bd 127.0.0.1:7005
   replicates 1ee27f35e0472d161087f33e257299a5a8bad4ca
S: 8cf19cbf46cb8fa0b0d72b967a32876d8b170f35 127.0.0.1:7006
   replicates 6c13e1821e3c35cf4dfc462a2d35e962509f787a
Can I set the above configuration? (type 'yes' to accept): 

 这样redis集群就创建成功了

连接集群的命令

/usr/local/bin/redis-cli -h 127.0.0.1 -c -p 7000 -a 12345678

info replication查看redis服务详情

cluster nodes

 

最少需要3个主节点,我们这里是3主3从,如果有一对主从节点都挂了,那么集群就无法正常运行了

start.sh

/usr/local/bin/redis-server ./7000/redis.conf
/usr/local/bin/redis-server ./7001/redis.conf
/usr/local/bin/redis-server ./7002/redis.conf
/usr/local/bin/redis-server ./7003/redis.conf
/usr/local/bin/redis-server ./7004/redis.conf
/usr/local/bin/redis-server ./7005/redis.conf

chomd u+x start.h

shotdown.sh

/usr/local/bin/redis-cli -c -h 127.0.0.1 -p 7000 -a 12345678 shotdown
/usr/local/bin/redis-cli -c -h 127.0.0.1 -p 7001 -a 12345678 shotdown
/usr/local/bin/redis-cli -c -h 127.0.0.1 -p 7002 -a 12345678 shotdown
/usr/local/bin/redis-cli -c -h 127.0.0.1 -p 7003 -a 12345678 shotdown
/usr/local/bin/redis-cli -c -h 127.0.0.1 -p 7004 -a 12345678 shotdown
/usr/local/bin/redis-cli -c -h 127.0.0.1 -p 7005 -a 12345678 shotdown

chomd u+x shotdown.h

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值