NetLife Guru

Open source Go packages for fast, maintainable web systems. Built with a documentation-first approach.

Product
OverviewGolang packagesNews
Documentation
DocumentationGo LoggerGo RouterGo DB Form
Company
OverviewContactNewsGitHub
Community / Support
Supportinfo@netlife.guru
© 2026 NetLife Guru. All rights reserved.
GitHubinfo@netlife.guru
NetLife GuruNetLife GuruNetLife Guru
NetLife GuruNetLife GuruNetLife Guru
OverviewDocumentationNewsSupportContact

Golang packages

AboutMulti-DriverSQL Files
OverviewInstallationMySQLPostgreSQLScylla
DBGetting Started

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-mysql

Installing the driver also automatically installs:

github.com/netlifeguru/db
github.com/netlifeguru/mapper

Import

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=secret

MySQL Defaults

If values are not provided, the MySQL driver applies sensible defaults internally.

OptionDefault
Identifierdefault
Host127.0.0.1
Port3306
MaxConns25
MinConns2
MaxConnIdleTime5m
MaxConnLifetime1h
ConnectTimeout5s
HealthCheckPeriod30s
SSLModefalse
TimeZoneLocal

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.sql

The 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

Installation

Install a NetLifeGuru database driver and use the shared DB layer in your Go project.

PostgreSQL

Create a PostgreSQL connection using the NetLifeGuru PostgreSQL driver and the shared DB layer.

On this page

InstallImportConnection ExampleEnvironment VariablesMySQL DefaultsPlaceholder StyleInsert BehaviorTransactionsSQL FilesNext Step