Error while creating connection with phpmyadmin using golang "commands out of sync. Did you run multiple statements at once?"
I’m facing some issue while fetching the data from the MySQL database using golang below is my code and the error that I’m facing
package main
import ( "database/sql" "fmt" _ "github.com/go-sql-driver/mysql" ) func ConnectMsqlDb() (db *sql.DB, err error) { db, err = sql.Open("mysql", fmt.Sprintf("%s:%s@tcp(%s:"+sqlDbPort+")/"+sqlDB, sqlUserName, sqlPassword, dbServerIP)) if err != nil { return nil, err } //defer db.Close() err = db.Ping() if err != nil { return nil, err } return db, nil } func GetSqlData() (err error, data interface{}) { db, err := ConnectMsqlDb() if err != nil { // here it will returning me the error return err, nil } rows, err := db.Query("SELECT * FROM users") if err != nil { return err, nil } for rows.Next() { } defer db.Close() fmt.Println(rows) return err, rows } func main() { err, data := GetSqlData() fmt.Println("data", data, "err", err) }
error
data commands out of sync. Did you run multiple statements at once?
Can anyone tell me why I’m facing this issue
If the error is comming while opening a connection to mysqld
, it could be possible that MySQL server (mysqld) is blocking the host from connecting to it. It means that mysqld has received many connection requests from the given host that were interrupted in the middle.
Read why? To confirm you could see the DB’s logs as well. So, a way to unblock it is to flush the cached hosts using the MySQL CLI:
mysql> FLUSH HOSTS;
And then try again to connect.
Plus, give this answer a read as well. Might help.
You can check the state of the socket used by mysqld using (For Linux):
netstat -nt
Check if there’s any previously hanging connection from the host to mysqld. If yes, then kill it and try again.