I have a mysql DB lives on another server. is my connection string correct? I kept getting "Default addr for network '11.11.11.12:3306' unknown", not sure what is wrong and hwo to fix it.
i.DB, err = gorm.Open("mysql", "test:[email protected]:3306/gormtest?charset=utf8@parseTime=true")
Should be: id:password@tcp(your-amazonaws-uri.com:3306)/dbname
Please refer https://github.com/go-sql-driver/mysql
you should make sure the database you connect (here gormtest) has been created.
Try to do:
import (
"github.com/jinzhu/gorm"
_ "github.com/jinzhu/gorm/dialects/mysql"
)
func main() {
db, err := gorm.Open("mysql", "user:password@/dbname?charset=utf8&parseTime=True&loc=Local")
defer db.Close()
}
Work for me
Reference: http://doc.gorm.io/database.html#connecting-to-a-database
what is the code if we want open a connection and never close it
should we open in func init(){} ?
what is the code if we want open a connection and never close it
should we open in func init(){} ?
Simply don't call "defer db.Close()" after opening the connection to the database. But, this would be a very bad practice.
Most helpful comment
Should be:
id:password@tcp(your-amazonaws-uri.com:3306)/dbnamePlease refer https://github.com/go-sql-driver/mysql