1. 程式人生 > >Python遠程連接Redis

Python遠程連接Redis

order access 互聯網 使用 pub local 重新啟動 get write

import redis
r=redis.Redis(host=‘192.168.56.102‘,port=6379,db=0,password=‘jinxfredis‘ )
r.set(‘name‘,‘jin‘)
print(r.get(‘name‘))

Python遠程連接Redis時報如下錯誤:

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.

拒絕的Redis在保護模式下運行,因為保護模式是啟用的,沒有指定綁定地址,沒有向客戶端請求身份驗證密碼。在這種模式下,連接只能從環回接口接受。如果你想從外部計算機連接到復述,你可能采取的解決方案:

1)只是禁用保護模式發送命令的配置設置保護模式沒有從loopback接口連接到復述同一主機服務器正在運行,然而確保復述,不是公開從互聯網訪問如果你這樣做。使用配置重寫使此更改永久存在。

2)也可以通過編輯Redis配置文件,將protected模式選項設置為no,然後重新啟動服務器,從而禁用protected模式。

3)如果您只是為了測試而手動啟動服務器,那麽使用“—保護模式no”選項重新啟動服務器。

4)設置綁定地址或身份驗證密碼。註意:為了讓服務器開始接受來自外部的連接,您只需要做上述的一件事。

按照上面2-4設置後,

bind 0.0.0.0

protected-mode no

requirepass password

從新啟動Redis,還是報相同的錯誤。。。

原來Redis不指定配置文件,就按照默認的設置啟動,所以重新啟動Redis時,設置指定的配置文件,遠程就可以正常訪問了

[root@localhost ~]# redis-server redis-3.2/redis.conf 

Python遠程連接Redis