Skip to content


Switch branches/tags

Name already in use

A tag already exists with the provided branch name. Many Git commands accept both tag and branch names, so creating this branch may cause unexpected behavior. Are you sure you want to create this branch?

Latest commit


Git stats


Failed to load latest commit information.
Latest commit message
Commit time

MySQL Hotswap DSN Driver

Go Reference

This driver is a drop-in replacement for the real Go MySQL driver with one extra feature: it hotswaps the DSN on MySQL error 1045 (access denied). This allows frequently rotating the MySQL password without app downtime or "access denied" errors.

Since the hotswap is handled at the driver-level, you don't have to handle the "access denied" error everywhere the *sql.DB is used. Instead, a single hotswap callback function is set:

import dsndriver ""

// Set hotswap callback function
dsndriver.SetHotswapFunc(func(ctx context.Context, currentDSN string) (newDSN string) {
    // Reload latest DSN and return.
    // Be sure to respect ctx, too.
    return "user:new_pass@tcp("

db, err := sql.Open("mysql-hotswap-dsn", "user:pass@tcp(")

// Use db as usual

To use this driver, only two changes are required as shown above:

  1. Set the hotswap callback func by calling SetHotswapFunc
  2. Use driver name "mysql-hotswap-dsn" instead of "mysql": sql.Open("mysql-hotswap-dsn", "<dsn>")

When using this driver, you do not need to import (but you can if you need to).

The hotswap function is called only for MySQL error 1045. Other errors are ignored. This driver only implements driver- and connector-related interfaces. None of the query-related interfaces are implemented, which means this driver never interferes with queries, transactions, etc.

See the Go docs for additional information.