1. 程式人生 > >redis 單機或叢集 設定密碼

redis 單機或叢集 設定密碼

一單機版

開啟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>

<bean id="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>