为了账号安全,请及时绑定邮箱和手机立即绑定

容器无法连接到 redis 容器

容器无法连接到 redis 容器

Go
小唯快跑啊 2023-07-10 18:00:56
我正在尝试从运行 Go 服务器的容器连接到我的 redis 容器,但连接不断被拒绝,尽管我的 docker-compose.yml 中的设置似乎是正确的:去redisClient = redis.NewClient(&redis.Options{        Network:  "tcp",        Addr:     "redis_server:6379",        Password: "", // no password set        DB:       0,  // use default DB    })docker 撰写version: "0.1"services:  redis_server:    image: "redis"    ports:      - "6379:6379"  lambda_server:    build: .    ports:      - "8080:50051"    links:      - redis_server
查看完整描述

2 回答

?
慕哥6287543

TA贡献1831条经验 获得超10个赞

默认情况下,Redis 不允许远程连接。您只能从 127.0.0.1 (localhost)(运行 Redis 的计算机)连接到 Redis 服务器。

替换/etc/redis/redis.conf 文件中bind 127.0.0.1的。bind 0.0.0.0

然后运行sudo service redis-server restart以重新启动服务器。

使用以下命令验证 redis 是否正在侦听端口 6379 上的所有接口:

ss -an | grep 6379

您应该看到如下所示的内容。0.0.0.0 表示计算机上的所有 IPv4 地址。

tcp  LISTEN 0   128   0.0.0.0:6379   0.0.0.0:*
tcp  LISTEN 0   128      [::]:6379      [::]:*

如果这不能解决问题,您可能需要检查可能阻止访问的防火墙。


查看完整回答
反对 回复 2023-07-10
?
慕婉清6462132

TA贡献1804条经验 获得超2个赞

我遇到了类似的问题,这与地址绑定有关。在 redis 配置文件 /etc/redis/redis.conf 中,找到具有 prefix 的行bind。通常,该行包含bind 127.0.0.1. 这意味着,仅接受来自与 redis 服务器(在您的情况下是 redis 服务器容器)相同的主机的客户端连接。

如果您希望接受客户端连接,则需要在此绑定定义行中添加客户端容器的主机名或主机 IP。

bind 127.0.0.1 <client-ip or client-hostname>

实现此目的的另一种方法是绑定任何地址,

bind 0.0.0.0

无论哪种情况,都需要使用更改后的 .redis 文件重新启动 redis 服务器redis.conf

更新

从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 loopback 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

可以看到默认的绑定地址是127.0.0.1。因此,对于您的情况,您可以指定地址或注释该行。


查看完整回答
反对 回复 2023-07-10
  • 2 回答
  • 0 关注
  • 144 浏览
慕课专栏
更多

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信