首页 > 技术文章 > redis的bind绑定详解

gcxblogs 2021-04-17 09:18 原文

查看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了

推荐阅读