Description
MySQL config code should use AllowNativePasswords: true
.
The tutorial https://golang.org/doc/tutorial/database-access describes MySQL config code as the following:
cfg := mysql.Config{
User: os.Getenv("DBUSER"),
Passwd: os.Getenv("DBPASS"),
Net: "tcp",
Addr: "127.0.0.1:3306",
DBName: "recordings",
}
But, it causes an error like the following:
[mysql] 2021/10/24 17:03:01 connector.go:95: could not use requested auth plugin 'mysql_native_password': this user requires mysql native password authentication.
2021/10/24 17:03:01 this user requires mysql native password authentication.
To run the code correctly, we should add AllowNativePasswords: true
to the MySQL config or use msql.NewConfig
method instead of creating mysql.Config
struct directly. Please see the following full config code.
The correct code is:
cfg := mysql.Config{
User: os.Getenv("DBUSER"),
Passwd: os.Getenv("DBPASS"),
Net: "tcp",
Addr: "127.0.0.1:3306",
DBName: "recordings",
AllowNativePasswords: true,
}
or
cfg := mysql.NewConfig()
cfg.User = os.Getenv("DBUSER")
cfg.Passwd = os.Getenv("DBPASS")
cfg.Net = "tcp"
cfg.Addr = "127.0.0.1:3306"
cfg.DBName = "recordings"
See also:
- Mark
AllowNativePassword
in DSN param string be true by default. go-sql-driver/mysql#644 - https://github.com/golang/website/blob/281050ffddd88206d068f5f9e9cfa31cf01c9446/_content/doc/tutorial/database-access.md?plain=1#L243-L249
- https://github.com/golang/website/blob/281050ffddd88206d068f5f9e9cfa31cf01c9446/_content/doc/tutorial/database-access.md?plain=1#L692-L698