# go-sql-driver/mysql architecture

> Architecture diagram of the GitHub repository go-sql-driver/mysql, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2026-10-01). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/go-sql-driver/mysql
- Repository: https://github.com/go-sql-driver/mysql

## Overview

Go-MySQL-Driver implements Go’s database/sql driver interface. The principal workflow is an application opening the registered driver with a DSN, the driver establishing and authenticating a MySQL connection, then executing queries or prepared statements and exposing results through database/sql rows. The README also documents optional compression, time parsing, placeholder interpolation, and secure LOAD DATA LOCAL INFILE support. The map uses sampled code and README statements; some internal calls are shown as unverified where excerpts do not establish their exact wiring.

Main entry points: [`connection.go`](https://github.com/go-sql-driver/mysql/blob/master/connection.go) (Connection operations), [`connector.go`](https://github.com/go-sql-driver/mysql/blob/master/connector.go) (Connection setup), [`statement.go`](https://github.com/go-sql-driver/mysql/blob/master/statement.go) (Prepared statements), [`packets.go`](https://github.com/go-sql-driver/mysql/blob/master/packets.go) (Packet protocol)

## Diagram (Mermaid)

```mermaid
flowchart TD

subgraph group_setup["Configuration and entry"]
  node_driver["Driver registration<br/>[driver.go]"]
  node_dsn["DSN configuration<br/>[dsn.go]"]
  node_connector["Connection setup<br/>[connector.go]"]
end

subgraph group_protocol["Connection protocol"]
  node_auth["Authentication<br/>[auth.go]"]
  node_packets["Packet protocol<br/>[packets.go]"]
  node_connection["Connection operations<br/>[connection.go]"]
  node_collations["Collation mapping<br/>[collations.go]"]
end

subgraph group_queries["Query and results"]
  node_statement["Prepared statements<br/>[statement.go]"]
  node_rows["Result rows<br/>[rows.go]"]
  node_fields["Column metadata<br/>[fields.go]"]
  node_transactions["Transactions<br/>[transaction.go]"]
end

subgraph group_features["Optional capabilities"]
  node_infile["LOCAL INFILE<br/>[infile.go]"]
  node_compression["Zlib compression<br/>[compress.go]"]
  node_time["Time values<br/>[nulltime.go]"]
end

node_application(("Go application"))
node_sqlapi["database/sql API"]
node_mysql[("MySQL server")]

node_application -->|"uses"| node_sqlapi
node_sqlapi -->|"selects driver"| node_driver
node_sqlapi -->|"provides DSN"| node_dsn
node_driver -.->|"opens connection"| node_connector
node_connector -->|"dials"| node_mysql
node_connector -->|"authenticates"| node_auth
node_connector -.->|"exchanges handshake"| node_packets
node_auth -->|"sends auth packets"| node_packets
node_connection -.->|"sends commands"| node_packets
node_connection -->|"queries"| node_mysql
node_sqlapi -.->|"prepares"| node_statement
node_statement -.->|"executes prepared"| node_packets
node_connection -.->|"returns results"| node_rows
node_statement -.->|"returns results"| node_rows
node_rows -.->|"uses metadata"| node_fields
node_connection -->|"starts transaction"| node_transactions
node_connection -.->|"serves file request"| node_infile
node_connection -->|"uses when enabled"| node_compression
node_sqlapi -->|"scans time values"| node_time
node_dsn -->|"configures option"| node_compression
node_dsn -.->|"selects collation"| node_collations
node_application -->|"registers allowed files"| node_infile
node_application -->|"enables option"| node_compression
node_application -->|"uses interpolation"| node_statement

click node_driver "https://github.com/go-sql-driver/mysql/blob/master/driver.go"
click node_dsn "https://github.com/go-sql-driver/mysql/blob/master/dsn.go"
click node_connector "https://github.com/go-sql-driver/mysql/blob/master/connector.go"
click node_auth "https://github.com/go-sql-driver/mysql/blob/master/auth.go"
click node_packets "https://github.com/go-sql-driver/mysql/blob/master/packets.go"
click node_connection "https://github.com/go-sql-driver/mysql/blob/master/connection.go"
click node_statement "https://github.com/go-sql-driver/mysql/blob/master/statement.go"
click node_rows "https://github.com/go-sql-driver/mysql/blob/master/rows.go"
click node_fields "https://github.com/go-sql-driver/mysql/blob/master/fields.go"
click node_collations "https://github.com/go-sql-driver/mysql/blob/master/collations.go"
click node_transactions "https://github.com/go-sql-driver/mysql/blob/master/transaction.go"
click node_infile "https://github.com/go-sql-driver/mysql/blob/master/infile.go"
click node_compression "https://github.com/go-sql-driver/mysql/blob/master/compress.go"
click node_time "https://github.com/go-sql-driver/mysql/blob/master/nulltime.go"

classDef toneNeutral fill:#f8fafc,stroke:#334155,stroke-width:1.5px,color:#0f172a
classDef toneBlue fill:#dbeafe,stroke:#2563eb,stroke-width:1.5px,color:#172554
classDef toneAmber fill:#fef3c7,stroke:#d97706,stroke-width:1.5px,color:#78350f
classDef toneMint fill:#dcfce7,stroke:#16a34a,stroke-width:1.5px,color:#14532d
classDef toneRose fill:#ffe4e6,stroke:#e11d48,stroke-width:1.5px,color:#881337
classDef toneIndigo fill:#e0e7ff,stroke:#4f46e5,stroke-width:1.5px,color:#312e81
classDef toneTeal fill:#ccfbf1,stroke:#0f766e,stroke-width:1.5px,color:#134e4a
class node_driver,node_dsn,node_connector toneBlue
class node_auth,node_packets,node_connection,node_collations,node_sqlapi,node_mysql toneAmber
class node_statement,node_rows,node_fields,node_transactions toneMint
class node_infile,node_compression,node_time toneRose
class node_application toneIndigo
```

## Components

### Configuration and entry

- [Driver registration](https://github.com/go-sql-driver/mysql/blob/master/driver.go): `driver.go`
- [DSN configuration](https://github.com/go-sql-driver/mysql/blob/master/dsn.go): `dsn.go`
- [Connection setup](https://github.com/go-sql-driver/mysql/blob/master/connector.go): `connector.go`

### Connection protocol

- [Authentication](https://github.com/go-sql-driver/mysql/blob/master/auth.go): `auth.go`
- [Packet protocol](https://github.com/go-sql-driver/mysql/blob/master/packets.go): `packets.go`
- [Connection operations](https://github.com/go-sql-driver/mysql/blob/master/connection.go): `connection.go`
- [Collation mapping](https://github.com/go-sql-driver/mysql/blob/master/collations.go): `collations.go`

### Query and results

- [Prepared statements](https://github.com/go-sql-driver/mysql/blob/master/statement.go): `statement.go`
- [Result rows](https://github.com/go-sql-driver/mysql/blob/master/rows.go): `rows.go`
- [Column metadata](https://github.com/go-sql-driver/mysql/blob/master/fields.go): `fields.go`
- [Transactions](https://github.com/go-sql-driver/mysql/blob/master/transaction.go): `transaction.go`

### Optional capabilities

- [LOCAL INFILE](https://github.com/go-sql-driver/mysql/blob/master/infile.go): `infile.go`
- [Zlib compression](https://github.com/go-sql-driver/mysql/blob/master/compress.go): `compress.go`
- [Time values](https://github.com/go-sql-driver/mysql/blob/master/nulltime.go): `nulltime.go`

### Other components

- **Go application**
- **database/sql API**
- **MySQL server**

## Connections

- Go application → database/sql API: uses (evidence: `README.md`)
- database/sql API → Driver registration: selects driver (evidence: `README.md`)
- database/sql API → DSN configuration: provides DSN (evidence: `README.md`)
- Driver registration → Connection setup: opens connection
- Connection setup → MySQL server: dials (evidence: `connector.go`)
- Connection setup → Authentication: authenticates (evidence: `connector.go`)
- Connection setup → Packet protocol: exchanges handshake
- Authentication → Packet protocol: sends auth packets (evidence: `auth.go`)
- Connection operations → Packet protocol: sends commands
- Connection operations → MySQL server: queries (evidence: `README.md`)
- database/sql API → Prepared statements: prepares
- Prepared statements → Packet protocol: executes prepared
- Connection operations → Result rows: returns results
- Prepared statements → Result rows: returns results
- Result rows → Column metadata: uses metadata
- Connection operations → Transactions: starts transaction (evidence: `connection.go`)
- Connection operations → LOCAL INFILE: serves file request
- Connection operations → Zlib compression: uses when enabled
- database/sql API → Time values: scans time values (evidence: `README.md`)
- DSN configuration → Zlib compression: configures option (evidence: `dsn.go`)
- DSN configuration → Collation mapping: selects collation
- Go application → LOCAL INFILE: registers allowed files (evidence: `infile.go`)
- Go application → Zlib compression: enables option (evidence: `README.md`)
- Go application → Prepared statements: uses interpolation (evidence: `README.md`)
