# Go-MySQL-Driver
A MySQL-Driver for Go's [database/sql](https://golang.org/pkg/database/sql/) package
![Go-MySQL-Driver logo](https://raw.github.com/wiki/go-sql-driver/mysql/gomysql_m.png "Golang Gopher holding the MySQL Dolphin")
---------------------------------------
* [Features](#features)
* [Requirements](#requirements)
* [Installation](#installation)
* [Usage](#usage)
* [DSN (Data Source Name)](#dsn-data-source-name)
* [Password](#password)
* [Protocol](#protocol)
* [Address](#address)
* [Parameters](#parameters)
* [Examples](#examples)
* [Connection pool and timeouts](#connection-pool-and-timeouts)
* [context.Context Support](#contextcontext-support)
* [ColumnType Support](#columntype-support)
* [LOAD DATA LOCAL INFILE support](#load-data-local-infile-support)
* [time.Time support](#timetime-support)
* [Unicode support](#unicode-support)
* [Testing / Development](#testing--development)
* [License](#license)
---------------------------------------
## Features
* Lightweight and [fast](https://github.com/go-sql-driver/sql-benchmark "golang MySQL-Driver performance")
* Native Go implementation. No C-bindings, just pure Go
* Connections over TCP/IPv4, TCP/IPv6, Unix domain sockets or [custom protocols](https://godoc.org/github.com/go-sql-driver/mysql#DialFunc)
* Automatic handling of broken connections
* Automatic Connection Pooling *(by database/sql package)*
* Supports queries larger than 16MB
* Full [`sql.RawBytes`](https://golang.org/pkg/database/sql/#RawBytes) support.
* Intelligent `LONG DATA` handling in prepared statements
* Secure `LOAD DATA LOCAL INFILE` support with file allowlisting and `io.Reader` support
* Optional `time.Time` parsing
* Optional placeholder interpolation
## Requirements
* Go 1.18 or higher. We aim to support the 3 latest versions of Go.
* MySQL (5.6+), MariaDB, Percona Server, Google CloudSQL or Sphinx (2.2.3+)
---------------------------------------
## Installation
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:
```bash
$ go get -u github.com/go-sql-driver/mysql
```
Make sure [Git is installed](https://git-scm.com/downloads) on your machine and in your system's `PATH`.
## Usage
_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.
Use `mysql` as `driverName` and a valid [DSN](#dsn-data-source-name) as `dataSourceName`:
```go
import (
"database/sql"
"time"
_ "github.com/go-sql-driver/mysql"
)
// ...
db, err := sql.Open("mysql", "user:password@/dbname")
if err != nil {
panic(err)
}
// See "Important settings" section.
db.SetConnMaxLifetime(time.Minute * 3)
db.SetMaxOpenConns(10)
db.SetMaxIdleConns(10)
```
[Examples are available in our Wiki](https://github.com/go-sql-driver/mysql/wiki/Examples "Go-MySQL-Driver Examples").
### Important settings
`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.
`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.
`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.
### DSN (Data Source Name)
The Data Source Name has a common format, like e.g. [PEAR DB](http://pear.php.net/manual/en/package.database.db.intro-dsn.php) uses it, but without type-prefix (optional parts marked by squared brackets):
```
[username[:password]@][protocol[(address)]]/dbname[?param1=value1&...¶mN=valueN]
```
A DSN in its fullest form:
```
username:password@protocol(address)/dbname?param=value
```
Except for the databasename, all values are optional. So the minimal DSN is:
```
/dbname
```
If you do not want to preselect a database, leave `dbname` empty:
```
/
```
This has the same effect as an empty DSN string:
```
```
`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:
```
/dbname%2Fwithslash
```
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.
#### Password
Passwords can consist of any character. Escaping is **not** necessary.
#### Protocol
See [net.Dial](https://golang.org/pkg/net/#Dial) for more information which networks are available.
In general you should use a Unix domain socket if available and TCP otherwise for best performance.
#### Address
For TCP and UDP networks, addresses have the form `host[:port]`.
If `port` is omitted, the default port will be used.
If `host` is a literal IPv6 address, it must be enclosed in square brackets.
The functions [net.JoinHostPort](https://golang.org/pkg/net/#JoinHostPort) and [net.SplitHostPort](https://golang.org/pkg/net/#SplitHostPort) manipulate addresses in this form.
For Unix domain sockets the address is the absolute path to the MySQL-Server-socket, e.g. `/var/run/mysqld/mysqld.sock` or `/tmp/mysql.sock`.
#### Parameters
*Parameters are case-sensitive!*
Notice that any of `true`, `TRUE`, `True` or `1` is accepted to stand for a true boolean value. Not surprisingly, false can be specified as any of: `false`, `FALSE`, `False` or `0`.
##### `allowAllFiles`
```
Type: bool
Valid Values: true, false
Default: false
```
`allowAllFiles=true` disables the file allowlist for `LOAD DATA LOCAL INFILE` and allows *all* files.
[*Might be insecure!*](https://dev.mysql.com/doc/refman/8.0/en/load-data.html#load-data-local)
##### `allowCleartextPasswords`
```
Type: bool
Valid Values: true, false
Default: false
```
`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.
##### `allowFallbackToPlaintext`
```
Type: bool
Valid Values: true, false
Default: false
```
`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)
##### `allowNativePasswords`
```
Type: bool
Valid Values: true, false
Default: true
```
`allowNativePasswords=false` disallows the usage of MySQL native password method.
##### `allowOldPasswords`
```
Type: bool
Valid Values: true, false
Default: false
```
`allowOldPasswords=true` allows the usage of the insecure old password met
没有合适的资源?快使用搜索试试~ 我知道了~
WEB服务编程-基于GO语言标准库实现简易贴吧
共138个文件
go:53个
sample:26个
head:8个
需积分: 0 1 下载量 109 浏览量
2023-10-31
14:56:44
上传
评论
收藏 1.72MB RAR 举报
温馨提示
WEB服务编程-基于GO语言标准库实现简易贴吧
资源推荐
资源详情
资源评论
收起资源包目录
WEB服务编程-基于GO语言标准库实现简易贴吧 (138个子文件)
3d9e45669dffbb7e7543282f40046ed2451f81 1KB
528b920b8e2ace2b144eccb8546ec5084312ae 1KB
56011a7cd68c7ba8484267fe5f46730dceeaac 26KB
6ac3b1b03066645b1f42ad50bea64f16980165 1KB
75348d98a9a245462ade75c6fde69424eb6fd4 656B
AUTHORS 5KB
c29ce0b1b8f84567de97ca0d32cebd53f05aa9 917B
config 301B
config 296B
description 73B
description 73B
e07e753d755b1310320e754b98b344d480b792 26KB
ecc0a63e2eb3143c930ba16714bc0752c28fef 3KB
exclude 240B
exclude 240B
FETCH_HEAD 1KB
.gitignore 93B
driver_test.go 96KB
auth_test.go 40KB
packets.go 36KB
utils.go 21KB
dsn.go 16KB
dsn_test.go 16KB
connection.go 15KB
utils_test.go 12KB
auth.go 11KB
benchmark_test.go 10KB
collations.go 9KB
packets_test.go 9KB
codec_test.go 7KB
generator.go 7KB
generator_test.go 6KB
codec.go 6KB
main.go 6KB
statement.go 6KB
fields.go 5KB
connector.go 5KB
rows.go 5KB
buffer.go 5KB
connection_test.go 5KB
infile.go 5KB
uuid.go 4KB
const.go 4KB
sql_test.go 4KB
driver.go 4KB
user.go 4KB
context.go 4KB
uuid_test.go 3KB
statement_test.go 3KB
errors.go 3KB
gow.go 3KB
prefix_tree.go 2KB
sql.go 2KB
group.go 2KB
nulltime.go 2KB
errors_test.go 2KB
nulltime_test.go 2KB
result.go 2KB
post.go 2KB
atomic_bool_test.go 1KB
atomic_bool_go118.go 1KB
conncheck.go 1KB
conncheck_test.go 1005B
session.go 977B
dsn_fuzz_test.go 920B
transaction.go 758B
atomic_bool.go 706B
connector_test.go 652B
conncheck_dummy.go 625B
comment.go 468B
HEAD 416B
HEAD 219B
HEAD 214B
HEAD 214B
HEAD 32B
HEAD 32B
HEAD 23B
HEAD 23B
post.html 2KB
sign.html 2KB
login.html 1KB
publish.html 1KB
user.html 966B
index.html 932B
navbar.html 477B
pack-ed6b08566a9b7312df7b4bfc44e08503dfaa5637.idx 98KB
pack-17e0ebf67e3abc241fb13d0f29d54061fb40f94c.idx 14KB
index 4KB
index 906B
LICENSE 17KB
LICENSE 1KB
master 416B
master 214B
master 197B
master 41B
master 41B
master 41B
README.md 25KB
CHANGELOG.md 11KB
README.md 2KB
共 138 条
- 1
- 2
资源评论
drinkwaterwater
- 粉丝: 8
- 资源: 1
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功