redis中redis.windows.conf和redis.windows-service.conf文件的區別
原文鏈接:https://blog.csdn.net/summer_style/java/article/details/106534996
Windows版的Redis有2個配置文件,一個是:redis.windows.conf,另一個是redis.windows-service.conf。
由於安裝版的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
1
2
因為Redis服務已經自啟動,這里是不會再新啟動的,故加載配置文件是失敗的。也沒有出現Redis啟動的小盒子(下面有圖片,慢慢往下看),需要注意的是Windows版的Redis安裝時,默認啟動加載的配置文件是redis.windows-service.conf,如下圖所示:
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
1
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自啟動的,配置文件(redis.windows.conf)的設置都是無效的
五、解決方案:
1、禁用Redis的自啟動,設置為手動
2、不要使用Redis安裝版,使用壓縮版
3、通過命令行CMD加載配置文件(redis.windows.conf)啟動
D:\soft\Redis>redis-server.exe redis.windows.conf
1
4、再新打開一個cmd(不要關閉之前打的Cmd窗口),啟動Redis客戶端:
D:\soft\Redis>redis-cli.exe
1
5、獲取Redis中某個key的值,提示無權限。
127.0.0.1:6379> get name
(error) NOAUTH Authentication required.
127.0.0.1:6379>
1
2
3
通過密碼進入訪問,使用 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,又想讓Redis自啟動的時候,可以向Windows添加自啟動服務:
1、進入到Redis的安裝目錄
D:\soft\Redis>
1
2、執行命令:
redis-server --service-install redis.windows.conf --loglevel notice --service-name Redis
1
完整實例指令:
D:\soft\Redis>redis-server --service-install redis.windows.conf --loglevel notice --service-name Redis
1
–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.
1
4、安裝服務后,默認不是馬上啟動的,但啟動類型是自啟動,如果想馬上啟動,請執行命令:
redis-server --service-start
1
服務成功啟動顯示如下:
[9876] 12 Jul 09:57:41.251 # Redis service successfully started.
1
2
或者重啟電腦。
5、刪除Redis服務:
redis-server --service-uninstall
5:redis的配置文件講解
redis.windows.conf文件中
#redis的配置
#Redis默認不是以守護進程的方式運行,可以通過該配置項修改,使用yes啟用守護進程
daemonize yes
#當Redis以守護進程方式運行時,Redis默認會把pid寫入redis.pid文件,可以通過pidfile指定
pidfile 'E:/xxx/redis/redis_pid/redis.pid'
#端口
port 6379
#綁定主機的ip地址
bind 127.0.0.1
#當 客戶端閑置多長時間后關閉連接,如果指定為0,表示關閉該功能
timeout 300
#指定日志記錄級別,Redis總共支持四個級別:debug、verbose、notice、warning,默認為verbose
loglevel notice
#日志記錄方式,默認為標准輸出,如果配置Redis為守護進程方式運行,而這里又配置為日志記錄方式為標准輸出,則日志將會發送給/dev/null
logfile stdout
#設置數據庫的數量,默認數據庫為0,可以使用SELECT <dbid>命令在連接上指定數據庫id
databases 16
#指定在多長時間內,有多少次更新操作,就將數據同步到數據文件,可以多個條件配合
#分別表示900秒(15分鍾)內有1個更改,300秒(5分鍾)內有10個更改以及60秒內有10000個更改
save 900 1
save 300 10
save 60 10000
#指定存儲至本地數據庫時是否壓縮數據,默認為yes,Redis采用LZF壓縮,如果為了節省CPU時間,可以關閉該選項,但會導致數據庫文件變的巨大
rdbcompression yes
#指定本地數據庫文件名,默認值為dump.rdb
dbfilename dump.rdb
#指定本地數據庫存放目錄
dir 'D:/XXX/redis/redis_database'
#設置當本機為slav服務時,設置master服務的IP地址及端口,在Redis啟動時,它會自動從master進行數據同步
#slaveof 127.0.0.1 6379
#當master服務設置了密碼保護時,slav服務連接master的密碼
#masterauth 123456
#設置Redis連接密碼,如果配置了連接密碼,客戶端在連接Redis時需要通過AUTH <password>命令提供密碼,默認關閉
#requirepass foobared
#設置同一時間最大客戶端連接數,默認無限制,Redis可以同時打開的客戶端連接數為Redis進程可以打開的最大文件描述符數,如果設置 maxclients 0,表示不作限制。當客戶端連接數到達限制時,Redis會關閉新的連接並向客戶端返回max number of clients reached錯誤信息
maxclients 10000
#指定Redis最大內存限制,Redis在啟動時會把數據加載到內存中,達到最大內存后,Redis會先嘗試清除已到期或即將到期的Key,當此方法處理 后,仍然到達最大內存設置,將無法再進行寫入操作,但仍然可以進行讀取操作。Redis新的vm機制,會把Key存放內存,Value會存放在swap區
maxmemory 300m
#指定是否在每次更新操作后進行日志記錄,Redis在默認情況下是異步的把數據寫入磁盤,如果不開啟,可能會在斷電時導致一段時間內的數據丟失。因為 redis本身同步數據文件是按上面save條件來同步的,所以有的數據會在一段時間內只存在於內存中。默認為no
appendonly yes
#指定更新日志文件名,默認為appendonly.aof
appendfilename 'appendonly.aof'
#指定更新日志條件,共有3個可選值
#no:表示等操作系統進行數據緩存同步到磁盤(快)
#always:表示每次更新操作后手動調用fsync()將數據寫到磁盤(慢,安全)
#everysec:表示每秒同步一次(折衷,默認值)
appendfsync everysec
————————————————
原文鏈接:https://blog.csdn.net/summer_style/java/article/details/106534996