MySQL
Create a MySQL connection using the NetLifeGuru MySQL driver and the shared DB layer.
Use github.com/netlifeguru/db-mysql when your application connects to MySQL or a MySQL-compatible database such as MariaDB.
The MySQL driver implements the shared db.Conn interface, so once the connection is created, the rest of your application can use the common db APIs such as List, Get, Value, Maps, Insert, Update, and Delete.
Install
Install the MySQL driver:
go get github.com/netlifeguru/db-mysqlInstalling the driver also automatically installs:
github.com/netlifeguru/db
github.com/netlifeguru/mapperImport
import (
"github.com/netlifeguru/db"
"github.com/netlifeguru/db-mysql"
)Connection Example
This example creates a MySQL connection pool from environment variables.
package main
import (
"os"
"strconv"
"time"
"github.com/netlifeguru/db"
"github.com/netlifeguru/db-mysql"
)
func connectDB() (db.Conn, error) {
conn := mysql.New()
host := os.Getenv("DB_HOST")
database := os.Getenv("DB_NAME")
username := os.Getenv("DB_USER")
password := os.Getenv("DB_PASSWORD")
port, err := strconv.Atoi(os.Getenv("DB_PORT"))
if err != nil {
return nil, err
}
cfg := db.Config{
Identifier: "default",
Host: host,
Port: port,
Database: database,
Username: username,
Password: password,
MaxConns: 50,
MinConns: 5,
MaxConnIdleTime: 10 * time.Minute,
MaxConnLifetime: 2 * time.Hour,
HealthCheckPeriod: 30 * time.Second,
ConnectTimeout: 10 * time.Second,
SSLMode: "false",
TimeZone: "Local",
}
if err := conn.CreatePool(cfg); err != nil {
return nil, err
}
return conn.Fork(), nil
}Environment Variables
The example above expects these environment variables:
DB_HOST=127.0.0.1
DB_PORT=3306
DB_NAME=app
DB_USER=root
DB_PASSWORD=secretMySQL Defaults
If values are not provided, the MySQL driver applies sensible defaults internally.
| Option | Default |
|---|---|
Identifier | default |
Host | 127.0.0.1 |
Port | 3306 |
MaxConns | 25 |
MinConns | 2 |
MaxConnIdleTime | 5m |
MaxConnLifetime | 1h |
ConnectTimeout | 5s |
HealthCheckPeriod | 30s |
SSLMode | false |
TimeZone | Local |
Placeholder Style
MySQL uses ? placeholders.
users, err := db.List[User](ctx, conn, `
SELECT * FROM users
WHERE active = ?
ORDER BY created_at DESC
`, true)The shared db helpers pass the arguments to the driver in order.
Insert Behavior
MySQL inserts commonly use db.Insert and read the generated ID from LastInsertId.
result, err := db.Insert(ctx, conn, `
INSERT INTO users (name, email, active)
VALUES (?, ?, ?)
`, name, email, active)
if err != nil {
return err
}
fmt.Println(result.LastInsertId(), result.RowsAffected())For PostgreSQL, the common pattern is different because PostgreSQL usually uses RETURNING id.
For Scylla, IDs are usually generated by the application and written into one or more query tables.
Transactions
The MySQL driver supports transactions through the shared transaction API.
Transaction usage is documented in the Transactions guide.
SQL Files
When using SQL model files, MySQL uses:
model.sqlThe shared db.LoadModel helper can load MySQL SQL sections from this file and store them in db.DialectSQL values.
SQL files are documented in the SQL Files guide.
Next Step
After creating a MySQL connection, continue with the shared querying guides:
- List, Get, Value, and Maps
- Query Objects
- Dialect SQL
- Mutations