Home > OS >  How to resolve `invalid packet size, it is shorter than header size` error
How to resolve `invalid packet size, it is shorter than header size` error

Time:11-14

I am trying to connect to my db. but getting the following Error:

2022/11/10 13:30:43 invalid packet size, it is shorter than header size

My Code:

var server = "123.45.67.89"
var port = 3030
var user = "myUserId"
var password = "MyPassword"
var database = "myDB"

func main() {
    // Build connection string
    connString := fmt.Sprintf("server=%s;user id=%s;password=%s;port=%d;database=%s;",
        server, user, password, port, database)

    var err error

    // Create connection pool
    db, err = sql.Open("sqlserver", connString)
    if err != nil {
        log.Fatal("Error creating connection pool: ", err.Error())
    }

    ctx := context.Background()
    err = db.PingContext(ctx)
    if err != nil {
        fmt.Println("Catching ERR")
        log.Fatal(err.Error())
    }
    fmt.Printf("Connected!\n")
}

At this following line it is catching an error:

err = db.PingContext(ctx)

Do Someone knows, how I can resolve this issue? Please help.

CodePudding user response:

I tried another way to connect to DB. and it worked for me.

package main

import (
    "database/sql"
    "fmt"

    _ "github.com/go-sql-driver/mysql"
)

func main() {

    fmt.Println("Golang: My Sql Connection Code:")

    db, err := sql.Open("mysql", "userName:password@tcp(123.45.67.89:3030)/myDB")
    if err != nil {
        panic(err.Error())
    }

    defer db.Close()

    fmt.Println("Connection Successful")

    data, err := db.Query("select * from my_user_table")

    if err != nil {
        panic(err.Error())
    }
}
  • Related