1. Configuration through configuration files
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"
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.
- 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
Use the modified password to log in to redis, you can perform corresponding operations
- 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
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
- 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"
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.
- 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. 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
- #masterauth mstpassword