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

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

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初始化连接的方式:

func Connect() {
	d, err := gorm.Open("mysql", "docker:password@tcp(bookstoreDB)/bookstore")
	if err != nil {
		panic(err)
	}

	db = d
}

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

version: "3.8"

services: 
  mysql_server:
    image: mysql:8.0
    container_name: bookstoreDB
    environment: 
      - MYSQL_DATABASE=bookstore
      - MYSQL_USER=docker
      - MYSQL_PASSWORD=password
      - MYSQL_ROOT_PASSWORD=password
    ports:
      - "3306:3306"

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

   Name                 Command             State                 Ports
-------------------------------------------------------------------------------------
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:

func Connect() {
	d, err := gorm.Open("mysql", "docker:password@tcp(bookstoreDB)/bookstore")
	if err != nil {
		panic(err)
	}

	db = d
}

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

version: "3.8"

services: 
  mysql_server:
    image: mysql:8.0
    container_name: bookstoreDB
    environment: 
      - MYSQL_DATABASE=bookstore
      - MYSQL_USER=docker
      - MYSQL_PASSWORD=password
      - MYSQL_ROOT_PASSWORD=password
    ports:
      - "3306:3306"

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

   Name                 Command             State                 Ports
-------------------------------------------------------------------------------------
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集群之外。

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

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

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

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

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

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.

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 :

   Name                 Command             State                 Ports
-------------------------------------------------------------------------------------
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:

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:

确定