2

I'm rather new to use go and am having issues connecting to an external mysql database. I'm using the go-sql-driver which seams rather nice. Suggestions to other drivers are welcomed!

this is the whole program:

import (
  "database/sql"
   _ "github.com/go-sql-driver/mysql"
  "fmt"
)

const (
  DB_HOST = "tcp(http://thedburl.com)"
  DB_NAME = "nameofdatabase"
  DB_USER = "username"
  DB_PW = "password"
)

func main() {
  dsn := DB_USER + ":" + DB_PW + "@" + DB_HOST + "/" + DB_NAME + "?charset=uf8"
  db, err := sql.Open("mysql", dsn)

  if err != nil {
       fmt.Println("shiiet didn't work yo! Initialization failed")
  }

  defer db.Close() // go's purty cool

  var str string
  q := "SELECT * FROM forums"
  err = db.QueryRow(q).Scan(&str)

  if err != nil {
      fmt.Println(err)
  }

  fmt.Println(str)
}

On the request I'm recieving the following error

 "GetAddrInfoW: The specified class was not found."

Any ideas? I've siting for hours on the webs, and can't seem to solve the problem. It might be worth noting that I have used the same database service many times in java.

Daniel Varab
  • 223
  • 1
  • 10
  • How did you install the driver ? Using `go get` ? Are you using Go1.1 ? – Denys Séguret May 26 '13 at 18:21
  • Yeap, via the prompt with go get, and I'm using Go 1.1 :) It's a mad mystery for me .. ! – Daniel Varab May 26 '13 at 18:37
  • I'm going towards this is a driver issue.. I can't seem to find the problem tho. When I call "go get github.com/go-sql-driver/mysql" I get a load time, but no response. Go list is also buggin out and giving me a weird result. – Daniel Varab May 26 '13 at 19:54
  • Just to be sure, did you try with mymysql ? [You just have to do go get then change 2 lines](http://stackoverflow.com/questions/11353679/whats-the-recommended-way-to-connect-to-mysql-from-go). – Denys Séguret May 26 '13 at 19:56
  • With mymysql it returns with the error message "driver: bad connection" after 'hanging' for a few seconds. Another note might be that i'm running windows atm. Worth a try on linux, but honestly, what does this seem to be ? – Daniel Varab May 26 '13 at 20:28
  • 1
    What's "on the request" mean? Also, is this "tcp(http://thedburl.com)" really how things look in your code? In that case, get rid of the "http" bit. – a2800276 May 26 '13 at 21:03
  • on the request: err = db.QueryRow(q).Scan(&str). I've cleaned up the http as you said, sadly no improvements. Could this be caused by the fact that the url for the database has a slash (/) in it? – Daniel Varab May 26 '13 at 21:12
  • latest news is that when using go-sql-driver, it returns an error requiring a port. I don't have this for the given db server. – Daniel Varab May 26 '13 at 21:25
  • @DanielVarab: default port for mysql is 3306. – Lepidosteus May 26 '13 at 22:05
  • @Lepidosteus cheers mate. Turns out this was the problem, and futhermore that the database service in use now implements mysql with old_passwords - Not supported by go-sql-driver. Mymysql on the other hand seems to time out due to the Error "ErrBadConn". Concluding the issue is the database... – Daniel Varab May 27 '13 at 00:29
  • As an aside, I'd suggest not aliasing `_` with your MySQL driver. `_` is typically used to ignore a value in idiomatic Go code. You should also probably print out the error received from `sql.Open` in addition to the message you have there. Use `fmt.Errorf` for that. – elithrar May 27 '13 at 08:35
  • 1
    What `http://` is doing in the database URL? A MySQL URL for the `tcp` transport takes the form `hostname_or_ip[:port]`. – kostix May 27 '13 at 09:24
  • @elithrar Good points. Highly appriciated to have this, again very new to Go – Daniel Varab May 27 '13 at 11:10

2 Answers2

1

Interestingly, this error can be also caused by spaces in the server port as in server.Run(":8080 ") vs server.Run(":8080")

monkrus
  • 1,470
  • 24
  • 23
0

Thanks for everyones answers.

The mysql drivers for Go are at the current time having a hard time dealing with older mysql versions. Specifically this issue happened due to the incompatibility of mysql's old_password from 2006. Therefore working with older databases is a pain. (source: https://github.com/go-sql-driver/mysql/wiki/old_passwords) - My educated guess also applies to the mymysql driver whom throws the "bad connection" error. Which basicly times out after repetitive attempts of connection.

Special thanks to @Lepidosteus who's answer made me discover the real problem.

Daniel Varab
  • 223
  • 1
  • 10