Redis安裝和遠程訪問

Redis安裝和遠程訪問

安裝

下載地址:
redis下載地址

下面是官方的編譯安裝方法:

$ wget http://download.redis.io/releases/redis-3.2.5.tar.gz
$ tar xzf redis-3.2.5.tar.gz
$ cd redis-3.2.5
$ make

啓動redis:

$ src/redis-server

利用客戶端命令測試:

$ src/redis-cli
redis> set foo bar
OK
redis> get foo
"bar"

此時的redis使用默認配置,只能本地訪問。

開啓遠程訪問

我們使用redis,一般不在本地使用,而在生產環境的機器上內網遠程訪問。

當我們使用默認配置時,用Python API會報這樣的異常

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.

原因是redis默認開啓了protected mode。
從以上報告的異常可以發現,有3種方法解決這個問題:
1. 直接關閉protected mode:在redis.conf配置文件中的protected mode選項(需要重啓redis),或者在redis-cli執行CONFIG SET protected-mode no或者在啓動命令後加上參數--protected-mode no
2. 設置認證密碼:使用命令config set requirepass <你的密碼>
3. 綁定IP:修改redis.conf配置文件,加入bind <你允許訪問的IP>

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章