App won't connect to mysql


(Glenn Hancock) #1

I am using the github.com/go-sql-driver/mysql package to interface to mysql. I have the variables that get set coming from a conf file. Each element is a string value.

DBUser = readconf.GetConfig("Defaults", "DBUser", "")
DBPass = readconf.GetConfig("Defaults", "DBPass", "")
DBHost = readconf.GetConfig("Defaults", "DBHost", "")
DBPort = readconf.GetConfig("Defaults", "DBPort", "")
DBName = readconf.GetConfig("Defaults", "DBName", "")
SetMaxOpenConns, _ = strconv.Atoi(readconf.GetConfig("Defaults", "SetMaxOpenConns", "")

DBConnect = DBUser + ":" + DBPass + "@" + "(" + DBHost + ":" + DBPort + ")/" + DBName + "?charset=utf8&autocommit=true"

if db, err = connectDB( DBConnect ); err != nil {
   fmt.Println( err )
   panic( "Can't connect to the database" ) 
}
    

I’m getting the following error when I attempt to run the program:

dial tcp: lookup tcp/“3306”: Servname not supported for ai_socktype
panic: Can’t connect to the database

What am I doing wrong here?

Thanks,

Glenn


(Norbert Melzer) #2

Have you tried hardcoding your connection string? Does it work then? Is your connection string really assembled as you would expect it to be, or is there something wrong? Does defaulting your config to an empty string make any sense or would erroring be more appropriate on missing config values?


(Glenn Hancock) #3

So the problem ended up being a couple things:

  1. When I copied my parameters that were hardcoded in the go service into the conf file I left the quotes around the fields. These pulled through literally causing 1 problem. Removing them got me past that issue.

  2. I still couldn’t connect though and I was using the root account to test the program. I created a new user account that had access from localhost and now things are working.

So it ended up being a slightly misconfigured mysql and a conf file that was pulling in quotes.

Thanks,

Glenn