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 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.