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
- #requirepass foobared
Remove the comment before the line, and change the password to the desired password, save the file
- requirepass myRedis
restart redis
- sudo service redis restart
- #or
- sudo service redis stop
- 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.
- redis-cli -h 127.0.0.1 -p 6379
- redis 127.0.0.1:6379>
- redis 127.0.0.1:6379> keys *
- (error) ERR operation not permitted
- redis 127.0.0.1:6379> select 1
- (error) ERR operation not permitted
- 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
- redis-cli -h 127.0.0.1 -p 6379 -a myRedis
- redis 127.0.0.1:6379> keys *
- 1) "myset"
- 2) "mysortset"
- redis 127.0.0.1:6379> select 1
- OK
- redis 127.0.0.1:6379[1]> config get requirepass
- 1) "requirepass"
- 2) "myRedis"
2. Configuration via command line
- redis 127.0.0.1:6379[1]> config set requirepass my_redis
- OK
- redis 127.0.0.1:6379[1]> config get requirepass
- 1) "requirepass"
- 2) "my_redis"
无需重启redis
使用第一步中配置文件中配置的老密码登录redis,会发现原来的密码已不可用,操作被拒绝
- redis-cli -h 127.0.0.1 -p 6379 -a myRedis
- redis 127.0.0.1:6379> config get requirepass
- (error) ERR operation not permitted
使用修改后的密码登录redis,可以执行相应操作
- redis-cli -h 127.0.0.1 -p 6379 -a my_redis
- redis 127.0.0.1:6379> config get requirepass
- 1) "requirepass"
- 2) "my_redis
尝试重启一下redis,用新配置的密码登录redis执行操作,发现新的密码失效,redis重新使用了配置文件中的密码
- sudo service redis restart
- Stopping redis-server: [ OK ]
- Starting redis-server: [ OK ]
- redis-cli -h 127.0.0.1 -p 6379 -a my_redis
- redis 127.0.0.1:6379> config get requirepass
- (error) ERR operation not permitted
- redis-cli -h 127.0.0.1 -p 6379 -a myRedis
- redis 127.0.0.1:6379> config get requirepass
- 1) "requirepass"
- 2) "myRedis"
除了在登录时通过 -a 参数制定密码外,还可以登录时不指定密码,而在执行操作前进行认证。
- redis-cli -h 127.0.0.1 -p 6379
- redis 127.0.0.1:6379> config get requirepass
- (error) ERR operation not permitted
- redis 127.0.0.1:6379> auth myRedis
- OK
- redis 127.0.0.1:6379> config get requirepass
- 1) "requirepass"
- 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
- #masterauth mstpassword