You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
* go-mysql would work with Percona Server, Google CloudSQL or Sphinx (2.2.3+).
51
+
* Maintainers won't support them. Do not expect issues are investigated and resolved by maintainers.
52
+
* Investigate issues yourself and please send a pull request to fix it.
45
53
46
54
---------------------------------------
47
55
48
56
## Installation
49
57
Simple install the package to your [$GOPATH](https://github.com/golang/go/wiki/GOPATH"GOPATH") with the [go tool](https://golang.org/cmd/go/"go command") from shell:
50
58
```bash
51
-
$ go get -u github.com/go-sql-driver/mysql
59
+
go get -u github.com/go-sql-driver/mysql
52
60
```
53
61
Make sure [Git is installed](https://git-scm.com/downloads) on your machine and in your system's `PATH`.
54
62
@@ -85,7 +93,7 @@ db.SetMaxIdleConns(10)
85
93
86
94
`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.
87
95
88
-
`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.
96
+
`db.SetMaxIdleConns()` is recommended to be set same to `db.SetMaxOpenConns()`. When it is smaller than `SetMaxOpenConns()`, connections can be opened and closed much more 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.
89
97
90
98
91
99
### DSN (Data Source Name)
@@ -114,14 +122,20 @@ This has the same effect as an empty DSN string:
114
122
115
123
```
116
124
125
+
`dbname` is escaped by [PathEscape()](https://pkg.go.dev/net/url#PathEscape) since v1.8.0. If your database name is `dbname/withslash`, it becomes:
126
+
127
+
```
128
+
/dbname%2Fwithslash
129
+
```
130
+
117
131
Alternatively, [Config.FormatDSN](https://godoc.org/github.com/go-sql-driver/mysql#Config.FormatDSN) can be used to create a DSN string by filling a struct.
118
132
119
133
#### Password
120
134
Passwords can consist of any character. Escaping is **not** necessary.
121
135
122
136
#### Protocol
123
137
See [net.Dial](https://golang.org/pkg/net/#Dial) for more information which networks are available.
124
-
In general you should use an Unix domain socket if available and TCP otherwise for best performance.
138
+
In general you should use a Unix domain socket if available and TCP otherwise for best performance.
125
139
126
140
#### Address
127
141
For TCP and UDP networks, addresses have the form `host[:port]`.
@@ -145,7 +159,7 @@ Default: false
145
159
```
146
160
147
161
`allowAllFiles=true` disables the file allowlist for `LOAD DATA LOCAL INFILE` and allows *all* files.
148
-
[*Might be insecure!*](http://dev.mysql.com/doc/refman/5.7/en/load-data-local.html)
162
+
[*Might be insecure!*](https://dev.mysql.com/doc/refman/8.0/en/load-data.html#load-data-local)
149
163
150
164
##### `allowCleartextPasswords`
151
165
@@ -157,6 +171,17 @@ Default: false
157
171
158
172
`allowCleartextPasswords=true` allows using the [cleartext client side plugin](https://dev.mysql.com/doc/en/cleartext-pluggable-authentication.html) if required by an account, such as one defined with the [PAM authentication plugin](http://dev.mysql.com/doc/en/pam-authentication-plugin.html). Sending passwords in clear text may be a security problem in some configurations. To avoid problems if there is any possibility that the password would be intercepted, clients should connect to MySQL Server using a method that protects the password. Possibilities include [TLS / SSL](#tls), IPsec, or a private network.
159
173
174
+
175
+
##### `allowFallbackToPlaintext`
176
+
177
+
```
178
+
Type: bool
179
+
Valid Values: true, false
180
+
Default: false
181
+
```
182
+
183
+
`allowFallbackToPlaintext=true` acts like a `--ssl-mode=PREFERRED` MySQL client as described in [Command Options for Connecting to the Server](https://dev.mysql.com/doc/refman/5.7/en/connection-options.html#option_general_ssl-mode)
184
+
160
185
##### `allowNativePasswords`
161
186
162
187
```
@@ -183,10 +208,9 @@ Valid Values: <name>
183
208
Default: none
184
209
```
185
210
186
-
Sets the charset used for client-server interaction (`"SET NAMES <value>"`). If multiple charsets are set (separated by a comma), the following charset is used if setting the charset failes. This enables for example support for `utf8mb4` ([introduced in MySQL 5.5.3](http://dev.mysql.com/doc/refman/5.5/en/charset-unicode-utf8mb4.html)) with fallback to `utf8` for older servers (`charset=utf8mb4,utf8`).
211
+
Sets the charset used for client-server interaction (`"SET NAMES <value>"`). If multiple charsets are set (separated by a comma), the following charset is used if setting the charset fails. This enables for example support for `utf8mb4` ([introduced in MySQL 5.5.3](http://dev.mysql.com/doc/refman/5.5/en/charset-unicode-utf8mb4.html)) with fallback to `utf8` for older servers (`charset=utf8mb4,utf8`).
187
212
188
-
Usage of the `charset` parameter is discouraged because it issues additional queries to the server.
189
-
Unless you need the fallback behavior, please use `collation` instead.
213
+
See also [Unicode Support](#unicode-support).
190
214
191
215
##### `checkConnLiveness`
192
216
@@ -215,6 +239,7 @@ The default collation (`utf8mb4_general_ci`) is supported from MySQL 5.5. You s
215
239
216
240
Collations for charset "ucs2", "utf16", "utf16le", and "utf32" can not be used ([ref](https://dev.mysql.com/doc/refman/5.7/en/charset-connection.html#charset-connection-impermissible-client-charset)).
217
241
242
+
See also [Unicode Support](#unicode-support).
218
243
219
244
##### `clientFoundRows`
220
245
@@ -268,13 +293,22 @@ Note that this sets the location for time.Time values but does not change MySQL'
268
293
269
294
Please keep in mind, that param values must be [url.QueryEscape](https://golang.org/pkg/net/url/#QueryEscape)'ed. Alternatively you can manually replace the `/` with `%2F`. For example `US/Pacific` would be `loc=US%2FPacific`.
270
295
296
+
##### `timeTruncate`
297
+
298
+
```
299
+
Type: duration
300
+
Default: 0
301
+
```
302
+
303
+
[Truncate time values](https://pkg.go.dev/time#Duration.Truncate) to the specified duration. The value must be a decimal number with a unit suffix (*"ms"*, *"s"*, *"m"*, *"h"*), such as *"30s"*, *"0.5m"* or *"1m30s"*.
304
+
271
305
##### `maxAllowedPacket`
272
306
```
273
307
Type: decimal number
274
-
Default: 4194304
308
+
Default: 64*1024*1024
275
309
```
276
310
277
-
Max packet size allowed in bytes. The default value is 4 MiB and should be adjusted to match the server settings. `maxAllowedPacket=0` can be used to automatically fetch the `max_allowed_packet` variable from server *on every connection*.
311
+
Max packet size allowed in bytes. The default value is 64 MiB and should be adjusted to match the server settings. `maxAllowedPacket=0` can be used to automatically fetch the `max_allowed_packet` variable from server *on every connection*.
278
312
279
313
##### `multiStatements`
280
314
@@ -284,9 +318,25 @@ Valid Values: true, false
284
318
Default: false
285
319
```
286
320
287
-
Allow multiple statements in one query. While this allows batch queries, it also greatly increases the risk of SQL injections. Only the result of the first query is returned, all other results are silently discarded.
321
+
Allow multiple statements in one query. This can be used to bach multiple queries. Use [Rows.NextResultSet()](https://pkg.go.dev/database/sql#Rows.NextResultSet) to get result of the second and subsequent queries.
288
322
289
-
When `multiStatements` is used, `?` parameters must only be used in the first statement.
323
+
When `multiStatements` is used, `?` parameters must only be used in the first statement. [interpolateParams](#interpolateparams) can be used to avoid this limitation unless prepared statement is used explicitly.
324
+
325
+
It's possible to access the last inserted ID and number of affected rows for multiple statements by using `sql.Conn.Raw()` and the `mysql.Result`. For example:
326
+
327
+
```go
328
+
conn, _:= db.Conn(ctx)
329
+
conn.Raw(func(conn any) error {
330
+
ex:= conn.(driver.Execer)
331
+
res, err:= ex.Exec(`
332
+
UPDATE point SET x = 1 WHERE y = 2;
333
+
UPDATE point SET x = 2 WHERE y = 3;
334
+
`, nil)
335
+
// Both slices have 2 elements.
336
+
log.Print(res.(mysql.Result).AllRowsAffected())
337
+
log.Print(res.(mysql.Result).AllLastInsertIds())
338
+
})
339
+
```
290
340
291
341
##### `parseTime`
292
342
@@ -382,6 +432,15 @@ Default: 0
382
432
383
433
I/O write timeout. The value must be a decimal number with a unit suffix (*"ms"*, *"s"*, *"m"*, *"h"*), such as *"30s"*, *"0.5m"* or *"1m30s"*.
384
434
435
+
##### `connectionAttributes`
436
+
437
+
```
438
+
Type: comma-delimited string of user-defined "key:value" pairs
[Connection attributes](https://dev.mysql.com/doc/refman/8.0/en/performance-schema-connection-attribute-tables.html) are key-value pairs that application programs can pass to the server at connect time.
385
444
386
445
##### System Variables
387
446
@@ -454,7 +513,7 @@ user:password@/
454
513
The connection pool is managed by Go's database/sql package. For details on how to configure the size of the pool and how long connections stay in the pool see `*DB.SetMaxOpenConns`, `*DB.SetMaxIdleConns`, and `*DB.SetConnMaxLifetime` in the [database/sql documentation](https://golang.org/pkg/database/sql/). The read, write, and dial timeouts for each individual connection are configured with the DSN parameters [`readTimeout`](#readtimeout), [`writeTimeout`](#writetimeout), and [`timeout`](#timeout), respectively.
455
514
456
515
## `ColumnType` Support
457
-
This driver supports the [`ColumnType` interface](https://golang.org/pkg/database/sql/#ColumnType) introduced in Go 1.8, with the exception of [`ColumnType.Length()`](https://golang.org/pkg/database/sql/#ColumnType.Length), which is currently not supported.
516
+
This driver supports the [`ColumnType` interface](https://golang.org/pkg/database/sql/#ColumnType) introduced in Go 1.8, with the exception of [`ColumnType.Length()`](https://golang.org/pkg/database/sql/#ColumnType.Length), which is currently not supported. All Unsigned database type names will be returned `UNSIGNED ` with `INT`, `TINYINT`, `SMALLINT`, `MEDIUMINT`, `BIGINT`.
458
517
459
518
## `context.Context` Support
460
519
Go 1.8 added `database/sql` support for `context.Context`. This driver supports query timeouts and cancellation via contexts.
@@ -467,7 +526,7 @@ For this feature you need direct access to the package. Therefore you must chang
467
526
import"github.com/go-sql-driver/mysql"
468
527
```
469
528
470
-
Files must be explicitly allowed by registering them with `mysql.RegisterLocalFile(filepath)` (recommended) or the allowlist check must be deactivated by using the DSN parameter `allowAllFiles=true` ([*Might be insecure!*](http://dev.mysql.com/doc/refman/5.7/en/load-data-local.html)).
529
+
Files must be explicitly allowed by registering them with `mysql.RegisterLocalFile(filepath)` (recommended) or the allowlist check must be deactivated by using the DSN parameter `allowAllFiles=true` ([*Might be insecure!*](https://dev.mysql.com/doc/refman/8.0/en/load-data.html#load-data-local)).
471
530
472
531
To use a `io.Reader` a handler function must be registered with `mysql.RegisterReaderHandler(name, handler)` which returns a `io.Reader` or `io.ReadCloser`. The Reader is available with the filepath `Reader::<name>` then. Choose different names for different handlers and `DeregisterReaderHandler` when you don't need it anymore.
473
532
@@ -485,9 +544,11 @@ However, many want to scan MySQL `DATE` and `DATETIME` values into `time.Time` v
485
544
### Unicode support
486
545
Since version 1.5 Go-MySQL-Driver automatically uses the collation ` utf8mb4_general_ci` by default.
487
546
488
-
Other collations / charsets can be set using the [`collation`](#collation) DSN parameter.
547
+
Other charsets / collations can be set using the[`charset`](#charset) or[`collation`](#collation) DSN parameter.
489
548
490
-
Version 1.0 of the driver recommended adding `&charset=utf8` (alias for `SET NAMES utf8`) to the DSN to enable proper UTF-8 support. This is not necessary anymore. The [`collation`](#collation) parameter should be preferred to set another collation / charset than the default.
549
+
- When only the `charset` is specified, the `SET NAMES <charset>` query is sent and the server's default collation is used.
550
+
- When both the `charset` and `collation` are specified, the `SET NAMES <charset> COLLATE <collation>` query is sent.
551
+
- When only the `collation` is specified, the collation is specified in the protocol handshake and the `SET NAMES` query is not sent. This can save one roundtrip, but note that the server may ignore the specified collation silently and use the server's default charset/collation instead.
491
552
492
553
See http://dev.mysql.com/doc/refman/8.0/en/charset-unicode.html for more details on MySQL's Unicode support.
0 commit comments