Skip to content

charset option does not work. #1680

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

Closed
furusax0621 opened this issue Feb 25, 2025 · 1 comment
Closed

charset option does not work. #1680

furusax0621 opened this issue Feb 25, 2025 · 1 comment

Comments

@furusax0621
Copy link

Since v1.9.0, when initializing sql.DB with the charset option set in config.Params as shown in the following code, an error occurs during query execution:

config := mysql.NewConfig()
config.Net = "tcp"
config.Addr = "127.0.0.1:3306"
// etc...
config.Params = map[string]string{
    "charset": "utf8mb4",
}

conn, err := mysql.NewConnector(config)
if err != nil {
    log.Fatal(err)
}
db := sql.OpenDB(conn)

if _, err := db.Exec("SELECT 1"); err != nil {
    log.Fatal(err)
}
// return Error 1193 (HY000): Unknown system variable 'charset'

However, it works without any issues when initialized using config.FormatDSN():

config := mysql.NewConfig()
config.Net = "tcp"
config.Addr = "127.0.0.1:3306"
// etc...
config.Params = map[string]string{
    "charset": "utf8mb4",
}
db, err := sql.Open("mysql", config.FormatDSN())
if err != nil {
    log.Fatal(err)
}

if _, err := db.Exec("SELECT 1"); err != nil {
    log.Fatal(err)
}

I believe this is due to the change in the timing of interpreting the charset option in #1604 .
In the current implementation, it is interpreted within the ParseDSN method and stored in the config.charsets field.

mysql/dsn.go

Lines 525 to 527 in 58941dd

// charset
case "charset":
cfg.charsets = strings.Split(value, ",")

However, in the way I wrote, the connection is created without going through the ParseDSN method, so charset is sent as an invalid option.

The charset option should be interpreted regardless of the connection establishment procedure.

@methane
Copy link
Member

methane commented Feb 25, 2025

Duplicate of #1664 and #1678.

See #1679 as fix PR.

And using mysql.ParseDSN("/?charset=utf8mb4") instead of mysql.NewConfig() is workaround for now.

@methane methane closed this as completed Feb 25, 2025
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