Skip to content

Commit 75bef8c

Browse files
methanetz70s
authored andcommitted
README: Recommend some DB settings (go-sql-driver#1141)
1 parent 8650021 commit 75bef8c

File tree

1 file changed

+16
-1
lines changed

1 file changed

+16
-1
lines changed

README.md

+16-1
Original file line numberDiff line numberDiff line change
@@ -56,15 +56,31 @@ Make sure [Git is installed](https://git-scm.com/downloads) on your machine and
5656
_Go MySQL Driver_ is an implementation of Go's `database/sql/driver` interface. You only need to import the driver and can use the full [`database/sql`](https://golang.org/pkg/database/sql/) API then.
5757

5858
Use `mysql` as `driverName` and a valid [DSN](#dsn-data-source-name) as `dataSourceName`:
59+
5960
```go
6061
import "database/sql"
6162
import _ "github.com/go-sql-driver/mysql"
6263

6364
db, err := sql.Open("mysql", "user:password@/dbname")
65+
if err != nil {
66+
panic(err)
67+
}
68+
// See "Important settings" section.
69+
db.SetConnMaxLifetime(time.Minute * 3)
70+
db.SetMaxOpenConns(10)
71+
db.SetMaxIdleConns(10)
6472
```
6573

6674
[Examples are available in our Wiki](https://github.com/go-sql-driver/mysql/wiki/Examples "Go-MySQL-Driver Examples").
6775

76+
### Important settings
77+
78+
`db.SetConnMaxLifetime()` is required to ensure connections are closed by the driver safely before connection is closed by MySQL server, OS, or other middlewares. Since some middlewares close idle connections by 5 minutes, we recommend timeout shorter than 5 minutes. This setting helps load balancing and changing system variables too.
79+
80+
`db.SetMaxOpenConns()` is highly recommended to limit the number of connection used by the application. There is no recommended limit number because it depends on application and MySQL server.
81+
82+
`db.SetMaxIdleConns()` is recommended to be set same to (or greater than) `db.SetMaxOpenConns()`. When it is smaller than `SetMaxOpenConns()`, connections can be opened and closed very frequently than you expect. Idle connections can be closed by the `db.SetConnMaxLifetime()`. If you want to close idle connections more rapidly, you can use `db.SetConnMaxIdleTime()` since Go 1.15.
83+
6884

6985
### DSN (Data Source Name)
7086

@@ -496,4 +512,3 @@ Please read the [MPL 2.0 FAQ](https://www.mozilla.org/en-US/MPL/2.0/FAQ/) if you
496512
You can read the full terms here: [LICENSE](https://raw.github.com/go-sql-driver/mysql/master/LICENSE).
497513

498514
![Go Gopher and MySQL Dolphin](https://raw.github.com/wiki/go-sql-driver/mysql/go-mysql-driver_m.jpg "Golang Gopher transporting the MySQL Dolphin in a wheelbarrow")
499-

0 commit comments

Comments
 (0)