Git Product home page Git Product logo

hotswap-dsn-driver's Introduction

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 "github.com/go-mysql/hotswap-dsn-driver"

// 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(127.0.0.1)/"
})

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

// 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 github.com/go-sql-driver/mysql (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.

hotswap-dsn-driver's People

Contributors

daniel-nichter avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.