connection refused

Hai im deploy using golang and have some little problem at here. i dont know how to solve it can you help me? 2024/07/11 19:04:12 /app/database/db.go:34 [error] failed to initialize database, got error dial tcp :0: connect: connection refused 2024/07/11 19:04:12 DB Connection Errordial tcp :0: connect: connection refused
10 Replies
Percy
Percy5mo ago
Project ID: N/A
Brody
Brody5mo ago
please send the db.go file
raii6926
raii6926OP5mo ago
ok, that the file of db
Brody
Brody5mo ago
can you show me what all those log.Println lines log, minus anything sensitive
raii6926
raii6926OP5mo ago
2024/07/12 02:11:05 MYSQLUSER: root 2024/07/12 02:11:05 MYSQLPORT: 32847 2024/07/12 02:11:05 MYSQLDATABASE: railway
Brody
Brody5mo ago
well i did say minus anything sensitive
raii6926
raii6926OP5mo ago
its okey so why the log in railways always say : [error] failed to initialize database, got error dial tcp :0: connect: connection refused 2024/07/11 19:06:42 DB Connection Errordial tcp :0: connect: connection refused container event container died
Brody
Brody5mo ago
your dsn variable is likely in the wrong format, try using the database url variable instead
raii6926
raii6926OP5mo ago
what is an example of a database url format ? i change be like this : databaseURL := os.Getenv("DATABASE_URL") var dsn string if databaseURL != "" { dsn = databaseURL } else { dbUsername := os.Getenv("MYSQLUSER") dbPassword := os.Getenv("MYSQLPASSWORD") dbHost := os.Getenv("MYSQLHOST") dbPort := os.Getenv("MYSQLPORT") dbName := os.Getenv("MYSQLDATABASE") // Debug prints to verify environment variables log.Println("MYSQLUSER:", dbUsername) log.Println("MYSQLPASSWORD:", dbPassword) log.Println("MYSQLHOST:", dbHost) log.Println("MYSQLPORT:", dbPort) log.Println("MYSQLDATABASE:", dbName) // Construct the DSN from individual environment variables dsn = dbUsername + ":" + dbPassword + "@tcp(" + dbHost + ":" + dbPort + ")/" + dbName + "?charset=utf8mb4&parseTime=True&loc=Local" } db, err := gorm.Open(mysql.Open(dsn), &gorm.Config{}) if err != nil { log.Fatal("DB Connection Error:", err) } its that okey? hi when i see deploy log it say : Starting Container 2024/07/11 19:21:40 MYSQLUSER: 2024/07/11 19:21:40 MYSQLPASSWORD: 2024/07/11 19:21:40 MYSQLHOST: 2024/07/11 19:21:40 MYSQLPORT: 2024/07/11 19:21:40 MYSQLDATABASE: 2024/07/11 19:21:40 /app/database/db.go:44 [error] failed to initialize database, got error dial tcp :0: connect: connection refused 2024/07/11 19:21:40 DB Connection Error:dial tcp :0: connect: connection refused the print not give the key from my .env its that the problem?
Brody
Brody5mo ago
try passing the URL directly to mysql.open
Want results from more Discord servers?
Add your server