rdsmysql

package module
v2.2.0 Latest Latest
Warning

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

Go to latest
Published: Mar 11, 2024 License: MIT Imports: 12 Imported by: 0

README

GitHub Actions status PkgGoDev

rdsmysql

The rdsmysql package is a SQL driver that allows IAM Database Authentication for Amazon RDS and Amazon Aurora. It also supports connecting to the RDS proxy using IAM authentication.

rdsmysql v2 works with AWS SDK for Go v2:

import (
	"context"
	"database/sql"

	"github.com/aws/aws-sdk-go-v2/config"
	"github.com/go-sql-driver/mysql"
	"github.com/shogo82148/rdsmysql/v2"
)

func main() {
	// configure AWS SDK
	awsConfig, err := config.LoadDefaultConfig(context.TODO(), config.WithRegion("ap-northeast-1"))
	if err != nil {
		panic(err)
	}

	// configure the connector
	mysqlConfig, err := mysql.ParseDSN("user:@tcp(db-foobar.ap-northeast-1.rds.amazonaws.com:3306)/")
	if err != nil {
		panic(err)
	}
	connector := &rdsmysql.Connector{
		AWSConfig:   awsConfig,
		MySQLConfig: mysqlConfig,
	}

	// open the database
	db := sql.OpenDB(connector)
	defer db.Close()

	// ... do something using db ...
}

If you use AWS SDK for Go v1, use rdsmysql v1.

Documentation

Overview

Package rdsmysql is a MySQL SQL driver that allows IAM Database Authentication for Amazon RDS and IAM Database Authentication for Amazon Aurora. It also supports connecting with the RDS proxy using IAM authentication.

Index

Examples

Constants

View Source
const Certificates = rdsCertificates

Certificates is the certificates for connecting RDS MySQL with SSL/TLS. It contains the intermediate and root certificates for Amazon RDS MySQL and Amazon Aurora MySQL.

Variables

View Source
var TLSConfig *tls.Config

TLSConfig is the tls.TLSConfig for connecting RDS MySQL with SSL/TLS.

Functions

func Apply added in v2.2.0

func Apply(mysqlConfig *mysql.Config, awsConfig aws.Config) error

Apply applies the IAM DB Auth to mysqlConfig.

It overrides the following fields of mysqlConfig:

  • AllowCleartextPasswords: true
  • TLS: the certificate of Amazon RDS
  • Passwd: the auth token
  • BeforeConnect: refresh the auth token

Types

type Connector

type Connector struct {
	// AWSConfig is AWS Config.
	AWSConfig aws.Config

	// MySQLConfig is a configure for connecting to MySQL servers.
	MySQLConfig *mysql.Config

	// MaxConnsPerSecond is a limit for creating new connections.
	// Zero means no limit.
	MaxConnsPerSecond int
	// contains filtered or unexported fields
}

Connector is a MySQL connector using IAM DB Auth. It implements database/sql/driver.Connector.

Example
// configure AWS SDK
awsConfig, err := config.LoadDefaultConfig(context.TODO(), config.WithRegion("ap-northeast-1"))
if err != nil {
	panic(err)
}

// configure the connector
mysqlConfig, err := mysql.ParseDSN("user:@tcp(db-foobar.ap-northeast-1.rds.amazonaws.com:3306)/")
if err != nil {
	panic(err)
}
connector := &rdsmysql.Connector{
	AWSConfig:   awsConfig,
	MySQLConfig: mysqlConfig,
}

// open the database
db := sql.OpenDB(connector)
defer db.Close()

// ... do something using db ...
Output:

func (*Connector) Connect

func (c *Connector) Connect(ctx context.Context) (driver.Conn, error)

Connect returns a connection to the database.

func (*Connector) Driver

func (c *Connector) Driver() driver.Driver

Driver returns the underlying database/sql/driver.Driver of the Connector.

type Driver

type Driver struct {
	// AWSConfig is AWS Config.
	AWSConfig aws.Config
}

Driver is a MySQL driver using IAM DB Auth.

Example
// configure AWS SDK
awsConfig, err := config.LoadDefaultConfig(context.TODO(), config.WithRegion("ap-northeast-1"))
if err != nil {
	panic(err)
}

driver := &rdsmysql.Driver{
	AWSConfig: awsConfig,
}
sql.Register("rdsmysql", driver)

db, err := sql.Open("rdsmysql", "user:@tcp(db-foobar.ap-northeast-1.rds.amazonaws.com:3306)/")
if err != nil {
	panic(err)
}
defer db.Close()

// ... do something using db ...
Output:

func (*Driver) Open

func (d *Driver) Open(name string) (driver.Conn, error)

Open opens a new connection.

func (*Driver) OpenConnector

func (d *Driver) OpenConnector(name string) (driver.Connector, error)

OpenConnector opens a new connector.

Directories

Path Synopsis
cmd
internal

Jump to

Keyboard shortcuts

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