go-sql-driver / mysql

Go MySQL Driver is a MySQL driver for Go's (golang) database/sql package
https://pkg.go.dev/github.com/go-sql-driver/mysql
Mozilla Public License 2.0
14.45k stars 2.3k forks source link

Missing default `Net` value in default Config #1616

Open enrichman opened 1 month ago

enrichman commented 1 month ago

Issue description

I spent lot of times thinking about some weird networking issues with my containers but I've finally found which was the problem.

Despite the comment in the doc saying that the tcp protocol is the default for the Net field, the default Config struct from NewConfig is not setting it:

https://github.com/go-sql-driver/mysql/blob/44553d64bcde78a5b58cb133a5cc708281c333e0/dsn.go#L85-L94

Because of that when trying to set only the Addr the DSN will not be respected.

Example code

With only Addr set:

cfg := mysql.NewConfig()
cfg.Addr = "my-address"
fmt.Println(cfg.FormatDSN())

output:

/

With Addr and Net:

cfg = mysql.NewConfig()
cfg.Net = "tcp"
cfg.Addr = "my-address"
fmt.Println(cfg.FormatDSN())

output:

tcp(my-address)/

Expected behaviour

I was expecting to have the tcp(my-address)/ also in the first case. If the fix to add the default tcp value to the default config in the NewConfig is ok I'll be happy to create a PR.