go-sql-driver/mysql

Irrelevant error when connection fails: busy buffer, commands out of sync

Closed this issue · 3 comments

I had a whole post written to explain my problem, but while writing I found the solution. The error however is very vague and led me to about 6h lost digging for a bug.

In my case the MySQL server was listening on a socket and not on localhost:3306, however connecting to the latter like below was giving an error only when executing a query, not when connecting. Nor was the error appropriate for the problem.

Example:

	db, err := sql.Open("mysql", "USER:PASSWORD@tcp(localhost:3306)/DBNAME?charset=utf8")
	if err != nil {
		log.Fatalln(err)
	}
	rows, err := db.Query("SELECT domain FROM domains")
	if err != nil {
		log.Fatalln(err) // error here
	}
	// ...

This fails with:

[mysql] 2025/01/27 21:44:02 connection.go:49: busy buffer
commands out of sync. Did you run multiple statements at once?

Using unix(/var/lib/mysql/mysql.sock) as the host fixes the problem. I'm running MariaDB 10.3.28

It is database/sql design. sql.Open() creates connection pool, not a connection.
If you want to check connection, call db.Ping().

Then why doesn't the error from Query say so? Why does it say busy buffer, commands out of sync, when it should say connection failed? That's not by design I may hope...

I don't know. Ask to your localhost:3306.