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.

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

报告相同问题?

悬赏问题

  • ¥15 生物医学数据分析。基础课程就v经常唱课程舅成牛逼
  • ¥15 云环境云开发云函数对接微信商户中的分账功能
  • ¥15 空间转录组CRAD遇到问题
  • ¥20 materialstudio计算氢键脚本问题
  • ¥15 有没有代做有偿主要做数据可视化部分即可(2023全国高考更省一本线理科类)
  • ¥15 配置FPT报错,该如何处理
  • ¥15 请大家看一下这个代码咋写,一点思路都没有,最好能做一下,不要伪代码,有偿
  • ¥15 有偿请人帮写个安卓系统下禁止装软件及禁止拷入文件的程序
  • ¥100 用 H.265 对音视频硬编码 (CUDA)
  • ¥20 mpich安装完成后出问题