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
Project ID:
N/A
please send the db.go file
can you show me what all those
log.Println
lines log, minus anything sensitive2024/07/12 02:11:05 MYSQLUSER: root
2024/07/12 02:11:05 MYSQLPORT: 32847
2024/07/12 02:11:05 MYSQLDATABASE: railw
ay
well i did say minus anything sensitive
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
your dsn variable is likely in the wrong format, try using the database url variable instead
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?
try passing the URL directly to mysql.open