I am getting the unexpected EOF and busy buffer
error in go-sql-driver/mysql
despite after setting the SetConnMaxLifetime
, SetMaxIdleConns
and SetMaxOpenConns
as suggested here. Can anyone tell me the proper solution of this issue nothing seems to work for me?
db, err := sql.Open("mysql", "USERNAME:PASSWORD@tcp(IP:PORT)/DB?charset=utf8")
checkErr(err)
db.SetConnMaxLifetime(time.Second * 5)
db.SetMaxIdleConns(0)
db.SetMaxOpenConns(151)
rows, err := db.Query("Select col1, col2, col3 from tbl")
checkErr(err)
for rows.Next() {
var col1 string
var col2 int32
var col3 uint64
err = rows.Scan(&col1, &col2, &col3)
checkErr(err)
Process(col1, col2, col3)
}
I setup a local MySQL database and ran your code:
package main
import (
"database/sql"
"fmt"
"log"
"time"
_ "github.com/go-sql-driver/mysql"
)
func main() {
db, err := sql.Open("mysql", "root@tcp(localhost)/test?charset=utf8")
if err != nil {
log.Fatalln(err)
}
db.SetConnMaxLifetime(time.Second * 5)
db.SetMaxIdleConns(0)
db.SetMaxOpenConns(151)
rows, err := db.Query("SELECT col1, col2, col3 FROM tbl2")
if err != nil {
log.Fatalln(err)
}
for rows.Next() {
var col1 string
var col2 int32
var col3 uint64
err = rows.Scan(&col1, &col2, &col3)
if err != nil {
log.Fatalln(err)
}
fmt.Println(col1, col2, col3)
}
}
..and it worked just fine for me. My CREATE TABLE
statement looks like this:
CREATE TABLE `tbl2` (
`col1` varchar(25) DEFAULT NULL,
`col2` int(11) DEFAULT NULL,
`col3` bigint(20) unsigned DEFAULT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
What does your table structure look like?
I found two ways for work arround the packets.go:36: unexpected EOF
error, first way was changing driver to ziutek/mymysql
driver, works ok and more or less with the same performance.
Second way, with the default driver, is to set db.SetMaxIdleConns(100)
and db.SetMaxOpenConns(100)
, my mysql has max_connections to 151, so I thought that limit to 100 will be ok.
In addition, prepared statements has speeded up a lot, before they were slower than Db.Query, now are at least twice as fast. (Tested with 200k queries in 20 goroutines)
I think the problem could be in file buffer.go
of driver go-sql-driver/mysql
, in line: nn, err := b.nc.Read(b.buf[n:])
, some kind of timeout.