error connecting to database with mysqldriver

MysqlGo

Mysql Problem Overview


I'm trying to follow the instructions here https://github.com/go-sql-driver/mysql#installation and http://go-database-sql.org/accessing.html to create a sql.db.

The first line of my code has this

db, err := sql.Open("mysql", "username@localhost/my_db")

When I ran the program on the terminal, I got this:

Default addr for network ''localhost'' unknown

Why is this? When I checked the user and host to mysql it states 'username' and 'localhost'. I followed the parameters like this:

[username[:password]@][protocol[(address)]]/dbname[?param1=value1&...&paramN=valueN]

Mysql Solutions


Solution 1 - Mysql

You might want to specify the protocol (like 'tcp'), instead of localhost directly.
See those examples:

user:password@tcp(localhost:5555)/dbname

In your case:

username@tcp(localhost)/my_db

Note, if you use the default protocol (tcp) and host (localhost:3306), this could be rewritten as

user:password@/dbname

Solution 2 - Mysql

I had the same problem and the most voted answer couldn't help me. What saved me was putting (host:port) inside quotation --> "(host:port)"

Solution 3 - Mysql

I had faced a similar issue as I was running Docker containers on my Linux VM. In my application (server.go) I changed the localhost value to use the IP of my virtual machine and then build and ran the container successfully.

mysql container (3307) <--> [(application mysql:<vm ip>:3307) container](expose 3000) <-->  world 

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
Questionuser3918985View Question on Stackoverflow
Solution 1 - MysqlVonCView Answer on Stackoverflow
Solution 2 - MysqlrahaView Answer on Stackoverflow
Solution 3 - MysqlnevosialView Answer on Stackoverflow