3
回答
python远程连接redis服务器
利用AWS快速构建适用于生产的无服务器应用程序,免费试用12个月>>>   

import redis
r = redis.StrictRedis(host=' 47.94.223.129',port=6379,db=0,password='*****')
r.flushdb()

出现了下面的错误

Traceback (most recent call last):
  File "run.py", line 3, in <module>
    r.flushdb()
  File "C:\Users\kalao\AppData\Local\Programs\Python\Python36\lib\site-packages\redis\client.py", line 652, in flushdb
    return self.execute_command('FLUSHDB')
  File "C:\Users\kalao\AppData\Local\Programs\Python\Python36\lib\site-packages\redis\client.py", line 578, in execute_command
    connection.send_command(*args)
  File "C:\Users\kalao\AppData\Local\Programs\Python\Python36\lib\site-packages\redis\connection.py", line 565, in send_command
    self.send_packed_command(self.pack_command(*args))
  File "C:\Users\kalao\AppData\Local\Programs\Python\Python36\lib\site-packages\redis\connection.py", line 540, in send_packed_command
    self.connect()
  File "C:\Users\kalao\AppData\Local\Programs\Python\Python36\lib\site-packages\redis\connection.py", line 443, in connect
    raise ConnectionError(self._error_message(e))
redis.exceptions.ConnectionError: Error 11001 connecting to  47.94.223.129:6379. getaddrinfo failed.

举报
kalao
发帖于3个月前 3回/135阅

步骤:

1.修改redis-server的配置

在运行redis-server的主机上执行命令ipconfig,找到ip地址,假设为192.168.1.100

在redis.windows.conf 中找到bind 127.0.0.1这行并修改为bind 192.168.1.100

redis.windows.conf 的内容摘录如下:

 

# IF YOU ARE SURE YOU WANT YOUR INSTANCE TO LISTEN TO ALL THE INTERFACES
# JUST COMMENT THE FOLLOWING LINE.
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
bind 192.168.1.100

# Protected mode is a layer of security protection, in order to avoid that
# Redis instances left open on the internet are accessed and exploited.
#
# When protected mode is on and if:

2.运行redis-server

在命令窗口中切换到redis所在的目录,执行命令:redis-server.exe redis.windows.conf 。

3.远程连接

需要安装redis-py:

pip install redis

Python远程连接代码如下:

>>> import redis
>>> r = redis.Redis(host='192.168.1.100')
>>> r.set('name', 'xiemanR')
True
>>> r.get('name')
b'xiemanR'
>>>

host参数填上运行redis-server机器的ip,本文为192.168.1.100。

运行redis的set和get函数没错误的话说明远程连接成功了。

顶部