1 // Copyright 2012 The Go-MySQL-Driver Authors. All rights reserved.
3 // This Source Code Form is subject to the terms of the Mozilla Public
4 // License, v. 2.0. If a copy of the MPL was not distributed with this file,
5 // You can obtain one at http://mozilla.org/MPL/2.0/.
7 // Package mysql provides a MySQL driver for Go's database/sql package.
9 // The driver should be used via the database/sql package:
11 // import "database/sql"
12 // import _ "github.com/go-sql-driver/mysql"
14 // db, err := sql.Open("mysql", "user:password@/dbname")
16 // See https://github.com/go-sql-driver/mysql#usage for details
26 // MySQLDriver is exported to make the driver directly accessible.
27 // In general the driver is used via the database/sql package.
28 type MySQLDriver struct{}
30 // DialFunc is a function which can be used to establish the network connection.
31 // Custom dial functions must be registered with RegisterDial
32 type DialFunc func(addr string) (net.Conn, error)
35 dialsLock sync.RWMutex
36 dials map[string]DialFunc
39 // RegisterDial registers a custom dial function. It can then be used by the
40 // network address mynet(addr), where mynet is the registered new network.
41 // addr is passed as a parameter to the dial function.
42 func RegisterDial(net string, dial DialFunc) {
44 defer dialsLock.Unlock()
46 dials = make(map[string]DialFunc)
51 // Open new Connection.
52 // See https://github.com/go-sql-driver/mysql#dsn-data-source-name for how
53 // the DSN string is formated
54 func (d MySQLDriver) Open(dsn string) (driver.Conn, error) {
59 maxAllowedPacket: maxPacketSize,
60 maxWriteSize: maxPacketSize - 1,
61 closech: make(chan struct{}),
63 mc.cfg, err = ParseDSN(dsn)
67 mc.parseTime = mc.cfg.ParseTime
71 dial, ok := dials[mc.cfg.Net]
74 mc.netConn, err = dial(mc.cfg.Addr)
76 nd := net.Dialer{Timeout: mc.cfg.Timeout}
77 mc.netConn, err = nd.Dial(mc.cfg.Net, mc.cfg.Addr)
83 // Enable TCP Keepalives on TCP connections
84 if tc, ok := mc.netConn.(*net.TCPConn); ok {
85 if err := tc.SetKeepAlive(true); err != nil {
86 // Don't send COM_QUIT before handshake.
93 // Call startWatcher for context support (From Go 1.8)
96 mc.buf = newBuffer(mc.netConn)
99 mc.buf.timeout = mc.cfg.ReadTimeout
100 mc.writeTimeout = mc.cfg.WriteTimeout
102 // Reading Handshake Initialization Packet
103 authData, plugin, err := mc.readHandshakePacket()
109 plugin = defaultAuthPlugin
112 // Send Client Authentication Packet
113 authResp, addNUL, err := mc.auth(authData, plugin)
115 // try the default auth plugin, if using the requested plugin failed
116 errLog.Print("could not use requested auth plugin '"+plugin+"': ", err.Error())
117 plugin = defaultAuthPlugin
118 authResp, addNUL, err = mc.auth(authData, plugin)
124 if err = mc.writeHandshakeResponsePacket(authResp, addNUL, plugin); err != nil {
129 // Handle response to auth packet, switch methods if possible
130 if err = mc.handleAuthResult(authData, plugin); err != nil {
131 // Authentication failed and MySQL has already closed the connection
132 // (https://dev.mysql.com/doc/internals/en/authentication-fails.html).
133 // Do not send COM_QUIT, just cleanup and return the error.
138 if mc.cfg.MaxAllowedPacket > 0 {
139 mc.maxAllowedPacket = mc.cfg.MaxAllowedPacket
141 // Get max allowed packet size
142 maxap, err := mc.getSystemVar("max_allowed_packet")
147 mc.maxAllowedPacket = stringToInt(maxap) - 1
149 if mc.maxAllowedPacket < maxPacketSize {
150 mc.maxWriteSize = mc.maxAllowedPacket
154 err = mc.handleParams()
164 sql.Register("mysql", &MySQLDriver{})