drcomwc134525 2019-09-16 22:01
浏览 257

allowCleartextPasswords = 1到DSN

I'm attempting to connect to my local DB via a mysql connection to initiate and run a query off of my database via Go.

When attempting to connect, I am met with this error:

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

My Db connection looks like the following: db, err := sql.Open("sql", "<username>:<password>@tcp(127.0.0.1:<port>)/<dbname>?charset=utf8" )

Where can I include this dependency for cleartext authentication?

  • 写回答

1条回答 默认 最新

  • dongzhuo1498 2019-09-17 03:02
    关注

    Since you mentioned you're using MySQL I recommend leveraging the native struct mysql.Config like so:

    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())
    

    It will properly format the DSN string for you - escaping any sensitive character values along the way (e.g. the loc value):

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

    Playground Example

    评论

报告相同问题?

悬赏问题

  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 保护模式-系统加载-段寄存器
  • ¥15 电脑桌面设定一个区域禁止鼠标操作
  • ¥15 求NPF226060磁芯的详细资料