今天服務(wù)器安裝了redis,為了安全設(shè)置一下訪問redis-server的密碼。
推薦:《redis教程》
一、查找redis.conf文件
我們服務(wù)器已經(jīng)安裝了redis,現(xiàn)在通過命令查看下redis的進(jìn)程:
[root@lnp ~]# ps -aux|grep redis root 7374 0.0 0.0 145312 7524 ? Ssl 16:37 0:00 redis-server 192.168.17.105:6379 root 10692 0.0 0.0 112724 984 pts/7 S+ 16:54 0:00 grep --color=auto redis
可以看到我們的redis-server的服務(wù)地址為192.168.17.105,端口為6379,對(duì)外訪問的時(shí)候需要指定對(duì)應(yīng)的IP和端口:
redis-cli -h 192.168.17.105 -p 6379
查找redis安裝目錄
> whereis redis redis: /usr/local/redis
我們可以看到redis在該目錄下安裝,然后找到配置文件redis.conf
> find /usr/local/redis/ -name redis.conf /usr/local/redis/etc/redis.conf
修改配置文件:
vim redis.conf
改該配置文件即可:
# requirepass foobared requirepass 123 指定密碼123
最后一步,重新加載配置文件即可:
redis-server /usr/local/redis/etc/redis.conf
二、連接測(cè)試
通過密碼-a訪問:
> redis-cli -h 192.168.17.105 -p 6379 -a 123
運(yùn)行結(jié)果:
[root@lnp etc]# redis-cli Could not connect to Redis at 127.0.0.1:6379: Connection refused Could not connect to Redis at 127.0.0.1:6379: Connection refused not connected> exit [root@lnp etc]# redis-cli -h 192.168.17.105 -p 6379 192.168.17.105:6379> keys * (error) NOAUTH Authentication required. 192.168.17.105:6379> exit [root@lnp etc]# redis-cli -h 192.168.17.105 -p 6379 -a 123 Warning: Using a password with '-a' option on the command line interface may not be safe. 192.168.17.105:6379> keys * (empty list or set) 192.168.17.105:6379> exit