问题描述
如何解决 MySQL连接报错 "panic: Error 1193: Unknown system variable 'characterEncoding 的问题 ?
问题分析
不同的 SDK 应用中的连接字符串参数是不一样的,客户使用的是 GO 的 GORM 去连接的数据,而连接字符串中确实使用的 JAVA Connector/J 的参数 characterEncoding 去连接的数据库。
解决方案
- JAVA 指定字符集的正确示例如下:
jdbc:mysql://localhost:3306/test?characterEncoding=utf8&useSSL=false
- GORM 的正确示例如下:
import (
"gorm.io/driver/mysql"
"gorm.io/gorm"
)
func main() {
// refer https://github.com/go-sql-driver/mysql#dsn-data-source-name for details
dsn := "user:pass@tcp(127.0.0.1:3306)/dbname?charset=utf8mb4&parseTime=True&loc=Local"
db, err := gorm.Open(mysql.Open(dsn), &gorm.Config{})
}
如果需要配置其他连接参数,请根据实际生产业务情况调整
参考文档
[1] https://gorm.io/docs/connecting_to_the_database.html [2] https://dev.mysql.com/doc/connector-j/5.1/en/connector-j-reference-charsets.html 如果您有其他问题,欢迎您联系火山引擎技术支持服务