douji1999 2017-02-19 20:59
浏览 119

使用Go在App Engine上进行GORM Cloud SQL连接

I'm trying to connect to a Cloud SQL database using GORM in golang.

db, _ = gorm.Open("mysql", "user:pass@cloudsql(connection:name:example)/")
if err != nil {
        log.Println(err)
        //panic(err)
    }

When I attempt to serve the app

goapp serve appengine/

I get a runtime error

ERROR    2017-02-19 20:48:05,436 http_runtime.py:396] bad runtime process port ['
']

Which I found was related to the database migration

db.AutoMigrate(&models.Event{})

If I remove the AutoMigrate, the runtime process port error goes away. However whenever I access a route (ie /events) that does a database query, the connection gets dropped, a 404 page is thrown, and an error message is logged sql: database is closed

When I run the app locally by building the package go build && ./appname and using a local MySQL server, it works fine.

Can someone please tell me how to connect to a Cloud SQL database using Go's GORM framework and App Engine?

  • 写回答

1条回答 默认 最新

  • doutuan9357 2017-02-20 21:43
    关注

    This is due to the call to log.New in this file: https://github.com/jinzhu/gorm/blob/master/logger.go#L15

    This anwser explain why dev_appserver.py gets it: https://stackoverflow.com/a/24112953/4266494

    To disable this, you can either disable all GORM logging:

    db.LogMode(false)
    

    Or use an adapter the logger output: https://github.com/benguild/GAEBridge/blob/master/log/debugLevel.go

    db.SetLogger(NewDebugLogger(nil)) // on application scope
    db.SetLogger(NewDebugLogger(appengine.NewContext(req))) // on request scope
    

    I'm setting a new logger with the real context This is the only workaround I found to avoid crashes while keeping some logs, it could be awesome if one of you had a real one.

    评论

报告相同问题?

悬赏问题

  • ¥15 r包runway详细安装教程
  • ¥15 Html中读取Json文件中数据并制作表格
  • ¥15 谁有RH342练习环境
  • ¥15 STM32F407 DMA中断问题
  • ¥15 uniapp连接阿里云无法发布消息和订阅
  • ¥25 麦当劳点餐系统代码纠错
  • ¥15 轮班监督委员会问题。
  • ¥20 关于变压器的具体案例分析
  • ¥15 生成的QRCode圖片加上下載按鈕
  • ¥15 板材切割优化算法,数学建模,python,lingo