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 如何用Labview在myRIO上做LCD显示?(语言-开发语言)
  • ¥15 Vue3地图和异步函数使用
  • ¥15 C++ yoloV5改写遇到的问题
  • ¥20 win11修改中文用户名路径
  • ¥15 win2012磁盘空间不足,c盘正常,d盘无法写入
  • ¥15 用土力学知识进行土坡稳定性分析与挡土墙设计
  • ¥70 PlayWright在Java上连接CDP关联本地Chrome启动失败,貌似是Windows端口转发问题
  • ¥15 帮我写一个c++工程
  • ¥30 Eclipse官网打不开,官网首页进不去,显示无法访问此页面,求解决方法
  • ¥15 关于smbclient 库的使用