Connecting to CloudSQL, AppEngine

56 views
Skip to first unread message

Alex Howard

unread,
Apr 20, 2015, 2:10:06 PM4/20/15
to golan...@googlegroups.com

I know this is a AppEngine topic, but this forum is the best Go forum.

Does anyone have an example of how to connect a Go AppEngine app to google cloud sql?

The below package gets me connecting to the server, but I am denied because I am forced to supply a user/pass when I dont need to because my appengine app is authenticated to use the database.

Received #1045 error from MySQL server: "Access denied for user 'root'@'localhost' (using password: YES)"

Someone must have an example lying around where they have done this, I searched for hours and there is nothing that works.

    "database/sql"
   _ "github.com/ziutek/mymysql/godrv"
   _ "github.com/ziutek/mymysql/mysql"
   _ "github.com/ziutek/mymysql/native"

func areaHandler(res http.ResponseWriter, r *http.Request, p httprouter.Params) {

    areaName
:= SHA1(p.ByName("areaName"))
    userName
:= SHA1("testuser")

    ds
:= DS(r)

    db
, err := sql.Open("mymysql", "cloudsql:my-project:app-sql*testdb/root/1234")
   
if err != nil { ds.Error("areaHandler", err); fmt.Fprintf(res, err.Error()); return }

    defer db
.Close()

    _
, err = db.Query("CREATE TABLE IF NOT EXISTS follow_area (uid int PRIMARY KEY AUTO_INCREMENT, area VARCHAR(40), user VARCHAR(16));")
   
if err != nil { ds.Error("areaHandler", err); fmt.Fprintf(res, err.Error()); return }

    _
, err = db.Query("SELECT * FROM follow_area WHERE user='"+userName+"' AND area='"+areaName+"';")
   
if err != nil { ds.Error("areaHandler", err); fmt.Fprintf(res, err.Error()) }
}



Nigel Tao

unread,
Apr 22, 2015, 12:31:24 AM4/22/15
to Alex Howard, golang-nuts
On Tue, Apr 21, 2015 at 4:10 AM, Alex Howard <3du...@gmail.com> wrote:
> I know this is a AppEngine topic, but this forum is the best Go forum.
>
> Does anyone have an example of how to connect a Go AppEngine app to google
> cloud sql?

I don't know the answer myself, but you might get a better response if
you asked on the google-appengine-go mailing list.

https://groups.google.com/forum/#!forum/google-appengine-go
Reply all
Reply to author
Forward
0 new messages