Redis configuration authentication password (reproduced)

Redis configuration password

1. Configure through the configuration file
The redis configuration file installed by yum is usually in /etc/redis.conf, open the configuration file to find

[plain]  view plain copy  
 
  1. #requirepass foobared  

Remove the comment before the line, and change the password to the desired password, save the file

[plain]  view plain copy  
 
  1. requirepass myRedis  

restart redis

[plain]  view plain copy  
 
  1. sudo service redis restart  
  2. #or  
  3. sudo service redis stop  
  4. sudo redis-server /etc/redis.conf  

At this time, I try to log in to redis and find that I can log in, but executing the specific command prompts that the operation is not allowed.

[plain]  view plain copy  
 
  1. redis-cli -h 127.0.0.1 -p 6379  
  2. redis 127.0.0.1:6379>  
  3. redis 127.0.0.1:6379> keys *  
  4. (error) ERR operation not permitted  
  5. redis 127.0.0.1:6379> select 1  
  6. (error) ERR operation not permitted  
  7. redis 127.0.0.1:6379[1]>   

Try to log in with the password and execute the specific command to see that it can be successfully executed

[plain]  view plain copy  
 
  1. redis-cli -h 127.0.0.1 -p 6379 -a myRedis  
  2. redis 127.0.0.1:6379> keys *  
  3. 1) "myset"  
  4. 2) "mysortset"  
  5. redis 127.0.0.1:6379> select 1  
  6. OK  
  7. redis 127.0.0.1:6379[1]> config get requirepass  
  8. 1) "requirepass"  
  9. 2) "myRedis"  


2. Configuration via command line

[plain]  view plain copy  
 
  1. redis 127.0.0.1:6379[1]> config set requirepass my_redis  
  2. OK  
  3. redis 127.0.0.1:6379[1]> config get requirepass  
  4. 1) "requirepass"  
  5. 2) "my_redis"  

无需重启redis
使用第一步中配置文件中配置的老密码登录redis,会发现原来的密码已不可用,操作被拒绝

 

[plain]  view plain  copy
 
  1. redis-cli -h 127.0.0.1 -p 6379 -a myRedis  
  2. redis 127.0.0.1:6379> config get requirepass  
  3. (error) ERR operation not permitted  

使用修改后的密码登录redis,可以执行相应操作

[plain]  view plain  copy
 
  1. redis-cli -h 127.0.0.1 -p 6379 -a my_redis  
  2. redis 127.0.0.1:6379> config get requirepass  
  3. 1) "requirepass"  
  4. 2) "my_redis  

尝试重启一下redis,用新配置的密码登录redis执行操作,发现新的密码失效,redis重新使用了配置文件中的密码

[plain]  view plain  copy
 
  1. sudo service redis restart  
  2. Stopping redis-server:                                     [  OK  ]  
  3. Starting redis-server:                                     [  OK  ]  
  4. redis-cli -h 127.0.0.1 -p 6379 -a my_redis  
  5. redis 127.0.0.1:6379> config get requirepass  
  6. (error) ERR operation not permitted  
  7. redis-cli -h 127.0.0.1 -p 6379 -a myRedis  
  8. redis 127.0.0.1:6379> config get requirepass  
  9. 1) "requirepass"  
  10. 2) "myRedis"  


除了在登录时通过 -a 参数制定密码外,还可以登录时不指定密码,而在执行操作前进行认证。

[plain]  view plain  copy
 
  1. redis-cli -h 127.0.0.1 -p 6379  
  2. redis 127.0.0.1:6379> config get requirepass  
  3. (error) ERR operation not permitted  
  4. redis 127.0.0.1:6379> auth myRedis  
  5. OK  
  6. redis 127.0.0.1:6379> config get requirepass  
  7. 1) "requirepass"  
  8. 2) "myRedis"  


3.master配置了密码,slave如何配置

If the master is configured with a password, the slave must also configure the corresponding password parameters, otherwise normal replication cannot be performed.
Find the following line in the configuration file of slave, remove the comment, and change the password

[plain]  view plain copy  
 
  1. #masterauth  mstpassword  

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=326945328&siteId=291194637