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 R语言爬虫的时候元素和园代码不一样怎么解决呀
  • ¥15 VS2022多项目启动有问题
  • ¥15 SQL删除添加数据后序号不连续问题。
  • ¥15 首次运行OmniEvent运行报错
  • ¥15 有没有人知道这个问题怎么解决
  • ¥15 comsol电力电缆载流量仿真
  • ¥15 webSocket可以接TCP socket接口吗
  • ¥60 mpi并行出错,CFD++计算
  • ¥15 c#:vsto,powerpoint的外接程序中换主题颜色
  • ¥15 状态机/汽车转向灯/Sateflow