allowCleartextPasswords=1 到 DSN

Damon   2022-11-11T08:48:59

我正在尝试通过 mysql 连接连接到我的本地数据库,以通过 Go 启动和运行我的数据库的查询。

尝试连接时,遇到此错误:

this user requires clear text authentication. If you still want to use it, please add allowCleartextPasswords=1' to your DSN

我的 Db 连接如下所示:db, err := sql.Open("sql", "<username>:<password>@tcp(127.0.0.1:<port>)/<dbname>?charset=utf8" )

我在哪里可以包含此依赖项以进行明文身份验证?

点击广告,支持我们为你提供更好的服务
评论(1)
Damon

既然你提到你正在使用MySQL我建议像这样利用本机结构mysql.Config

loc, _ := time.LoadLocation("America/Chicago") // handle any errors!

c := mysql.Config{
    User:                    "user",
    Passwd:                  "pa55w0rd",
    Net:                     "tcp",
    Addr:                    "127.0.0.1:3306",
    DBName:                  "dbname",
    Params:                  map[string]string{"charset": "utf8"}, // extra params
    AllowCleartextPasswords: true,
    ParseTime:               true, // demo option
    Loc:                     loc,  // demo option
}
fmt.Println(c.FormatDSN())

它将DSN为您正确格式化字符串 - 沿途转义任何敏感字符值(例如loc值):

user:pa55w0rd@tcp(127.0.0.1:3306)/dbname?allowCleartextPasswords=true&allowNativePasswords=false&loc=America%2FChicago&parseTime=true&maxAllowedPacket=0&charset=utf8

游乐场示例

2022-11-11T08:49:00   回复