英文:
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
这是我的项目结构的截图:
感谢您的时间。
英文:
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 :
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("mysql", "docker:password@tcp(bookstoreDB)/bookstore")
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->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("mysql", "<docker:password@tcp(localhost)/bookstore>")
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论