恐慌:拨号tcp:在172.22.64.1:53上查找bookstoreDB:没有这样的主机

huangapple go评论128阅读模式
英文:

panic: dial tcp: lookup bookstoreDB on 172.22.64.1:53: no such host

问题

首次在这里发帖!
我正在尝试使用golang、gin和gorm从docker连接到mysql服务器,但是我在通过gorm连接到服务器时遇到了问题。

这是我得到的错误信息:

panic: dial tcp: lookup bookstoreDB on 172.22.64.1:53: no such host

这是我尝试通过gorm初始化连接的方式:

  1. func Connect() {
  2. d, err := gorm.Open("mysql", "docker:password@tcp(bookstoreDB)/bookstore")
  3. if err != nil {
  4. panic(err)
  5. }
  6. db = d
  7. }

这是我的docker-compose.yml文件的内容:

  1. version: "3.8"
  2. services:
  3. mysql_server:
  4. image: mysql:8.0
  5. container_name: bookstoreDB
  6. environment:
  7. - MYSQL_DATABASE=bookstore
  8. - MYSQL_USER=docker
  9. - MYSQL_PASSWORD=password
  10. - MYSQL_ROOT_PASSWORD=password
  11. ports:
  12. - "3306:3306"

当我运行命令"docker-compose ps"时,显示如下内容:

  1. Name Command State Ports
  2. -------------------------------------------------------------------------------------
  3. bookstoreDB docker-entrypoint.sh mysqld Up 0.0.0.0:3306->3306/tcp, 33060/tcp

这是我的项目结构的截图:

bookstore项目结构

感谢您的时间。

英文:

First post here!
Trying to connect to a mysql server from docker using golang,gin, and gorm but I'm having issues connecting to the server through gorm.

Here is the error I am getting :

panic: dial tcp: lookup bookstoreDB on 172.22.64.1:53: no such host

Here is how I am trying to initiate the connection through gorm:

  1. func Connect() {
  2. d, err := gorm.Open("mysql", "docker:password@tcp(bookstoreDB)/bookstore")
  3. if err != nil {
  4. panic(err)
  5. }
  6. db = d
  7. }

Here's what my docker-compose.yml file looks like:

  1. version: "3.8"
  2. services:
  3. mysql_server:
  4. image: mysql:8.0
  5. container_name: bookstoreDB
  6. environment:
  7. - MYSQL_DATABASE=bookstore
  8. - MYSQL_USER=docker
  9. - MYSQL_PASSWORD=password
  10. - MYSQL_ROOT_PASSWORD=password
  11. ports:
  12. - "3306:3306"

Here is what appears when I run the "docker-compose ps" command:

  1. Name Command State Ports
  2. -------------------------------------------------------------------------------------
  3. bookstoreDB docker-entrypoint.sh mysqld Up 0.0.0.0:3306->3306/tcp, 33060/tcp

Here is a screenshot of my project structure :

bookstore project structure

Thank you for your time.

答案1

得分: 1

你正在尝试连接到一个完全未知的主机名,该主机名不在docker-compose集群之外。

  1. d, err := gorm.Open("mysql", "docker:password@tcp(bookstoreDB)/bookstore")

这里的bookstoreDB是完全未知的。将端口映射的想法(就像你正在做的mysql的端口映射一样:

  1. Name Command State Ports
  2. -------------------------------------------------------------------------------------
  3. bookstoreDB docker-entrypoint.sh mysqld Up 0.0.0.0:3306->3306/tcp, 33060/tcp

是将该映射用作与数据库的连接(而不是容器的内部主机名)。

修改你的代码,将连接改为localhost:3306。以下是伪代码,因为我不了解你的编程语言:

  1. d, err := gorm.Open("mysql", "<docker:password@tcp(localhost)/bookstore>")
英文:

Here you are trying to connect to a hostname that is totally unknown out of the docker-compose cluster.

  1. d, err := gorm.Open(&quot;mysql&quot;, &quot;docker:password@tcp(bookstoreDB)/bookstore&quot;)

bookstoreDB is totally unknown here. The idea of mapping the ports (as you are doing with mySql's one :

  1. Name Command State Ports
  2. -------------------------------------------------------------------------------------
  3. bookstoreDB docker-entrypoint.sh mysqld Up 0.0.0.0:3306-&gt;3306/tcp, 33060/tcp

Is to use that mapping as connection to the database (not the container's intra host name).

Change your code so you connect to your localhost:3306 instead. This is pseudocode as don't understand the language:

  1. d, err := gorm.Open(&quot;mysql&quot;, &quot;&lt;docker:password@tcp(localhost)/bookstore&gt;&quot;)

huangapple
  • 本文由 发表于 2022年7月31日 10:04:09
  • 转载请务必保留本文链接:https://go.coder-hub.com/73180220.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定