mysql

package
v1.3.3 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Sep 29, 2026 License: Apache-2.0 Imports: 11 Imported by: 1

README

mysql

This package registers a MySQL and MariaDB database/sql driver as mysql and selects its implementation at build time:

Build Backend
TinyGo or -tags force_tinygo_logic Petitweb tinygomysql
Standard Go github.com/go-sql-driver/mysql
import "github.com/shibukawa/tinygodriver/database/sql/mysql"

db, err := mysql.Open("user:pass@tcp(127.0.0.1:3306)/app")

The DSN syntax, driver name and database/sql behavior are the same on both backends, because the TinyGo backend is a fork of the standard-Go one. It exists because TinyGo's net.Conn cannot supply a descriptor for connection health checks or for an in-band TLS upgrade; see tinygomysql/README.md for the details and for the MPL-2.0 notice that covers that directory.

TLS

tls=true, tls=skip-verify and tls=preferred work on both backends. For a private CA, register the trust settings under a name and use it as tls=<name>:

ca, err := os.ReadFile("/etc/ssl/db-ca.pem")
if err != nil {
	log.Fatal(err)
}
err = mysql.RegisterTLSConfig("db", &https.Config{
	RootCAs:     [][]byte{ca},
	RootCAsOnly: true,
})
// ...
db, err := mysql.Open("user:pass@tcp(db.internal:3306)/app?tls=db")

RegisterTLSConfig takes an https.Config on both backends, so this code is portable; the standard-Go backend converts it to a crypto/tls.Config internally. PEM bytes are used rather than crypto/tls types because TinyGo builds must not link crypto/tls.

On TinyGo the handshake runs on the OS TLS stack through the https.DialPlain / https.Upgrade seam — Secure Transport on macOS, mbedTLS on Linux, Schannel on Windows. macOS caps at TLS 1.2 there, because Apple never added 1.3 to Secure Transport; build with -tags darwinstarttlswith13 for 1.3. Client certificates are unsupported on macOS.

TinyGo notes

Use the threads scheduler, which is the default on desktop targets. Under the cooperative scheduler (-scheduler=tasks) a blocking socket call holds the whole runtime, so the driver's cancellation watcher never runs and QueryContext ignores its deadline without reporting an error. Measured: a SELECT SLEEP(5) with a 500 ms deadline returned after the full five seconds with a nil error.

Blank-import netdev, as with any TinyGo program that uses the network:

import _ "github.com/shibukawa/tinygodriver/netdev"

Unix sockets and IPv6 are unavailable, so connect over TCP to an IPv4 host. The DSN timeout= parameter has no effect, because TinyGo's net.Dialer ignores both its Timeout field and the context. readTimeout, writeTimeout and query-level deadlines all work.

Documentation

Overview

Package mysql provides a MySQL and MariaDB database/sql driver that works under both TinyGo and standard Go.

Both builds are go-sql-driver/mysql. Standard Go uses it unmodified; TinyGo uses a fork under tinygomysql, because TinyGo's net.Conn supplies no descriptor for connection health checks or for an in-band TLS upgrade, and its crypto/tls is a stub. See tinygomysql/README.md.

db, err := mysql.Open("user:pass@tcp(127.0.0.1:3306)/app")
if err != nil { ... }
defer db.Close()

var n int
err = db.QueryRowContext(ctx, "SELECT 1").Scan(&n)

The DSN syntax, driver name and database/sql behavior are the same on both compilers: parameters, prepared statements, transactions, column metadata, and context cancellation.

TinyGo notes

Use the threads scheduler, which is the default on desktop targets. Under the cooperative scheduler (-scheduler=tasks) a blocking socket call holds the whole runtime, so the driver's cancellation watcher never runs and QueryContext ignores its deadline without reporting an error.

Import netdev for its side effect, as with any TinyGo program using the network:

import _ "github.com/shibukawa/tinygodriver/netdev"

Unix domain sockets and IPv6 are unavailable there, so connect over TCP to an IPv4 host. The DSN timeout parameter has no effect, because TinyGo's net.Dialer ignores both its Timeout field and the context; readTimeout, writeTimeout and query-level deadlines all work.

Index

Constants

View Source
const Backend = "go-sql-driver"

Backend identifies the implementation selected by build constraints.

View Source
const DriverName = "mysql"

DriverName is the portable database/sql driver name.

Variables

This section is empty.

Functions

func DeregisterTLSConfig

func DeregisterTLSConfig(name string)

DeregisterTLSConfig removes a configuration registered by RegisterTLSConfig.

func Open

func Open(dsn string) (*sql.DB, error)

Open opens a database handle for a go-sql-driver DSN.

The handle is lazy in the usual database/sql way: no connection is made until the first use. Call db.PingContext to verify the settings eagerly, or use OpenContext.

The tls parameter is honored on both builds. On TinyGo it is served by the platform's native TLS stack, which starts TLS on the already-connected socket after MySQL's capability exchange, so tls=true and a registered custom root both work. See RegisterTLSConfig.

func OpenContext

func OpenContext(ctx context.Context, dsn string) (*sql.DB, error)

OpenContext is Open plus an eager connectivity check, so configuration errors surface at open time instead of at first query.

func RegisterTLSConfig

func RegisterTLSConfig(name string, cfg *https.Config) error

RegisterTLSConfig registers trust settings under a name usable as tls=<name> in a DSN. This backend speaks crypto/tls, so the PEM bytes in cfg are parsed into a tls.Config here.

Types

This section is empty.

Directories

Path Synopsis
Package mysql provides a MySQL driver for Go's database/sql package.
Package mysql provides a MySQL driver for Go's database/sql package.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL