go version: go1.14.1 darwin/amd64 Server version: 10.3.17-MariaDB MariaDB Server

package main

import (
    "github.com/jinzhu/gorm"
    _ "github.com/jinzhu/gorm/dialects/mysql"
    "log"
)

func main() {
    _, err := gorm.Open("mysql", "@(localhost)/test")
    if err == nil {
        log.Println("Connection to MySQL successful")
    } else {
        log.Fatal(err)
    }
}

I get the following error when using GORM driver. I do not get this error when using the native go-sql-driver.

Screen Shot 2020-05-02 at 4 10 36 PM

Comment From: pravinba9495

To verify if this is actually true, I also installed MySQL Server Community V8 (not MariaDB). The above code works flawlessly. The issue seems to occur only with MariaDB.

Comment From: Metrakit

The same here, before i'm using the Psql driver without problems then now i have switched to mariadb i've also this probleme. Did you find a solution @pravinba9495 ?

Comment From: pravinba9495

@Metrakit Hi. Upon further investigation, i figured out that the issue happens only with Mariadb V10, and not the V5.5. I recommend using mariadb-5.5 unless you absolutely need v10.

Comment From: github-actions[bot]

This issue has been automatically marked as stale as it missing playground pull request link, checkout https://github.com/go-gorm/playground for details, it will be closed in 2 days if no further activity occurs.

Comment From: bmknaidu555

Hi, I too faced the same issue and did a lot of experiments to understand the actual reason.

  1. I suggest you look into the "stats" section in PHPMyAdmin/database admin panel and monitor the actual load on your server.
  2. I see that, in your example, you didn't close the connection to the database, In my case, this caused me "commands out of sync error", Because this makes connection buffer overflow.
  3. Make sure close all your connections once you finished using them, or maintain connection pools.

Change your code as shown below:

package main

import (
    "github.com/jinzhu/gorm"
    _ "github.com/jinzhu/gorm/dialects/mysql"
    "log"
)

func main() {
    db, err := gorm.Open("mysql", "@(localhost)/test")
    if err == nil {
        log.Println("Connection to MySQL successful")
    } else {
        log.Fatal(err)
    }
        defer func(){
             dbConn, err := db.DB()     
             dbConn.Close()   
         }
}

Hope this will resolve your issue....

Comment From: maxant

For me, the solution was to add defer rows.Close() after using the .Rows() method, and after checking that the error was nil. e.g.:

rows, err := ctx.GetDb().Raw(sql, applicationId).Rows()

if err != nil {
    return nil, err
} else {
    defer rows.Close() // <<<<<<< THIS FIXED IT!!
    if rows.Next() {

I am using Gorm in a Gin project, and I want to keep connections to the database pooled, which I understand to occur automatically, and so it would be wrong to close the actual connections.