Sunday, September 23, 2018

Golang, mysql: Error 1040: Too many connections too many open files

Golang, mysql: Error 1040: Too many connections too many open files

  • Opening and closing databases can cause exhaustion of resources.
  • Failing to read all rows or use rows.Close() reserves connections from the pool.
  • Using Query() for a statement that doesn’t return rows will reserve a connection from the pool.
  • Failing to be aware of how prepared statements work can lead to a lot of extra database activity.

Check MySQL connections:

mysql> SHOW PROCESSLIST;

mysql> SHOW STATUS LIKE '%connections%';

mysql> SHOW STATUS LIKE '%threads%';

Check network connection files:

# lsof -i

Set some connection limits in Go code:

db.SetMaxOpenConns(1000)
db.SetMaxIdleConns(300)
db.SetConnMaxLifetime(0)

Release the resource after used:

rows, err := db.Query("select name from beehives")

if err != nil {
    panic(err)
}   
    
defer rows.Close()

Reference:

https://stackoverflow.com/questions/28135580/golang-mysql-error-1040-too-many-connections

http://go-database-sql.org/surprises.html

http://go-database-sql.org/prepared.html

https://access.redhat.com/solutions/1160343

No comments: