blob: 022e600cd903a5fb917ee45f234d468e474e65db [file] [log] [blame]
David Symondsccbe3942015-12-07 00:16:231// Copyright 2013 Google Inc. All rights reserved.
2// Use of this source code is governed by the Apache 2.0
3// license that can be found in the LICENSE file.
4
5/*
6Package cloudsql exposes access to Google Cloud SQL databases.
7
David Symondsa503df92016-03-23 02:00:388This package does not work in App Engine "flexible environment".
David Symondsccbe3942015-12-07 00:16:239
10This package is intended for MySQL drivers to make App Engine-specific
11connections. Applications should use this package through database/sql:
12Select a pure Go MySQL driver that supports this package, and use sql.Open
13with protocol "cloudsql" and an address of the Cloud SQL instance.
14
15A Go MySQL driver that has been tested to work well with Cloud SQL
16is the go-sql-driver:
Zev Goldstein0c7f7a82022-07-12 16:28:2917
David Symondsccbe3942015-12-07 00:16:2318 import "database/sql"
19 import _ "github.com/go-sql-driver/mysql"
20
21 db, err := sql.Open("mysql", "user@cloudsql(project-id:instance-name)/dbname")
22
David Symondsccbe3942015-12-07 00:16:2323Another driver that works well with Cloud SQL is the mymysql driver:
Zev Goldstein0c7f7a82022-07-12 16:28:2924
David Symondsccbe3942015-12-07 00:16:2325 import "database/sql"
26 import _ "github.com/ziutek/mymysql/godrv"
27
28 db, err := sql.Open("mymysql", "cloudsql:instance-name*dbname/user/password")
29
David Symondsccbe3942015-12-07 00:16:2330Using either of these drivers, you can perform a standard SQL query.
31This example assumes there is a table named 'users' with
32columns 'first_name' and 'last_name':
33
34 rows, err := db.Query("SELECT first_name, last_name FROM users")
35 if err != nil {
36 log.Errorf(ctx, "db.Query: %v", err)
37 }
38 defer rows.Close()
39
40 for rows.Next() {
41 var firstName string
42 var lastName string
43 if err := rows.Scan(&firstName, &lastName); err != nil {
44 log.Errorf(ctx, "rows.Scan: %v", err)
45 continue
46 }
47 log.Infof(ctx, "First: %v - Last: %v", firstName, lastName)
48 }
49 if err := rows.Err(); err != nil {
50 log.Errorf(ctx, "Row error: %v", err)
51 }
52*/
53package cloudsql
54
55import (
56 "net"
57)
58
59// Dial connects to the named Cloud SQL instance.
60func Dial(instance string) (net.Conn, error) {
61 return connect(instance)
62}