redis的bind綁定詳解


查看redis版本: redis-server -v

$ redis-server -v
Redis server v=4.0.9 sha=00000000:0 malloc=jemalloc-3.6.0 bits=64 build=9435c3c2879311f3

或者運行redis-cli之后輸入info命令可以查看redis的配置信息

$ redis-cli
127.0.0.1:6379> info
# Server
redis_version:4.0.9
redis_git_sha1:00000000
redis_git_dirty:0
redis_build_id:9435c3c2879311f3

redis-server4.0的版本默認啟動了保護模式, 在redis的配置文件/etc/redis/redis.conf中可以看到

bind 127.0.0.1 ::1
# Protected mode is a layer of security protection, in order to avoid that
# Redis instances left open on the internet are accessed and exploited.
#
# When protected mode is on and if:
#
# 1) The server is not binding explicitly to a set of addresses using the
#    "bind" directive.
# 2) No password is configured.
#
# The server only accepts connections from clients connecting from the
# IPv4 and IPv6 loopback addresses 127.0.0.1 and ::1, and from Unix domain
# sockets.
#
# By default protected mode is enabled. You should disable it only if
# you are sure you want clients from other hosts to connect to Redis
# even if no authentication is configured, nor a specific set of interfaces
# are explicitly listed using the "bind" directive.
protected-mode yes

同時默認設置了綁定bind 127.0.0.1 ::1, 這時只能本機通過回環地址127.0.0.1訪問redis服務

讓其他電腦也能訪問到這台機器的redis

之前的誤區是:

如果想要其他的A電腦訪問本機redis服務, 那么在配置文件中加入 bind A電腦的IP 就可以了

我之前想在172.17.100.39上訪問172.17.100.37上運行的redis服務, 按之前錯誤的做法是bind 172.17.100.39, 但是發現redis始終啟動不起來

(base) xxxx:/etc/redis$ sudo systemctl restart redis.service
Job for redis-server.service failed because a timeout was exceeded.
See "systemctl status redis-server.service" and "journalctl -xe" for details.

其實這是錯誤的理解, bind的意思是將redis服務綁定在哪個網卡(IP)上, 通過ifconfig可以查看本機所有的網卡對應的IP地址, 因此bind后面的ip地址只能在ifconfigIP地址中.

因此正確的做法是將redis服務器添加bind 172.17.100.37, 將其綁定在37上后, 39服務器直接訪問37IP地址就可以訪問到37的redis了


免責聲明!

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



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