Python远程连接阿里云里面的redis报错信息为:
D:\PycharmProjects\db1901\venv\Scripts\python.exe D:/PycharmProjects/db1901/example09.py
Redis<ConnectionPool<Connection<host=39.98.xxx.xxx,port=6379,db=0>>>
Traceback (most recent call last):
File "D:/PycharmProjects/db1901/example09.py", line 13, in <module>
main()
File "D:/PycharmProjects/db1901/example09.py", line 7, in main
client.set('username','hellokitty',ex=300)
File "D:\PycharmProjects\db1901\venv\lib\site-packages\redis\client.py", line 1766, in set
return self.execute_command('SET', *pieces)
File "D:\PycharmProjects\db1901\venv\lib\site-packages\redis\client.py", line 875, in execute_command
conn = self.connection or pool.get_connection(command_name, **options)
File "D:\PycharmProjects\db1901\venv\lib\site-packages\redis\connection.py", line 1185, in get_connection
connection.connect()
File "D:\PycharmProjects\db1901\venv\lib\site-packages\redis\connection.py", line 561, in connect
self.on_connect()
File "D:\PycharmProjects\db1901\venv\lib\site-packages\redis\connection.py", line 637, in on_connect
auth_response = self.read_response()
File "D:\PycharmProjects\db1901\venv\lib\site-packages\redis\connection.py", line 752, in read_response
raise response
redis.exceptions.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.
Process finished with exit code 1
解决方案为:
1、查询内网的IP地址:ifconfig eth0
2、把redis服务器绑定在内网的IP地址上
redis-server --bind 172.26.181.xx > redis.log & --把redis服务绑定在阿里云内网IP地址上,然
后在后台启动(不加连接密码)
redis-server --bind 172.26.181.xx --requirepass 1qaz2wsx > redis.log & --把redis服务绑定
在阿里云内网IP
地址上,然后在后
台启动(加连接密
码)
如果你开了防火墙,下面的可以不用看了!!!!!!
可惜使用了上面的方法还是不行,反而报错:
D:\PycharmProjects\db1901\venv\Scripts\python.exe D:/PycharmProjects/db1901/example09.py
Redis<ConnectionPool<Connection<host=39.98.165.236,port=6379,db=0>>>
Traceback (most recent call last):
File "D:\PycharmProjects\db1901\venv\lib\site-packages\redis\connection.py", line 552, in connect
sock = self._connect()
File "D:\PycharmProjects\db1901\venv\lib\site-packages\redis\connection.py", line 609, in _connect
raise err
File "D:\PycharmProjects\db1901\venv\lib\site-packages\redis\connection.py", line 597, in _connect
sock.connect(socket_address)
TimeoutError: [WinError 10060] 由于连接方在一段时间后没有正确答复或连接的主机没有反应,连接尝试失败。
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "D:/PycharmProjects/db1901/example09.py", line 13, in <module>
main()
File "D:/PycharmProjects/db1901/example09.py", line 7, in main
client.set('username','hellokitty',ex=300)
File "D:\PycharmProjects\db1901\venv\lib\site-packages\redis\client.py", line 1766, in set
return self.execute_command('SET', *pieces)
File "D:\PycharmProjects\db1901\venv\lib\site-packages\redis\client.py", line 875, in execute_command
conn = self.connection or pool.get_connection(command_name, **options)
File "D:\PycharmProjects\db1901\venv\lib\site-packages\redis\connection.py", line 1185, in get_connection
connection.connect()
File "D:\PycharmProjects\db1901\venv\lib\site-packages\redis\connection.py", line 557, in connect
raise ConnectionError(self._error_message(e))
redis.exceptions.ConnectionError: Error 10060 connecting to 39.98.xxx.xxx:6379. 由于连接方在一段时间后没有正确答复或连接的主机没有反应,连接尝试失败。.
该怎么办呢?
机智的我发现原来是我没有打开阿里云服务器的防火墙!!!!!真是傻x了。。。。。原因是因为我昨天没配置完就和女朋友聊天去了。本来昨天开了防火墙的,关闭服务器以后就又关了。所以做了上面的配置以后还是不行。
上面这个错误的解决方案是:
systemctl start firewalld ---打开/启动防火墙
firewall-cmd --reload #重启firewall。双保险一下,防止防火墙没开好
firewall-cmd --list-ports #查看已经开放的端口,再看看端口有没有真的开放给外部访问