Redis 配置文件中bind參數
前言#
我們都知道,redis 的配置文件中,默認綁定接口是 127.0.0.1,也就是本地回環接口,所以是無法從外網連接 redis 服務的。如果想要讓外網也能連接使用服務器上的 redis 服務,可以簡單地注釋掉 bind 這一行。但對於 bind 參數的作用,網上有很多文章的解釋都是誤人子弟的。
關於bind#
翻看網上的文章,此處多翻譯為:
指定 redis 只接收來自於該 IP 地址的請求,如果不進行設置,那么將處理所有請求,在生產環境中最好設置該項。
這種解釋會搞糊塗初學者,甚至是錯誤的。查看配置文件redis.conf
,可以看到很詳細的注釋說明。
# 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
請認真閱讀上述英文注釋, 下面是google翻譯
默認情況下,如果未指定“ bind”配置指令,則Redis將偵聽服務器上所有可用網絡接口的連接。 可以使用“ bind”配置指令僅偵聽一個或多個所選接口,然后偵聽一個或多個IP地址。
如果運行Redis的計算機直接暴露於Internet,則綁定到所有接口都是很危險的,並且會將實例暴露給Internet上的所有人。 因此,默認情況下,我們取消注釋以下bind指令,這將強制Redis僅偵聽IPv4回顧接口地址(這意味着Redis將只能接受來自運行在同一台計算機上的客戶端的連接)。
如果您確定要立即偵聽所有接口,請僅注意以下內容。
bind 127.0.0.1
正確做法#
redis部署在本機#
執行ifconfig
中獲取到本機ip (或者hostname -I
)
redis.conf
配置為bind 127.0.0.1 本機ip
redis部署在docker中#
進入redis所在的docker容器
執行ifconfig
中獲取到本機ip (或者hostname -I
)
redis.conf
配置為bind 127.0.0.1 本機ip
redis集群設置密碼
redis集群密碼設置
1、密碼設置(推薦)
方式一:修改所有Redis集群中的redis.conf文件加入:
masterauth passwd123 requirepass passwd123
說明:這種方式需要重新啟動各節點
方式二:進入各個實例進行設置:
./redis-cli -c -p 7000 config set masterauth passwd123 config set requirepass passwd123 config rewrite
之后分別使用./redis-cli -c -p 7001,./redis-cli -c -p 7002…..命令給各節點設置上密碼。
注意:各個節點密碼都必須一致,否則Redirected就會失敗, 推薦這種方式,這種方式會把密碼寫入到redis.conf里面去,且不用重啟。
用方式二修改密碼,./redis-trib.rb check 10.104.111.174:6379執行時可能會報[ERR] Sorry, can't connect to node 10.104.111.174:6379,因為6379的redis.conf沒找到密碼配置。
2、設置密碼之后如果需要使用redis-trib.rb的各種命令
如:./redis-trib.rb check 127.0.0.1:7000,則會報錯ERR] Sorry, can’t connect to node 127.0.0.1:7000
解決辦法:vim /usr/local/rvm/gems/ruby-2.3.3/gems/redis-4.0.0/lib/redis/client.rb,然后修改passord
class Client DEFAULTS = { :url => lambda { ENV["REDIS_URL"] }, :scheme => "redis", :host => "127.0.0.1", :port => 6379, :path => nil, :timeout => 5.0, :password => "passwd123", :db => 0, :driver => nil, :id => nil, :tcp_keepalive => 0, :reconnect_attempts => 1, :inherit_socket => false }
注意:client.rb路徑可以通過find命令查找:find / -name 'client.rb'
帶密碼訪問集群
./redis-cli -c -p 7000 -a passwd123
在springboot項目中還需要設置application.properties中需要設置redisde的密碼
spring.redis.password=xxxxx