尝试连接阿里云上的 redis 服务:
In [10]: import redis
In [11]: r = redis.Redis(host='47.107.180.248',port=6379,db=0)
In [12]: r.set('name','ruiyang')
报错信息如下:
ResponseError: DENIED Redis is running in protected mode because protected mode is enabled, no bind address was specified, no authentication password is requested to clients. In this mode connections are only accepted from the loopback interface. If you want to connect from external computers to Redis you may adopt one of the following solutions: 1) Just disable protected mode sending the command 'CONFIG SET protected-mode no' from the loopback interface by connecting to Redis from the same host the server is running, however MAKE SURE Redis is not publicly accessible from internet if you do so. Use CONFIG REWRITE to make this change permanent. 2) Alternatively you can just disable the protected mode by editing the Redis configuration file, and setting the protected mode option to 'no', and then restarting the server. 3) If you started the server manually just for testing, restart it with the '--protected-mode no' option. 4) Setup a bind address or an authentication password. NOTE: You only need to do one of the above things in order for the server to start accepting connections from the outside.
意为保护模式,在非本地连接的情况下不能访问。
根据: https://hans007.github.io/redis/2016/12/07/redis-protected-03
最偷懒的方式将配置文件里面的 保护模式置为 False 即可。
使用ps和netstat检查一下redis是否在运行 端口是否在监听:
ps -ef|grep redis
netstat -tunpl |grep 6379
发现这两项都是正常的。
但是我使用 find / -name redis.conf 没有找到配置文件,猜想可能是启动了redis 之后将配置文件删除了
Kill 掉 redis 的进程,然后重新进行安装:
(demo) [root@furuiyang etc]$ ps aux | grep redis
root 16451 0.0 0.0 16304 1352 pts/2 S+ 09:57 0:00 redis-cli
root 16582 0.0 0.0 136972 1812 ? Sl 2018 337:26 redis-server *:6379
root 24519 0.0 0.0 112720 984 pts/3 S+ 10:48 0:00 grep --color=auto redis
(demo) [root@furuiyang etc]$ sudo kill -9 16582
(demo) [root@furuiyang etc]$ ps aux | grep redis
root 16451 0.0 0.0 16304 1352 pts/2 S+ 09:57 0:00 redis-cli
root 24556 0.0 0.0 112720 984 pts/3 S+ 10:49 0:00 grep --color=auto redis
(demo) [root@furuiyang 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
重新安装 redis:
wget et http://download.redis.io/releases/redis-3.2.9.tar.gz
tar zxvf redis-3.2.9.tar.gz
cd redis-3.2.9/
make && make install
这时,在相应的安装目录下 ,就重新拥有了 redis.conf 文件。
根据: https://blog.csdn.net/Enjolras_fuu/article/details/79661474
以及 https://hans007.github.io/redis/2016/12/07/redis-protected-03
对 redis.conf 文件进行修改
先将配置文件移动到比较普遍的位置 :
(demo) [root@furuiyang redis-3.2.9]$ sudo cp /usr/local/etc/redis-3.2.9/redis.conf /etc/redis/
cp: 无法创建普通文件"/etc/redis/": 不是目录
(demo) [root@furuiyang redis-3.2.9]$ mkdir -p /etc/redis/
(demo) [root@furuiyang redis-3.2.9]$ sudo cp /usr/local/etc/redis-3.2.9/redis.conf /etc/redis/
需要远程访问,将绑定端口 127.0.0.1 注释掉:
bind 127.0.0.1
端口设置 默认为 6379
port 6379
以守护进程运行
daemonize yes
数据文件以及数据文件的存储路径
dbfilename dump.rdb
dir /var/lib/redis # 默认为 ./ 表示当前路径
日志文件的路径 (redis目录如不存在需要自行创建)
logfile /etc/redis/redis-server.log
redis 数据库的个数(默认为 16 个)
database 16
不设置 redis 的保护模式
protected-mode no
启动redis服务
redis-server /etc/redis/redis.conf
启动报错:
30495:C 05 Aug 11:27:08.051 # Can't chdir to '/var/lib/redis': No such file or directory
按照报错的提示创建文件夹:
mkdir -p /var/lib/redis
开启成功
再次尝试远程连接:
In [14]: r = redis.Redis(host='47.107.180.248',port=6379,db=0)
In [15]: r.set('name','ruiyang')
Out[15]: True
连接成功