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 keil的map文件中Image component sizes各项意思
  • ¥30 BC260Y用MQTT向阿里云发布主题消息一直错误
  • ¥20 求个正点原子stm32f407开发版的贪吃蛇游戏
  • ¥15 划分vlan后,链路不通了?
  • ¥20 求各位懂行的人,注册表能不能看到usb使用得具体信息,干了什么,传输了什么数据
  • ¥15 Vue3 大型图片数据拖动排序
  • ¥15 Centos / PETGEM
  • ¥15 划分vlan后不通了
  • ¥20 用雷电模拟器安装百达屋apk一直闪退
  • ¥15 算能科技20240506咨询(拒绝大模型回答)