package cloudmysql
import "gocloud.dev/mysql/cloudmysql"
Package cloudmysql provides connections to managed MySQL Cloud SQL instances.
Code:play
Example¶
package main
import (
"context"
"gocloud.dev/gcp"
"gocloud.dev/gcp/cloudsql"
"gocloud.dev/mysql/cloudmysql"
"golang.org/x/oauth2"
"golang.org/x/oauth2/google"
)
func main() {
ctx := context.Background()
creds, err := google.FindDefaultCredentials(ctx, "https://www.googleapis.com/auth/cloud-platform")
if err != nil {
panic(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 {
panic(err)
}
// Use database in your program.
db.Exec("CREATE TABLE foo (bar INT);")
}
Index ¶
- Variables
- func NewCertSource(c *gcp.HTTPClient) *certs.RemoteCertSource
- func Open(ctx context.Context, certSource proxy.CertSource, params *Params) (*sql.DB, error)
- type Params
Examples ¶
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.
Functions ¶
func NewCertSource ¶
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 ¶
Open opens a Cloud SQL database.
Types ¶
type Params ¶
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.
Source Files ¶
cloudmysql.go
- Version
- v0.11.0
- Published
- Feb 28, 2019
- Platform
- js/wasm
- Imports
- 12 packages
- Last checked
- 37 minutes ago –
Tools for package owners.