Skip to content

On server restart clients get this error: MySQL error: commands out of sync. You can't run this command now #449

Closed
@tomponline

Description

@tomponline

Issue description

When using a persistent connection to a MySQL server, if the server is restarted, the old connection is left in a CLOSE_WAIT state on the client (suggesting the Go program has not detected the socket was closed).

E.g.

 service mysql restart
 netstat -tnp 
 tcp       35     0 127.0.0.1:51890             127.0.0.1:3306              CLOSE_WAIT  7662/db

This means that the next time the Go program tries to run a query, the dead connection is used and we get the error:

MySQL error: commands out of sync. You can't run this command now

The dead connection is then removed by the SQL drive and reconnects to the server.

Further queries run OK.

Example code

package main

import _ "github.com/go-sql-driver/mysql"
import "database/sql"
import "fmt"
import "time"

func main() {

    db, err := sql.Open("mysql", "root:@tcp(127.0.0.1:3306)/mysql")

        if err != nil {
                fmt.Println("Open error: ", err)
                return
        }

    err = db.Ping()

        if err != nil {
                fmt.Println("Ping error: ", err)
                return
        }
    fmt.Println("Ping OK")

        var connId int

        for {
                if err := db.QueryRow("SELECT CONNECTION_ID()").Scan(&connId); err != nil {
                        fmt.Println("MySQL error: ", err)
                        time.Sleep(10 * time.Second)
                        continue
                }
                fmt.Println("Conn ID: ", connId)
                time.Sleep(10 * time.Second)
        }

}

Error log

[root@tpel6 ~]# go run db.go 
Ping OK
Conn ID:  1519
Conn ID:  1519
Conn ID:  1519
Conn ID:  1519
Conn ID:  1519
Conn ID:  1519
Conn ID:  1519
Conn ID:  1519
MySQL error:  commands out of sync. You can't run this command now
[mysql] 2016/04/21 11:55:38 packets.go:405: busy buffer
[mysql] 2016/04/21 11:55:38 packets.go:386: busy buffer
Conn ID:  11
Conn ID:  11
Conn ID:  11
Conn ID:  11

Configuration

Driver version (or git SHA):
"ImportPath": "github.com/go-sql-driver/mysql",
"Comment": "v1.2-194-g7ebe0a5",
"Rev": "7ebe0a500653eeb1859664bed5e48dec1e164e73"

Go version: run go version in your console
go version go1.6.2 linux/amd64

Server version: E.g. MySQL 5.6, MariaDB 10.0.20
Server version: 10.0.24-MariaDB MariaDB Server

Server OS: E.g. Debian 8.1 (Jessie), Windows 10
CentOS 6.7 x86_64

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions