...

Text file src/cloud.google.com/go/cloudsqlconn/migration-guide.md

Documentation: cloud.google.com/go/cloudsqlconn

     1# Migrating from Cloud SQL Proxy v1 to the Go Connector
     2
     3The Go Connector supports an improved version of the drivers available in v1.
     4Unlike V1, the Go Connectors drivers support:
     5
     61. Configuring a driver with all supported Go connector options
     71. Configuring multiple drivers per engine type using distinct registered driver
     8   names
     91. Support for SQL Server
    101. (Postgres only) Configuring a connection using pgx directly (see README for
    11   details).
    12
    13Below are examples of the Cloud SQL Proxy invocation vs the new Go connector
    14invocation.
    15
    16## MySQL
    17
    18### Cloud SQL Proxy
    19
    20``` golang
    21import (
    22	"database/sql"
    23
    24	"github.com/GoogleCloudPlatform/cloudsql-proxy/proxy/dialers/mysql"
    25)
    26
    27func connectMySQL() *sql.DB {
    28	cfg := mysql.Cfg("project:region:instance", "user", "password")
    29	cfg.DBName = "DB_1"
    30	cfg.ParseTime = true
    31
    32	db, err := mysql.DialCfg(cfg)
    33	if err != nil {
    34		// handle error as necessary
    35	}
    36	return db
    37}
    38```
    39
    40### Cloud SQL Go Connector
    41
    42``` golang
    43import (
    44	"database/sql"
    45
    46	"cloud.google.com/go/cloudsqlconn"
    47	"cloud.google.com/go/cloudsqlconn/mysql/mysql"
    48)
    49
    50func connectMySQL() *sql.DB {
    51	// Register a driver using whatever name you like.
    52	cleanup, err := mysql.RegisterDriver(
    53		"cloudsql-mysql",
    54		// any desired options go here, for example:
    55		cloudsqlconn.WithCredentialsFile("key.json"),
    56	)
    57	if err != nil {
    58		// handle error as necessary
    59	}
    60	// call cleanup to close the underylying driver when you're done with the
    61	// db.
    62	defer cleanup()
    63
    64	db, err := sql.Open(
    65		"cloudsql-mysql", // matches the name registered above
    66		"myuser:mypass@cloudsql-mysql(project:region:instance)/mydb",
    67	)
    68	if err != nil {
    69		// handle error as necessary
    70	}
    71	return db
    72}
    73```
    74
    75## Postgres
    76
    77### Cloud SQL Proxy
    78
    79``` golang
    80import (
    81	"database/sql"
    82
    83	_ "github.com/GoogleCloudPlatform/cloudsql-proxy/proxy/dialers/postgres"
    84)
    85func connectPostgres() *sql.DB {
    86	db, err := sql.Open(
    87		"cloudsqlpostgres",
    88		"host=project:region:instance user=postgres dbname=postgres password=password sslmode=disable",
    89	)
    90	if err != nil {
    91		// handle error as necessary
    92	}
    93	return db
    94}
    95```
    96
    97### Cloud SQL Go Connector
    98
    99``` golang
   100import (
   101	"database/sql"
   102
   103	"cloud.google.com/go/cloudsqlconn"
   104	"cloud.google.com/go/cloudsqlconn/postgres/pgxv4"
   105)
   106
   107func connectPostgres() *sql.DB {
   108	// Register a driver using whatever name you like.
   109	cleanup, err := pgxv4.RegisterDriver(
   110		"cloudsql-postgres",
   111		// any desired options go here, for example:
   112		cloudsqlconn.WithCredentialsFile("key.json"),
   113		cloudsqlconn.WithIAMAuthN(),
   114	)
   115	if err != nil {
   116		// handle error as necessary
   117	}
   118	// call cleanup to close the underylying driver when you're done with the
   119	// db.
   120	defer cleanup()
   121	db, err := sql.Open(
   122		"cloudsql-postgres", // matches the name registered above
   123		"host=project:region:instance user=postgres password=password dbname=postgres sslmode=disable",
   124	)
   125	if err != nil {
   126		// handle error as necessary
   127	}
   128	return db
   129}
   130```
   131
   132## SQL Server
   133
   134### Cloud SQL Proxy
   135
   136The Cloud SQL Proxy does not support SQL Server as a driver.
   137
   138### Cloud SQL Go Connector
   139
   140``` golang
   141import (
   142	"database/sql"
   143
   144	"cloud.google.com/go/cloudsqlconn"
   145	"cloud.google.com/go/cloudsqlconn/sqlserver/mssql"
   146)
   147
   148func connectSQLServer() *sql.DB {
   149	// Register a driver using whatever name you like.
   150	cleanup, err := mssql.RegisterDriver(
   151		"cloudsql-sqlserver",
   152		// any desired options go here, for example:
   153		cloudsqlconn.WithCredentialsFile("key.json"),
   154	)
   155	if err != nil {
   156		// handle error as necessary
   157	}
   158	// call cleanup when you're done with the database connection
   159	defer cleanup()
   160
   161	db, err := sql.Open(
   162		"cloudsql-sqlserver", // matches the name registered above
   163		"sqlserver://user:password@localhost?database=mydb&cloudsql=project:region:instance",
   164	)
   165	if err != nil {
   166		// handle error as necessary
   167	}
   168	return db
   169}
   170```

View as plain text