windows Redis綁定ip無效,Redis設置密碼無效,Windows Redis 配置不生效, Windows Redis requirepass不生效


windows Redis綁定ip無效,Redis設置密碼無效,Windows Redis 配置不生效,

Windows Redis requirepass不生效

 

>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>

©Copyright 蕃薯耀 2017年7月11日

http://www.cnblogs.com/fanshuyao/

 

一、Redis下載地址:

https://github.com/MicrosoftArchive/redis/releases

1、Redis-x64-3.2.100.msi 為安裝版

2、Redis-x64-3.2.100.zip 為壓縮包

 

二、由於我使用的是安裝版,本次問題也是安裝版的問題

1、安裝后的目錄



 

2、安裝版的Redis安裝后服務會自動啟動。

 

 

三、問題所在:

由於安裝版的Redis服務自啟動,是直接通過redis-server.exe啟動的,但是,啟動時並沒有加載Redis的配置文件(redis.windows.conf),導致redis 中bind配置和密碼設置不生效。這導致我折騰了很久,后來才意識到這個問題。

 

四、Redis自啟動導致的常見的問題有:

1、在CMD命令加載配置文件(redis.windows.conf)進行啟動是不成功的。提示如下:

Java代碼   收藏代碼
  1. D:\soft\Redis>redis-server.exe redis.windows.conf  
  2. [13760] 11 Jul 16:39:51.067 # Creating Server TCP listening socket 127.0.0.1:6379: bind: No error  

 

因為Redis服務已經自啟動,這里是不會再新啟動的,故加載配置文件是失敗的。也沒有出現Redis啟動的小盒子(下面有圖片,慢慢往下看)

 

2、密碼失效

雖然在配置文件(redis.windows.conf)設置了密碼,密碼為123456:

Java代碼   收藏代碼
  1. ################################## SECURITY ###################################  
  2. ……省略……  
  3. # requirepass foobared  
  4. requirepass 123456  

 

但啟動客戶端進行Redis命令操作時,是不需要密碼的,也沒有提示無權限操作,這是一個嚴重的安全問題。

Java代碼   收藏代碼
  1. D:\soft\Redis>redis-cli.exe  
  2. 127.0.0.1:6379> get name  
  3. "haha"  
  4. 127.0.0.1:6379>  

 

3、Redis訪問IP綁定(bind)無效

Redis默認綁定的ip為127.0.0.1,但如果想內網的機器都能訪問,則需要設置內網的ip地址,如192.168.100.66,然后redis.host則可以設置為192.168.100.66訪問Redis。

 

Redis ip地址綁定默認說明:

Java代碼   收藏代碼
  1. ################################## NETWORK #####################################  
  2.   
  3. # By default, if no "bind" configuration directive is specified, Redis listens  
  4. for connections from all the network interfaces available on the server.  
  5. # It is possible to listen to just one or multiple selected interfaces using  
  6. # the "bind" configuration directive, followed by one or more IP addresses.  
  7. #  
  8. # Examples:  
  9. #  
  10. # bind 192.168.1.100 10.0.0.1  
  11. # bind 127.0.0.1 ::1  
  12. #  
  13. # ~~~ WARNING ~~~ If the computer running Redis is directly exposed to the  
  14. # internet, binding to all the interfaces is dangerous and will expose the  
  15. # instance to everybody on the internet. So by default we uncomment the  
  16. # following bind directive, that will force Redis to listen only into  
  17. # the IPv4 lookback interface address (this means Redis will be able to  
  18. # accept connections only from clients running into the same computer it  
  19. # is running).  
  20. #  
  21. # IF YOU ARE SURE YOU WANT YOUR INSTANCE TO LISTEN TO ALL THE INTERFACES  
  22. # JUST COMMENT THE FOLLOWING LINE.  
  23. # ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~  
  24. bind 127.0.0.1  

 主要是意思是,如果設置了bind,只能通過綁定的地址訪問Redis。

如果不設置bind,則所有地址都可以訪問,如果在項目部署外網,所有人都可以訪問到,所以這里也是個注意的地址,還是設置bind比較安全。

 

綁定多個ip地址:

Java代碼   收藏代碼
  1. bind 127.0.0.1 192.168.100.66  

 127.0.0.1和192.168.100.66之間通過空格分隔,不是逗號。

 

但如果Redis是自啟動的,沒有加載配置文件(redis.windows.conf)啟動,這里的設置也是無效的。

 

如果不綁定ip地址(192.168.100.66),直接設置redis.host=192.168.100.66是訪問不了的,出現以下的錯誤:

Java代碼   收藏代碼
  1. redis.clients.jedis.exceptions.JedisConnectionException: Could not get a resource from the pool  

 

 

所以說,Redis由Windows自啟動的,配置文件的設置都是無效的

 

 

五、解決方案:

1、禁用Redis的自啟動,設置為手動

2、不要使用Redis安裝版,使用壓縮版

3、通過命令行CMD加載配置文件(redis.windows.conf)啟動

Java代碼   收藏代碼
  1. D:\soft\Redis>redis-server.exe redis.windows.conf  

 

通過Cmd啟動的界面都是不一樣的,如下:

 

看到了正常啟動的盒子。

 

4、再新打開一個cmd(不要關閉之前打的Cmd窗口),啟動Redis客戶端:

Java代碼   收藏代碼
  1. D:\soft\Redis>redis-cli.exe  

 

5、獲取Redis中某個key的值,提示無權限。

 

Java代碼   收藏代碼
  1. 127.0.0.1:6379> get name  
  2. (error) NOAUTH Authentication required.  
  3. 127.0.0.1:6379>  

 這樣才是對的。

 

6、通過密碼進入訪問,使用 auth + 密碼,如下:

Java代碼   收藏代碼
  1. 127.0.0.1:6379> get name  
  2. (error) NOAUTH Authentication required.  
  3. 127.0.0.1:6379> auth 123456  
  4. OK  
  5. 127.0.0.1:6379> get name  
  6. "haha"  
  7. 127.0.0.1:6379>  

 

如果Redis設置了密碼,Spring整合Redis也是需要設置密碼的,具體的一些配置:

 

7、Spring整合Redis的一些配置(JedisPool單機版):

 

Spring.xml文件配置的JedisPool池:

Java代碼   收藏代碼
  1. <bean id="jedisPool" class="redis.clients.jedis.JedisPool">  
  2.         <constructor-arg name="poolConfig" ref="jedisPoolConfig"></constructor-arg>  
  3.         <constructor-arg name="host" value="${redis.host}" />  
  4.         <constructor-arg name="port" value="${redis.port}" type="int" />  
  5.         <constructor-arg name="timeout" value="${redis.timeout}" type="int" />  
  6.         <constructor-arg name="password" value="#{'${redis.password}'!=''?'${redis.password}':null}" />  
  7.         <!-- <constructor-arg name="database" value="${redis.db.index}" type="int" /> -->  
  8.     </bean>  

 

redis.properties配置文件

Java代碼   收藏代碼
  1. #*****************jedis連接參數設置*********************#  
  2. #redis服務器ip#    
  3. redis.host=192.168.100.66  
  4. #redis服務器端口號#  
  5. redis.port=6379  
  6. #超時時間:單位ms#    
  7. redis.timeout=3000  
  8. #授權密碼,沒有密碼則不設置,但屬性要保留#  
  9. redis.password=123456  

 

六、如果不是安裝版的Redis,又想讓Redis自啟動的時候,可以向Windows添加自啟動服務:

1、進入到Redis的安裝目錄

 

Java代碼   收藏代碼
  1. D:\soft\Redis>  

 

 

 2、執行命令:

 

Java代碼   收藏代碼
  1. redis-server --service-install redis.windows.conf --loglevel notice --service-name Redis  

 

 

3、完整示例:

 

Java代碼   收藏代碼
  1. D:\soft\Redis>redis-server --service-install redis.windows.conf --loglevel notice --service-name Redis  

 --service-install redis.windows.conf  指定redis配置文件

 

--loglevel notice 指定日志級別

--service-name Redis 指定服務名稱

 

運行結果如下( Redis successfully installed as a service.):

Java代碼   收藏代碼
  1. D:\soft\Redis>redis-server --service-install redis.windows.conf --loglevel notice --service-name Redis  
  2. [7176] 12 Jul 09:34:50.730 # Granting read/write access to 'NT AUTHORITY\NetworkService' on: "D:\soft\Redis" "D:\soft\Redis\"  
  3. [7176] 12 Jul 09:34:50.730 # Redis successfully installed as a service.  

 

4、安裝服務后,默認不是馬上啟動的,但啟動類型是自啟動,如果想馬上啟動,請執行命令:

Java代碼   收藏代碼
  1. redis-server --service-start  
Java代碼   收藏代碼
  1. 服務成功啟動顯示如下:  
  2. [9876] 12 Jul 09:57:41.251 # Redis service successfully started.  

或者重啟電腦。

 

停止服務:

Java代碼   收藏代碼
  1. redis-server --service-stop  

 

 

5、刪除Redis服務:

Java代碼   收藏代碼
  1. redis-server --service-uninstall  

 

 

(如果你覺得文章對你有幫助,歡迎捐贈,^_^,謝謝!) 

 >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>

©Copyright 蕃薯耀 2017年7月11日

http://www.cnblogs.com/fanshuyao/


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM