rdsmysql

package module
v1.6.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 v1 works with AWS SDK for Go v1:

import (
	"database/sql"

	"github.com/aws/aws-sdk-go/aws"
	"github.com/aws/aws-sdk-go/aws/session"
	"github.com/shogo82148/rdsmysql"
)

func main() {
	// configure AWS session
	awsConfig := aws.NewConfig().WithRegion("ap-northeast-1")
	awsSession := session.Must(session.NewSession(awsConfig))

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

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

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

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

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 v1.6.0

func Apply(config *mysql.Config, session *session.Session) 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 {
	// Session is AWS Session.
	Session *session.Session

	// Config is a configure for connecting to MySQL servers.
	Config *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
package main

import (
	"database/sql"

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

func main() {
	// configure AWS session
	awsConfig := aws.NewConfig().WithRegion("ap-northeast-1")
	awsSession := session.Must(session.NewSession(awsConfig))

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

	// 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 {
	// Session is an AWS session
	Session *session.Session
}

Driver is a MySQL driver using IAM DB Auth.

Example
package main

import (
	"database/sql"

	"github.com/aws/aws-sdk-go/aws"
	"github.com/aws/aws-sdk-go/aws/session"
	"github.com/shogo82148/rdsmysql"
)

func main() {
	// register authentication information
	awsConfig := aws.NewConfig().WithRegion("ap-northeast-1")
	awsSession := session.Must(session.NewSession(awsConfig))
	driver := &rdsmysql.Driver{
		Session: awsSession,
	}
	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