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)進行啟動是不成功的。提示如下:
- D:\soft\Redis>redis-server.exe redis.windows.conf
- [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:
- ################################## SECURITY ###################################
- ……省略……
- # requirepass foobared
- requirepass 123456
但啟動客戶端進行Redis命令操作時,是不需要密碼的,也沒有提示無權限操作,這是一個嚴重的安全問題。
- D:\soft\Redis>redis-cli.exe
- 127.0.0.1:6379> get name
- "haha"
- 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地址綁定默認說明:
- ################################## NETWORK #####################################
- # By default, if no "bind" configuration directive is specified, Redis listens
- # for connections from all the network interfaces available on the server.
- # It is possible to listen to just one or multiple selected interfaces using
- # the "bind" configuration directive, followed by one or more IP addresses.
- #
- # Examples:
- #
- # bind 192.168.1.100 10.0.0.1
- # bind 127.0.0.1 ::1
- #
- # ~~~ WARNING ~~~ If the computer running Redis is directly exposed to the
- # internet, binding to all the interfaces is dangerous and will expose the
- # instance to everybody on the internet. So by default we uncomment the
- # following bind directive, that will force Redis to listen only into
- # the IPv4 lookback interface address (this means Redis will be able to
- # accept connections only from clients running into the same computer it
- # is running).
- #
- # IF YOU ARE SURE YOU WANT YOUR INSTANCE TO LISTEN TO ALL THE INTERFACES
- # JUST COMMENT THE FOLLOWING LINE.
- # ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
- bind 127.0.0.1
主要是意思是,如果設置了bind,只能通過綁定的地址訪問Redis。
如果不設置bind,則所有地址都可以訪問,如果在項目部署外網,所有人都可以訪問到,所以這里也是個注意的地址,還是設置bind比較安全。
綁定多個ip地址:
- 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是訪問不了的,出現以下的錯誤:
- redis.clients.jedis.exceptions.JedisConnectionException: Could not get a resource from the pool
所以說,Redis由Windows自啟動的,配置文件的設置都是無效的
五、解決方案:
1、禁用Redis的自啟動,設置為手動
2、不要使用Redis安裝版,使用壓縮版
3、通過命令行CMD加載配置文件(redis.windows.conf)啟動
- D:\soft\Redis>redis-server.exe redis.windows.conf
通過Cmd啟動的界面都是不一樣的,如下:
看到了正常啟動的盒子。
4、再新打開一個cmd(不要關閉之前打的Cmd窗口),啟動Redis客戶端:
- D:\soft\Redis>redis-cli.exe
5、獲取Redis中某個key的值,提示無權限。
- 127.0.0.1:6379> get name
- (error) NOAUTH Authentication required.
- 127.0.0.1:6379>
這樣才是對的。
6、通過密碼進入訪問,使用 auth + 密碼,如下:
- 127.0.0.1:6379> get name
- (error) NOAUTH Authentication required.
- 127.0.0.1:6379> auth 123456
- OK
- 127.0.0.1:6379> get name
- "haha"
- 127.0.0.1:6379>
如果Redis設置了密碼,Spring整合Redis也是需要設置密碼的,具體的一些配置:
7、Spring整合Redis的一些配置(JedisPool單機版):
Spring.xml文件配置的JedisPool池:
- <bean id="jedisPool" class="redis.clients.jedis.JedisPool">
- <constructor-arg name="poolConfig" ref="jedisPoolConfig"></constructor-arg>
- <constructor-arg name="host" value="${redis.host}" />
- <constructor-arg name="port" value="${redis.port}" type="int" />
- <constructor-arg name="timeout" value="${redis.timeout}" type="int" />
- <constructor-arg name="password" value="#{'${redis.password}'!=''?'${redis.password}':null}" />
- <!-- <constructor-arg name="database" value="${redis.db.index}" type="int" /> -->
- </bean>
redis.properties配置文件
- #*****************jedis連接參數設置*********************#
- #redis服務器ip#
- redis.host=192.168.100.66
- #redis服務器端口號#
- redis.port=6379
- #超時時間:單位ms#
- redis.timeout=3000
- #授權密碼,沒有密碼則不設置,但屬性要保留#
- redis.password=123456
六、如果不是安裝版的Redis,又想讓Redis自啟動的時候,可以向Windows添加自啟動服務:
1、進入到Redis的安裝目錄
- D:\soft\Redis>
2、執行命令:
- redis-server --service-install redis.windows.conf --loglevel notice --service-name Redis
3、完整示例:
- 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.):
- D:\soft\Redis>redis-server --service-install redis.windows.conf --loglevel notice --service-name Redis
- [7176] 12 Jul 09:34:50.730 # Granting read/write access to 'NT AUTHORITY\NetworkService' on: "D:\soft\Redis" "D:\soft\Redis\"
- [7176] 12 Jul 09:34:50.730 # Redis successfully installed as a service.
4、安裝服務后,默認不是馬上啟動的,但啟動類型是自啟動,如果想馬上啟動,請執行命令:
- redis-server --service-start
- 服務成功啟動顯示如下:
- [9876] 12 Jul 09:57:41.251 # Redis service successfully started.
或者重啟電腦。
停止服務:
- redis-server --service-stop
5、刪除Redis服務:
- redis-server --service-uninstall
(如果你覺得文章對你有幫助,歡迎捐贈,^_^,謝謝!)
>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
©Copyright 蕃薯耀 2017年7月11日
http://www.cnblogs.com/fanshuyao/