douchui7332 2019-09-13 20:06
浏览 166
已采纳

仅当ECS时才能在Golang中连接到RDS mysql

I try to lunch application in ECS.
There is no problem when lunching in my local docker environment.
But it can't accrss to api server in ECS because of rds connection problem.

I use golang in api server and mysql for db.
I call db.go in main.go

func main() {

    db := db.NewDatabase(os.Getenv("MYSQL_USER"), os.Getenv("MYSQL_PASSWORD"), os.Getenv("MYSQL_HOST"))

Error occurs when connecting to rds database

func NewDatabase(user, password, host string) *Database {
    db, err := sql.Open("mysql", user+":"+password+"@tcp("+host+":3306)/article")
    if err != nil {
        panic(err.Error())
    }

    err = db.Ping()
    // error occurs here
    if err != nil {
        panic(err.Error())
    }

I deploy it to elastic beanstalk.
I checked environment variables are correctly set.

Here is the full source code:
https://github.com/jpskgc/article

I expect there is no error in elastic beans.
But the actual is not.
I want to know solution for that.

Here is the error log in elastic beanstalk.

-------------------------------------
/var/log/containers/server-4c66c8d1848a-stdouterr.log
-------------------------------------
panic: dial tcp 172.31.26.91:3306: connect: connection timed out

goroutine 1 [running]:
article/api/db.NewDatabase(0xc00002401b, 0x4, 0xc00002a00f, 0xb, 0xc00002800b, 0x3c, 0xdb94f2)
    /app/db/db.go:20 +0x3bc
main.main()
    /app/main.go:18 +0xee
  • 写回答

1条回答 默认 最新

  • dongzhina7098 2019-09-14 05:58
    关注

    "connection timed out" means that there are firewall limitation, and you can also check your mysql whitelist, which should has ip of your ECS.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 metadata提取的PDF元数据,如何转换为一个Excel
  • ¥15 关于arduino编程toCharArray()函数的使用
  • ¥100 vc++混合CEF采用CLR方式编译报错
  • ¥15 coze 的插件输入飞书多维表格 app_token 后一直显示错误,如何解决?
  • ¥15 vite+vue3+plyr播放本地public文件夹下视频无法加载
  • ¥15 c#逐行读取txt文本,但是每一行里面数据之间空格数量不同
  • ¥50 如何openEuler 22.03上安装配置drbd
  • ¥20 ING91680C BLE5.3 芯片怎么实现串口收发数据
  • ¥15 无线连接树莓派,无法执行update,如何解决?(相关搜索:软件下载)
  • ¥15 Windows11, backspace, enter, space键失灵