mariadb version: 10.5.22
db.Ping()
returns driver: bad connection
package main
import (
"database/sql"
"log"
"net/http"
"os"
"time"
"github.com/gin-gonic/gin"
_ "github.com/go-sql-driver/mysql"
)
func main() {
db, err := sql.Open("mysql", os.Getenv("DATABASE_URL"))
if err != nil {
log.Fatal(err)
}
db.SetMaxOpenConns(25)
db.SetMaxIdleConns(25)
db.SetConnMaxLifetime(2 * time.Minute)
router := gin.Default()
api := router.Group("/api")
api.GET("/health", func(c *gin.Context) {
err := db.Ping()
if err != nil {
c.JSON(http.StatusInternalServerError, gin.H{
"error": err.Error(),
})
return
}
c.JSON(http.StatusOK, gin.H{
"message": "OK",
})
})
if err := router.Run(":8080"); err != nil {
log.Fatal(err)
}
}
server logs:
[mysql] 2024/07/09 02:04:23 connection.go:49: unexpected EOF
[mysql] 2024/07/09 02:04:34 connection.go:49: unexpected EOF
[mysql] 2024/07/09 02:04:44 connection.go:49: unexpected EOF
I have tried playing with various db settings but no luck.
db.SetMaxOpenConns(25)
db.SetMaxIdleConns(25)
db.SetConnMaxLifetime(2 * time.Minute)
My password was not being parsed correctly because it has special character.
Using formatDSN()
fixed my problem.