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.

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

报告相同问题?

悬赏问题

  • ¥30 这是哪个作者做的宝宝起名网站
  • ¥60 版本过低apk如何修改可以兼容新的安卓系统
  • ¥25 由IPR导致的DRIVER_POWER_STATE_FAILURE蓝屏
  • ¥50 有数据,怎么建立模型求影响全要素生产率的因素
  • ¥50 有数据,怎么用matlab求全要素生产率
  • ¥15 TI的insta-spin例程
  • ¥15 完成下列问题完成下列问题
  • ¥15 C#算法问题, 不知道怎么处理这个数据的转换
  • ¥15 YoloV5 第三方库的版本对照问题
  • ¥15 请完成下列相关问题!