我正在尝试通过mysql连接连接到我的本地数据库,以便通过Go从我的数据库启动并运行一个查询。
尝试连接时,遇到以下错误:
this user requires clear text authentication. If you still want to use it, please add allowCleartextPasswords=1' to your DSN
我的数据库连接如下所示:db, err := sql.Open("sql", "<username>:<password>@tcp(127.0.0.1:<port>)/<dbname>?charset=utf8" )
在哪里可以包含此依赖项以进行明文身份验证?
发布于 2019-09-17 11:02:12
既然您提到您正在使用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=utf8https://stackoverflow.com/questions/57964922
复制相似问题