连接被拒绝,使用Go、MySQL和Docker。

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

connection refused with go&mysql&docker

问题

我正在尝试从我的本地go文件连接到在Docker上创建的mysqlDB。

main.go

package main

import (
	"database/sql"
	"fmt"

	_ "github.com/go-sql-driver/mysql"
)

func main() {
	fmt.Println("Hello")
	// 以下的4行代码都无法连接到数据库。
	// "db01"是Docker容器的名称,"test"是数据库名称。
	// db, err := sql.Open("mysql", "root:password@tcp(localhost:3306)/test")
	// db, err := sql.Open("mysql", "root:password@tcp(localhost)/test")
	// db, err := sql.Open("mysql", "root:password@tcp(db01)/test")
	db, err := sql.Open("mysql", "root:password@tcp(db01:3306)/test")

	if err != nil {
		fmt.Println(err)
	}
	defer db.Close()
	fmt.Println(db)
	err = db.Ping()
	if err != nil {
		fmt.Println(err)
	}
}

Docker命令

docker run --name db01 -dit -e MYSQL_ROOT_PASSWORD=password mysql:latest

错误信息

尝试使用localhost:3306时:

dial tcp [::1]:3306: connect: connection refused

尝试使用db01时:

dial tcp: lookup db01: no such host

mysql数据库已在Docker容器上成功创建,所以我认为go文件和Docker容器之间的连接出现了问题。

我需要创建docker-compose.yml文件吗?我希望现在不使用它进行连接。

谢谢。

英文:

I'm trying to connect mysqlDB which is created on Docker from my local go file.

main.go

package main

import (
	"database/sql"
	"fmt"

	_ "github.com/go-sql-driver/mysql"
)

func main() {
	fmt.Println("Hello")
	// All following 4 codes won't connect to the database.
        // "db01" is docker container name. "test" is db name.
	// db, err := sql.Open("mysql", "root:password@tcp(localhost:3306)/test")
	// db, err := sql.Open("mysql", "root:password@tcp(localhost)/test")
	// db, err := sql.Open("mysql", "root:password@tcp(db01)/test")
	db, err := sql.Open("mysql", "root:password@tcp(db01:3306)/test")

	if err != nil {
		fmt.Println(err)
	}
	defer db.Close()
	fmt.Println(db)
	err = db.Ping()
	if err != nil {
		fmt.Println(err)
	}
}

docker command

docker run --name db01 -dit -e MYSQL_ROOT_PASSWORD=password mysql:latest

error message

when trying with localhost:3306

dial tcp [::1]:3306: connect: connection refused

when trying with db01

dial tcp: lookup db01: no such host

mysql db is created on docker container without any issue, so I think connection between go file and docker container is not going well.

Do I have to create docker-compose.yml file?
I want to connect without it at now.

Thank you.

答案1

得分: 0

你创建了一个 Docker 容器,并在其中运行了 MySQL。该容器将有自己的网络地址。你可以通过运行以下命令,在本地主机上发布该容器的端口:

docker run --name db01 -p 3306:3306 -dit -e MYSQL_ROOT_PASSWORD=password mysql:latest

这将在 localhost:3306 上发布容器的 3306 端口。然后你可以连接到它。

或者,你可以使用 docker inspect <containerId> 命令获取正在运行的容器的 IP 地址,并连接到该地址的 3306 端口。

英文:

You created a docker container and ran MySQL in it. That container will have its own network address. You can publish a port of that container on your localhost by running:

docker run --name db01 -p 3306:3306 -dit -e MYSQL_ROOT_PASSWORD=password mysql:latest

This will publish the port 3306 of the container on localhost:3306. Then you can connect to it.

Alternatively, you can get the IP address of the container running using docker inspect &lt;containerId&gt;, and connect to that address:3306.

huangapple
  • 本文由 发表于 2022年12月29日 09:07:54
  • 转载请务必保留本文链接:https://go.coder-hub.com/74946363.html
匿名

发表评论

匿名网友

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

确定