redis集群设置密码
1,如果是使用redis-trib.rb工具构建集群,集群构建完成前不要配置密码,集群构建完毕再通过config set + config rewrite命令逐个机器设置密码
2,如果对集群设置密码,那么requirepass和masterauth都需要设置,否则发生主从切换时,就会遇到授权问题,可以模拟并观察日志
3,各个节点的密码都必须一致,否则Redirected就会失败
方法:
[root@iZj6c7eeosj2t5vjw8rf4xZ redis_cluster]# redis-cli -c -p 7004
127.0.0.1:7004> config set masterauth frank
OK
127.0.0.1:7004> config set requirepass frank
OK
127.0.0.1:7004> CONFIG REWRITE
(error) NOAUTH Authentication required.
127.0.0.1:7004> auth frank
OK
127.0.0.1:7004> config rewrite
OK
127.0.0.1:7004> exit
[root@iZj6c7eeosj2t5vjw8rf4xZ redis_cluster]# redis-cli -c -p 7004 -a frank
4,设置密码之后如果需要使用redis-trib.rb的各种命令报错问题
如:
[root@iZj6c7eeosj2t5vjw8rf4xZ redis-3.2.1]# redis-trib.rb check 47.52.41.245:7003
[ERR] Sorry, can't connect to node 47.52.41.245:7003
解决办法:
找到建立集群时执行
gem install redis
生成的client.rb文件,如果不知道该文件在哪可以通过下面命令查找
[root@iZj6c7eeosj2t5vjw8rf4xZ redis-3.2.1]# find / -name client.rb -print
/tmp/frank/ruby/ruby-2.2.8/lib/xmlrpc/client.rb
/usr/local/lib/ruby/gems/2.2.0/gems/redis-4.0.0/lib/redis/client.rb
/usr/local/lib/ruby/2.2.0/xmlrpc/client.rb
我这边的那个文件是/usr/local/lib/ruby/gems/2.2.0/gems/redis-4.0.0/lib/redis/client.rb
然后修改该文件
[root@iZj6c7eeosj2t5vjw8rf4xZ redis-3.2.1]# vim /usr/local/lib/ruby/gems/2.2.0/gems/redis-4.0.0/lib/redis/client.rb
修改password对应的值然后保存,就可以了
require_relative "errors"
require "socket"
require "cgi"
class Redis
class Client
DEFAULTS = {
:url => lambda { ENV["REDIS_URL"] },
:scheme => "redis",
:host => "127.0.0.1",
:port => 6379,
:path => nil,
:timeout => 5.0,
:password => "frank",
:db => 0,
:driver => nil,
:id => nil,
:tcp_keepalive => 0,
:reconnect_attempts => 1,
:inherit_socket => false
}
attr_reader :options
重新运行redis-trib.rb命令
redis-trib.rb check 47.52.41.245:7003
>>> Performing Cluster Check (using node 47.52.41.245:7003)
S: cc86a24f3896ad7530e2687cf52582912f74b661 47.52.41.245:7003
slots: (0 slots) slave
replicates 908430b2bf63669898e9eaef79dd6c1b33c8c57a
M: 668397aba571ece85532b1eb1fccb42e4e33b1f2 116.196.65.198:7001