gocloud.dev: gocloud.dev/mysql/cloudmysql Index | Examples | Files

package cloudmysql

import "gocloud.dev/mysql/cloudmysql"

Package cloudmysql provides connections to managed MySQL Cloud SQL instances.

Code:

ctx := context.Background()
creds, err := google.FindDefaultCredentials(ctx, "https://www.googleapis.com/auth/cloud-platform")
if err != nil {
    log.Fatal(err)
}
authClient := gcp.HTTPClient{Client: *oauth2.NewClient(ctx, creds.TokenSource)}
db, _, err := cloudmysql.Open(ctx, cloudsql.NewCertSource(&authClient), &cloudmysql.Params{
    // Replace these with your actual settings.
    ProjectID: "example-project",
    Region:    "us-central1",
    Instance:  "my-instance01",
    User:      "myrole",
    Database:  "test",
})
if err != nil {
    log.Fatal(err)
}

// Use database in your program.
db.Exec("CREATE TABLE foo (bar INT);")

Index

Examples

Package Files

cloudmysql.go

Variables

var CertSourceSet = cloudsql.CertSourceSet

CertSourceSet is a Wire provider set that binds a Cloud SQL proxy certificate source from an GCP-authenticated HTTP client.

Deprecated: Use cloudsql.CertSourceSet.

func NewCertSource Uses

func NewCertSource(c *gcp.HTTPClient) *certs.RemoteCertSource

NewCertSource creates a local certificate source that uses the given HTTP client. The client is assumed to make authenticated requests.

Deprecated: Use cloudsql.NewCertSource.

func Open Uses

func Open(ctx context.Context, certSource proxy.CertSource, params *Params) (*sql.DB, func(), error)

Open opens a Cloud SQL database. The second return value is a cleanup function that calls Close on the returned database.

type Params Uses

type Params struct {
    // ProjectID specifies the GCP project associated with the
    // CloudSQL instance.
    ProjectID string

    // Region is the GCP region containing the CloudSQL instance.
    Region string

    // Instance is the CloudSQL instance name. See
    // https://cloud.google.com/sql/docs/mysql/create-instance
    // for background.
    Instance string

    // User is the username used to connect to the database.
    User string

    // Password is the password used to connect to the database.
    // It may be empty, see https://cloud.google.com/sql/docs/sql-proxy#user
    Password string

    // Database is the name of the database to connect to.
    Database string

    // TraceOpts contains options for OpenCensus.
    TraceOpts []ocsql.TraceOption
}

Params specifies how to connect to a Cloud SQL database.

Package cloudmysql imports 12 packages (graph) and is imported by 2 packages. Updated 2019-05-02. Refresh now. Tools for package owners.