gpt4 book ai didi

mysql - 仅在ECS时无法连接到Golang中的RDS mysql

转载 作者:数据小太阳 更新时间:2023-10-29 03:36:00 25 4
gpt4 key购买 nike

我尝试在 ECS 中加载应用程序。
在我本地的docker环境下吃午饭是没有问题的。
但由于rds连接问题,无法访问ECS中的api服务器。

我在 api 服务器中使用 golang,在数据库中使用 mysql。
我在 main.go 中调用 db.go

func main() {

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

连接rds数据库出错

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())
}

我将它部署到弹性 beantalk。
我检查了环境变量是否设置正确。

这里是完整的源代码:
https://github.com/jpskgc/article

我希望弹性 bean 没有错误。
但实际情况并非如此。
我想知道解决方案。

这里是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

最佳答案

“connection timed out”表示有防火墙限制,你也可以查看你的mysql白名单,里面应该有你ECS的ip。

关于mysql - 仅在ECS时无法连接到Golang中的RDS mysql,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57929768/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com