Golang, mysql: Error 1040: Too many connections

前端 未结 5 1674
终归单人心
终归单人心 2021-02-01 17:57

I\'m using the github.com/go-sql-driver/mysql driver for go.

I open a database:

db, err := sql.Open(\"mysql\", str)

Then I have two fun

5条回答
  •  爱一瞬间的悲伤
    2021-02-01 18:40

    The *DB object that you get back from sql.Open doesn't corresponds to a single connection. It is better thought as a handle for the database: it manages a connection pool for you.

    You can control the number of open connections with `(*DB).SetMaxOpenConns and its pair for idling connections.

    So basically what happens here is that db.Query and db.QueryRow tries to acquire a connection for themselves and the DB handle doesn't put any restrictions on the number of simultaneous connections so your code panics when it opens more than what mysql can handle.

提交回复
热议问题