亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

容器無法連接到 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 關注
  • 304 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號