dongzhi9457 2018-02-04 00:18
浏览 101
已采纳

我需要一些帮助以正确获取连接字符串以将Golang连接到AWS RDS

I have been looking and looking on how to connect a Golang application to a MySQL database (I am using the MyMySQL library with the database/sql interface). If I use the none native version not the database/sql interface I can get the connection to work perfectly and perform queries. If I try to connect using the database/sql go interface (which I think is the best way to interface to MySQL?) then I just cannot get the connection to work? The error I keep getting is 'Wrong database part of URI'?????

Sorry for the variable names but I have been trying and trying to get various versions working so the names are a bit screwy sorry again.

I am grateful for any help understanding my problem, thanks again for your time.

The go code which I am using to produce the connection string.

  • 写回答

1条回答 默认 最新

  • douji0073 2018-02-04 05:51
    关注

    The mymysql package defines a non-standard URI format for accessing MySQL when using the database/sql package (from their readme):

    [PROTOCOL_SPECIFIC*]DBNAME/USER/PASSWD
    

    So in your case you'd want to change your format string to this:

    connectionStr := fmt.Sprintf("tcp:%s:3306,%s/%s/%s", database, dbname, user, password)
    

    The reason you are seeing the error you are seeing is that the Open function splits the URI into 3 parts based on / (see this) and fails if it finds less than three parts.

    On a side note, unless you have a strong reason for choosing mymysql you should consider using go-sql-driver/mysql as it is an excellent MySQL driver and supports standard MySQL DSNs like you already have defined.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

    报告相同问题?

    悬赏问题

    • ¥40 找同学帮敲Python代码
    • ¥15 MYSQL 订单的商品明细重复计算问题
    • ¥15 微信实时共享位置修改
    • ¥100 TG的session协议号转成直登号号后客户端登录几分钟后自动退出设备
    • ¥50 共模反馈回路的小信号增益
    • ¥15 arduino ssd1306函数与tone函数放歌代码不兼容问题
    • ¥70 0.96版本hbase的row_key里含有双引号,无法deleteall
    • ¥15 诊断性META分析合并效能的检验
    • ¥15 请问abb根据色块判断奇偶数并根据批次号放入仓储
    • ¥66 开发PC客户端一定也要开发上位机吗?