redis configuration password

1. Configuration through configuration files
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"  

There is no need to restart redis
. Use the old password configured in the configuration file in the first step to log in to redis. You will find that the original password is unavailable and the operation is rejected.

 

[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  

Use the modified password to log in to redis, you can perform corresponding operations

[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  

Try restarting redis, log in to redis with the newly configured password, and find that the new password is invalid, and redis reuses the password in the configuration file

[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"  


In addition to specifying a password through the -a parameter when logging in, you can also log in without specifying a password and perform authentication before performing an operation.

[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. The master is configured with a password, how to configure the 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://43.154.161.224:23101/article/api/json?id=326329652&siteId=291194637