redis 单机或集群 设置密码

来源:互联网 发布:python技术手册第三版 编辑:程序博客网 时间:2024/05/30 04:57

一单机版

打开redis.conf文件,搜索requirepass关键字,关注标记的那一行,#requirepass foobared。设置密码的方法就是去掉注释的#,把foobared替换成自己的密码即可,例如将密码设置为123456:如图


使用redus-cli客户端访问需要输入密码参数 redis-cli -a 123456,如图


spring-data-redis中的配置

       <bean id="jedisConnectionFactory" class="org.springframework.data.redis.connection.jedis.JedisConnectionFactory">              <property name="hostName" value="127.0.0.1" />              <property name="port" value="6379"/>              <property name="poolConfig" ref="jedisPoolConfig" />              <property name="usePool" value="true"/>              <property name="password" value="123456"/>       </bean>


二集群版

1.如果是使用redis-trib.rb工具构建集群,集群构建完成前不要配置密码,集群构建完毕再通过config set + config rewrite命令逐个机器设置密码
2.如果对集群设置密码,那么requirepass和masterauth都需要设置,否则发生主从切换时,就会遇到授权问题,可以模拟并观察日志
3.各个节点的密码都必须一致,否则Redirected就会失败

方式一:修改所有Redis集群中的redis.conf配置文件: 
masterauth passwd123 
requirepass passwd123 
说明:这种方式需要重新启动各节点


方式二:进入各个实例进行设置: 
./redis-cli -c -p 7000 
config set masterauth passwd123 
config set requirepass passwd123 
config rewrite 
之后分别使用./redis-cli -c -p 7001,./redis-cli -c -p 7002…..命令给各节点设置上密码 
注意:各个节点密码都必须一致,否则Redirected就会失败, 推荐这种方式,这种方式会把密码写入到redis.conf里面去,且不用重启

设置密码之后如果需要使用redis-trib.rb的各种命令 
如:redis-trib.rb check 127.0.0.1,则会报错ERR] Sorry, can’t connect to node 127.0.0.1:7000 
解决办法: 
vim /usr/lib/ruby/gems/1.8/gems/redis-3.3.0/lib/redis/client.rb,然后修改passord

class Client    DEFAULTS = {      :url => lambda { ENV["REDIS_URL"] },      :scheme => "redis",      :host => "127.0.0.1",      :port => 6379,      :path => nil,      :timeout => 5.0,      :password => "passwd123",      :db => 0,      :driver => nil,      :id => nil,      :tcp_keepalive => 0,      :reconnect_attempts => 1,      :inherit_socket => false    }
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16

带密码访问集群

./redis-cli -c -p 7000 -a passwd123
在程序里面使用最新的 redis.clients架包,老版本没有提供密码访问的方式
<dependency>          <groupId>redis.clients</groupId>          <artifactId>jedis</artifactId>          <version>2.9.0</version>          <type>jar</type>      </dependency>

spring-data-redis中的配置

<!--配置RedisClusterConfiguration--><bean id="redisClusterConfiguration" class="org.springframework.data.redis.connection.RedisClusterConfiguration">       <property name="maxRedirects" value="5"></property>       <property name="clusterNodes">              <set>                     <bean class="org.springframework.data.redis.connection.RedisNode">                            <constructor-arg name="host" value="127.0.0.1"/>                            <constructor-arg name="port" value="6379"/>                     </bean>                     <bean class="org.springframework.data.redis.connection.RedisNode">                            <constructor-arg name="host" value="127.0.0.1"/>                            <constructor-arg name="port" value="6380"/>                     </bean>                     <bean class="org.springframework.data.redis.connection.RedisNode">                            <constructor-arg name="host" value="127.0.0.1"/>                            <constructor-arg name="port" value="6381"/>                     </bean>                     <bean class="org.springframework.data.redis.connection.RedisNode">                            <constructor-arg name="host" value="127.0.0.1"/>                            <constructor-arg name="port" value="6382"/>                     </bean>                     <bean class="org.springframework.data.redis.connection.RedisNode">                            <constructor-arg name="host" value="127.0.0.1"/>                            <constructor-arg name="port" value="6383"/>                     </bean>                     <bean class="org.springframework.data.redis.connection.RedisNode">                            <constructor-arg name="host" value="127.0.0.1"/>                            <constructor-arg name="port" value="6384"/>                     </bean>              </set>       </property></bean>

<beanid="jedisConnectionFactory"class="org.springframework.data.redis.connection.jedis.JedisConnectionFactory">

       <constructor-arg name="poolConfig" ref="jedisPoolConfig"/>       <constructor-arg name="clusterConfig" ref="redisClusterConfiguration"/>       <property name="usePool" value="true"/>       <property name="password" value="123456"/></bean>

原创粉丝点击