Why I don’t want to connect to the mysql server (located on the local host: Denwer ). Apache has the following configurations: $&{ip:-127.0.0.1}:$&{port:-80} . Mistake:

 [mysql] 2017/11/29 20:56:50 packets.go:36: unexpected EOF panic: invalid connection 

Code:

 package main import ( "database/sql" _ "github.com/go-sql-driver/mysql" ) func main() { ModuleCreateDatabase() } func ModuleCreateDatabase() *sql.DB { name := "testx" db, err := sql.Open("mysql", "root:echo123@tcp(127.0.0.1:80)/test") if err != nil { panic(err) } defer db.Close() eerr = db.Ping() if eerr != nil { panic(eerr.Error()) } _,err = db.Exec("CREATE DATABASE IF NOT EXISTS "+name) if err != nil { panic(err) } db.Close() return db } 

Bd is, the table is there, everything is there, the localhost works at the address that is registered in Apache . The password is correct: echo123 .

  • After Open you should always do Ping . What will he get out? - Ainar-G
  • @ Ainar-G except panic: exit status 2 does not issue anything db.Ping () - massive
  • @ Ainar-G PHP code $ db = new PDO ('mysql: host = localhost; dbname = formm', 'root', ''); How to write this on Golang? With php it connects perfectly to the database, but golang doesn't want it. - massive
  • I have no experience with MySQL; added you a tag. I can only say that simply "panic: exit status 2" can not be, you have missed something. Either an error from Open or an error from Ping will tell you what's wrong. - Ainar-G
  • Well, yes, why are you using different connection strings in PHP and Go? - Ainar-G

1 answer 1

Everything is now clear. There is no need to complicate the task, just to replace db, err := sql.Open("mysql", "root:echo123@tcp(127.0.0.1:80)/test") with db, err := sql.Open("mysql", "root@/test") .