#1、进入redis配置文件目录下
cd /home/test/redis_qiye/
[test@10 redis_qiye]$ ll
total 32
drwxr-xr-x 2 test test 137 Jun 27 17:59 bin
drwxr-xr-x 2 test test 32 Jun 28 14:09 data
-rwxrwxr-x 1 test test 471 Jun 27 17:59 load.sh
drwxr-xr-x 2 test test 33 Jun 27 17:59 logs
-rw-r--r-- 1 test test 25474 Jun 27 17:59 redis90.conf
#2、查看配置文件是否设置密码:无
[test@10 redis_qiye]$ cat redis90.conf|grep requirepass
# If the master is password protected (using the "requirepass" configuration
# requirepass foobared
#3、设置redis密码 test
[test@10 redis_qiye]$ vim redis90.conf
requirepass test
#4、查看当前redis进程,关闭进行重启
[test@10 redis_qiye]$ ps -ef|grep redis
test 14007 1 0 14:20 ? 00:00:00 ./bin/redis-server redis90.conf
test 14063 10369 0 14:21 pts/0 00:00:00 grep --color=auto redis
# 关闭,方式1 kill, 方式2 进入终端shutdown
[test@10 redis_qiye]$ kill -9 14007
或
[test@10 redis_qiye]$ ./bin/redis-cli -h 127.0.0.1 -p 6390
redis 127.0.0.1:6390> shutdown
redis 127.0.0.1:6390> exit
# 查看密码是否已设置,进行重启
[test@10 redis_qiye]$ ps -ef|grep redis
test 14143 10369 0 14:22 pts/0 00:00:00 grep --color=auto redis
[test@10 redis_qiye]$ cat redis90.conf |grep requirepass
# If the master is password protected (using the "requirepass" configuration
# requirepass foobared
requirepass test
#启动redis
[test@10 redis_qiye]$ ./bin/redis-server redis90.conf
[test@10 redis_qiye]$ ps -ef|grep redis
test 14709 1 0 14:25 ? 00:00:00 ./bin/redis-server redis90.conf
test 14715 10369 0 14:25 pts/0 00:00:00 grep --color=auto redis
#测试密码是否生效
#1.无密码进入,服务不可用
[test@10 redis_qiye]$ ./bin/redis-cli -h 127.0.0.1 -p 6390
redis 127.0.0.1:6390> keys *
(error) ERR operation not permitted
#2.输入密码进入,服务可用
[test@10 redis_qiye]$ ./bin/redis-cli -h 127.0.0.1 -p 6390 -a test
redis 127.0.0.1:6390> set test_name test
OK
参考资料:https://cloud.tencent.com/developer/article/2133116?from=15425