我正在尝试通过 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" )
在哪里可以包含明文身份验证的此依赖项?
既然你提到你正在使用MySQL
我建议利用本机结构 mysql。配置如下:
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
游乐场示例