Skip to content

Missing default Net value in default Config #1616

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
enrichman opened this issue Jul 31, 2024 · 1 comment
Open

Missing default Net value in default Config #1616

enrichman opened this issue Jul 31, 2024 · 1 comment

Comments

@enrichman
Copy link

enrichman commented Jul 31, 2024

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:

mysql/dsn.go

Lines 85 to 94 in 44553d6

func NewConfig() *Config {
cfg := &Config{
Loc: time.UTC,
MaxAllowedPacket: defaultMaxAllowedPacket,
Logger: defaultLogger,
AllowNativePasswords: true,
CheckConnLiveness: true,
}
return cfg

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.

@olegkash
Copy link

olegkash commented Apr 20, 2025

I found private function normalize which should be assign many default values, not sure if it should be called in NewConfig

https://github.com/go-sql-driver/mysql/blob/21ef4c694538530b2e4b43d1a197402ed22e9749/dsn.go#L173C1-L181C3

func (cfg *Config) normalize() error {
	if cfg.InterpolateParams && cfg.Collation != "" && unsafeCollations[cfg.Collation] {
		return errInvalidDSNUnsafeCollation
	}

	// Set default network if empty
	if cfg.Net == "" {
		cfg.Net = "tcp"
	}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants