error connecting to database with mysqldriver
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&...¶mN=valueN]
Solution 1:
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:
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)"