I have Mysql Community Server installed on my mac, it is set up and is working, I can connect to it on localhost:3306 using Navicat for MySQL. However, whenever I try and connect to the database from my go app which is running using docker-compose, I get the following error:
dial tcp 127.0.0.1:3306: connect: connection refused
This is my go code:
// dbUser, dbPassword, & dbName are all variables that definitely contain the correct values
db, err = sql.Open("mysql", dbUser+":"+dbPassword+"@tcp(localhost:3306)/"+dbName)
if err != nil {
panic(err.Error())
}
defer db.Close()
query, err := db.Query("INSERT INTO test_table(test_field) VALUES(This is a test)")
if err != nil {
panic(err.Error())
}
defer query.Close()
and I am importing:
"database/sql"
_ "github.com/go-sql-driver/mysql"
Any help would be really appreciated, thank you.