当前位置:   article > 正文

Redis简单集群搭建及(error)NOAUTH Authentication required等问题的解决方法_noauth authentication required.

noauth authentication required.

 

 

Redis简单集群搭建及(error)NOAUTH Authentication required等问题的解决方法

 

一、redisz主从集群最少需要6个节点

首先我们既然要搭建集群,那么master节点至少要3个,slave节点也是3个,为什么呢?这是因为一个redis集群如果要对外提供可用的服务,那么集群中必须要有过半的master节点正常工作。基于这个特性,如果想搭建一个能够允许 n 个master节点挂掉的集群,那么就要搭建2n+1个master节点的集群。(感觉和Zookeeper的vote机制差不多)

如:

2个master节点,挂掉1个,则1不过半,则集群down掉,无法使用,容错率为0

3个master节点,挂掉1个,2>1,还可以正常运行,容错率为1

4个master节点,挂掉1个,3>1,还可以正常运行,但是当挂掉2个时,2=2,不过半,容错率依然为1


如果创建集群时设置slave为1个

--cluster-replicas 1

则当总节点少于6个时会有如下报错:

  1. [root@wzy-cloud 7000]# redis-cli --cluster create 0.0.0.0:7000 0.0.0.0:7001 0.0.0.0:7002 --cluster-replicas 1 -a wzy123
  2. Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe.
  3. *** ERROR: Invalid configuration for cluster creation.
  4. *** Redis Cluster requires at least 3 master nodes.
  5. *** This is not possible with 3 nodes and 1 replicas per node.
  6. *** At least 6 nodes are required.

 

所以集群搭建至少需要6个节点哦~

二、redis集群开始搭建

首先尼,由于条件限制,我只有一台服务器,所以节点ip都一致,只是端口不同,有条件的朋友可以多ip哦。

我们先在redis根目录下

mkdir redis_cluster

并在redis_cluster目录下分别创建7000、7001、7002、7003、7004、7005,并将redis.conf文件cp到这几个文件夹下

  1. [root@wzy-cloud redis_cluster]# pwd
  2. /usr/local/redis/redis_cluster
  3. [root@wzy-cloud redis_cluster]# ll
  4. total 24
  5. drwxr-xr-x 2 root root 4096 Jul 17 21:43 7000
  6. drwxr-xr-x 2 root root 4096 Jul 17 16:36 7001
  7. drwxr-xr-x 2 root root 4096 Jul 17 16:36 7002
  8. drwxrwxr-x 2 root root 4096 Jul 17 16:37 7003
  9. drwxrwxr-x 2 root root 4096 Jul 17 16:37 7004
  10. drwxrwxr-x 2 root root 4096 Jul 17 16:38 7005
  1. cp redis.conf redis_cluster/7000
  2. cp redis.conf redis_cluster/7001
  3. cp redis.conf redis_cluster/7002
  4. cp redis.conf redis_cluster/7003
  5. cp redis.conf redis_cluster/7004
  6. cp redis.conf redis_cluster/7005

然后修改各个文件夹中的redis.conf

  1. port 7000 //端口7000,7002,7003
  2. bind 本机ip //默认ip为127.0.0.1 需要改为其他节点机器可访问的ip 否则创建集群时无法访问对应的端口,无法创建集群,我这里是注释掉了,不知道0.0.0.0行不行
  3. daemonize yes //redis后台运行
  4. pidfile /var/run/redis_7000.pid //pidfile文件对应7000,7001,7002
  5. cluster-enabled yes //开启集群 把注释#去掉
  6. cluster-config-file nodes_7000.conf //集群的配置 配置文件首次启动自动生成 7000,7001,7002
  7. cluster-node-timeout 15000 //请求超时 默认15秒,可自行设置
  8. appendonly yes //aof日志开启 有需要就开启,它会每次写操作都记录一条日志 

然后分别启动这6个节点,也可以写个shell一键搞定~

  1. [root@wzy-cloud redis]# cat startRedis.sh
  2. redis-server /usr/local/redis/redis_cluster/7000/redis.conf
  3. redis-server /usr/local/redis/redis_cluster/7001/redis.conf
  4. redis-server /usr/local/redis/redis_cluster/7002/redis.conf
  5. redis-server /usr/local/redis/redis_cluster/7003/redis.conf
  6. redis-server /usr/local/redis/redis_cluster/7004/redis.conf
  7. redis-server /usr/local/redis/redis_cluster/7005/redis.conf
  8. [root@wzy-cloud redis]# ./startRedis.sh
  9. [root@wzy-cloud redis]# ps -ef|grep redis
  10. root 6304 1 0 21:44 ? 00:00:00 redis-server 0.0.0.0:7000 [cluster]
  11. root 6306 1 0 21:44 ? 00:00:00 redis-server 0.0.0.0:7001 [cluster]
  12. root 6308 1 0 21:44 ? 00:00:00 redis-server 0.0.0.0:7002 [cluster]
  13. root 6310 1 0 21:44 ? 00:00:00 redis-server 0.0.0.0:7003 [cluster]
  14. root 6318 1 0 21:44 ? 00:00:00 redis-server 0.0.0.0:7004 [cluster]
  15. root 6320 1 0 21:44 ? 00:00:00 redis-server 0.0.0.0:7005 [cluster]
  16. root 6345 4985 0 21:44 pts/0 00:00:00 grep --color=auto redi

所有节点启动好后,输入以下命令创建集群,注意ip哦,是你的本地ip

注意:如果输入命令后没有反应,那可能是防火墙的关系,关掉防火墙就好了

[root@wzy-cloud redis]# redis-cli --cluster create 114.116.35.252:7000 114.116.35.252:7001 114.116.35.252:7002 114.116.35.252:7003 114.116.35.252:7004 114.116.35.252:7005  --cluster-replicas 1 

如果加了密码,那么创建集群时就需要加-a wzy123参数

[root@wzy-cloud redis]# redis-cli --cluster create  114.116.35.252:7000  114.116.35.252:7001  114.116.35.252:7002  114.116.35.252:7003  114.116.35.252:7004  114.116.35.252:7005  --cluster-replicas 1 -a wzy123

不然会报以下错误

[ERR] Node  114.116.35.252:7000 NOAUTH Authentication required.

输入创建集群的命令后会出现以下提示,注意Can I set the above configuration? (type 'yes' to accept): yes,该处请输入yes,不然好像分配不了哈希槽(别人说的,没试)

  1. [root@wzy-cloud redis_cluster]# redis-cli --cluster create 114.116.35.252:7000 114.116.35.252:7001 114.116.35.252:7002 114.116.35.252:7003 114.116.35.252:7004 114.116.35.252:7005 --cluster-replicas 1 -a wzy123
  2. Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe.
  3. >>> Performing hash slots allocation on 6 nodes...
  4. Master[0] -> Slots 0 - 5460
  5. Master[1] -> Slots 5461 - 10922
  6. Master[2] -> Slots 10923 - 16383
  7. Adding replica 114.116.35.252:7003 to 114.116.35.252:7000
  8. Adding replica 114.116.35.252:7004 to 114.116.35.252:7001
  9. Adding replica 114.116.35.252:7005 to 114.116.35.252:7002
  10. >>> Trying to optimize slaves allocation for anti-affinity
  11. [WARNING] Some slaves are in the same host as their master
  12. M: 4e571c020d1f2cca020132a9adfdea2a367da21d 114.116.35.252:7000
  13. slots:[0-5460] (5461 slots) master
  14. M: 838382153a78260e274c1d2d11a105dd3986a223 114.116.35.252:7001
  15. slots:[5461-10922] (5462 slots) master
  16. M: e86d01e92214015304461a104a9f14e3cedc7829 114.116.35.252:7002
  17. slots:[10923-16383] (5461 slots) master
  18. S: 22b1f3d83f068973c6e8a5d0b9e87c0c1b950594 114.116.35.252:7003
  19. replicates 838382153a78260e274c1d2d11a105dd3986a223
  20. S: 5248474e122d745b7e929a2705da210d3d150b4c 114.116.35.252:7004
  21. replicates e86d01e92214015304461a104a9f14e3cedc7829
  22. S: b3d20a419df22b4c9f4fe14c1fda22c2920c5c11 114.116.35.252:7005
  23. replicates 4e571c020d1f2cca020132a9adfdea2a367da21d
  24. Can I set the above configuration? (type 'yes' to accept): yes

输完yes后,会出现如下提示,[OK] All 16384 slots covered.说明成功啦

  1. Can I set the above configuration? (type 'yes' to accept): yes
  2. >>> Nodes configuration updated
  3. >>> Assign a different config epoch to each node
  4. >>> Sending CLUSTER MEET messages to join the cluster
  5. Waiting for the cluster to join
  6. .....
  7. >>> Performing Cluster Check (using node 0.0.0.0:7000)
  8. M: 4e571c020d1f2cca020132a9adfdea2a367da21d 114.116.35.252:7000
  9. slots:[0-5460] (5461 slots) master
  10. 1 additional replica(s)
  11. S: b3d20a419df22b4c9f4fe14c1fda22c2920c5c11 114.116.35.252:7005
  12. slots: (0 slots) slave
  13. replicates 4e571c020d1f2cca020132a9adfdea2a367da21d
  14. M: e86d01e92214015304461a104a9f14e3cedc7829 114.116.35.252:7002
  15. slots:[10923-16383] (5461 slots) master
  16. 1 additional replica(s)
  17. S: 22b1f3d83f068973c6e8a5d0b9e87c0c1b950594 114.116.35.252:7003
  18. slots: (0 slots) slave
  19. replicates 838382153a78260e274c1d2d11a105dd3986a223
  20. M: 838382153a78260e274c1d2d11a105dd3986a223 114.116.35.252:7001
  21. slots:[5461-10922] (5462 slots) master
  22. 1 additional replica(s)
  23. S: 5248474e122d745b7e929a2705da210d3d150b4c 114.116.35.252:7004
  24. slots: (0 slots) slave
  25. replicates e86d01e92214015304461a104a9f14e3cedc7829
  26. [OK] All nodes agree about slots configuration.
  27. >>> Check for open slots...
  28. >>> Check slots coverage...
  29. [OK] All 16384 slots covered.

验证一下

  1. [root@wzy-cloud redis_cluster]# redis-cli -c -p 7000
  2. 127.0.0.1:7000> auth wzy123
  3. OK
  4. 127.0.0.1:7000> cluster nodes
  5. b3d20a419df22b4c9f4fe14c1fda22c2920c5c11 114.116.35.252:7005@17005 slave 4e571c020d1f2cca020132a9adfdea2a367da21d 0 1563378113000 6 connected
  6. e86d01e92214015304461a104a9f14e3cedc7829 114.116.35.252:7002@17002 master - 0 1563378115000 3 connected 10923-16383
  7. 22b1f3d83f068973c6e8a5d0b9e87c0c1b950594 114.116.35.252:7003@17003 slave 838382153a78260e274c1d2d11a105dd3986a223 0 1563378116490 4 connected
  8. 838382153a78260e274c1d2d11a105dd3986a223 114.116.35.252:7001@17001 master - 0 1563378114486 2 connected 5461-10922
  9. 5248474e122d745b7e929a2705da210d3d150b4c 114.116.35.252:7004@17004 slave e86d01e92214015304461a104a9f14e3cedc7829 0 1563378115488 5 connected
  10. 4e571c020d1f2cca020132a9adfdea2a367da21d 114.116.35.252:7000@17000 myself,master - 0 1563378114000 1 connected 0-5460
  11. 114.116.35.252:7000> set qwe 111
  12. OK
  13. 114.116.35.252:7000>exit
  14. [root@wzy-cloud 7000]# redis-cli -c -p 7003
  15. 114.116.35.252:7003> auth wzy123
  16. OK
  17. 114.116.35.252:7003> get qwe
  18. -> Redirected to slot [757] located at 127.0.0.1:7000
  19. (error) NOAUTH Authentication required.
  20. 114.116.35.252:7000> auth wzy123
  21. OK
  22. 127.0.0.1:7000> get qwe
  23. "111"
  24. 127.0.0.1:7000>

 

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/我家小花儿/article/detail/791529
推荐阅读
相关标签
  

闽ICP备14008679号