R
Railway17mo ago
Byron

Mysql + Golang App

So I've deployed a mysql database, and then deployed a golang api from github... I added a reference to the MYSQL_URL in the variables for my golang app. But my app cant connect to it. Its able to connect to a local db just fine. But i get these logs that the default addr for the mysql app is unknown then my app crashes.
36 Replies
Brody
Brody17mo ago
can you show a screenshot of your service variables
Byron
ByronOP17mo ago
Byron
ByronOP17mo ago
Tht DB_CONNECTION one isn't being used. I just added it to test
Brody
Brody17mo ago
show me the code that connects to the database please
Byron
ByronOP17mo ago
Byron
ByronOP17mo ago
I have MYSQL_URL set to a local MySQL db connection string in a local .env file. And it connects to that just fine
Brody
Brody17mo ago
everything looks fine, i will do some tests and get back to you
Byron
ByronOP17mo ago
Okay thanks
Brody
Brody17mo ago
package main

import (
"fmt"
"os"

"github.com/xo/dburl"
"gorm.io/driver/mysql"
"gorm.io/gorm"
)

func main() {
mysqlURL, ok := os.LookupEnv("MYSQL_URL")
if ok == false {
fmt.Println("MYSQL_URL not found")
return
}

u, err := dburl.Parse(mysqlURL + "?charset=utf8&parseTime=True&loc=Local")
if err != nil {
fmt.Println(err)
return
}

_, err = gorm.Open(mysql.Open(u.DSN), &gorm.Config{})
if err != nil {
fmt.Println(err)
return
}

fmt.Println("connected to database successfully")
}
package main

import (
"fmt"
"os"

"github.com/xo/dburl"
"gorm.io/driver/mysql"
"gorm.io/gorm"
)

func main() {
mysqlURL, ok := os.LookupEnv("MYSQL_URL")
if ok == false {
fmt.Println("MYSQL_URL not found")
return
}

u, err := dburl.Parse(mysqlURL + "?charset=utf8&parseTime=True&loc=Local")
if err != nil {
fmt.Println(err)
return
}

_, err = gorm.Open(mysql.Open(u.DSN), &gorm.Config{})
if err != nil {
fmt.Println(err)
return
}

fmt.Println("connected to database successfully")
}
MYSQL_URL is a URL, but gorm expects a DSN, so you can use github.com/xo/dburl to parse the DSN from the URL
Byron
ByronOP17mo ago
well, that gets me farther! its not crashing, new error though
invalid database source: &{0xc000282a10} is not a valid type
invalid database source: &{0xc000282a10} is not a valid type
Byron
ByronOP17mo ago
for some context, this is what the rest of my main.go looks like. After connecting to the db, I do an automigrate a bunch of gorm models (example in img 2). Then run a method for populating initial data in some of the tables (img 3). Then start a cron scheduler (that currently does nothing but print to the log), and then expose a graphql http route
Brody
Brody17mo ago
can you show me where that error comes from @agi_marvin
Byron
ByronOP17mo ago
its also in the deploy logs
Brody
Brody17mo ago
i meant where in the code does that error get logged
Byron
ByronOP17mo ago
it doesnt. you're looking at the entire project pretty much in those 3 images above. this is the full main.go
Brody
Brody17mo ago
wdym it doesnt?? where is that error logged from??
Byron
ByronOP17mo ago
its not getting logged from anything in my project
Byron
ByronOP17mo ago
i dont know where its coming from. If its a gorm error, a railway error. searching it I get nothing
Brody
Brody17mo ago
you are printing an err returned from a function call, what print is printing that error
Byron
ByronOP17mo ago
Byron
ByronOP17mo ago
from gorm.Open
Brody
Brody17mo ago
the code in the production if block is not the same as mine
Brody
Brody17mo ago
gorm.Open(mysql.Open(u.DSN), &gorm.Config{})
gorm.Open(mysql.Open(u.DSN), &gorm.Config{})
Brody
Brody17mo ago
are you useing an outdated gorm package or something?
Byron
ByronOP17mo ago
this block you included threw an error
_, err = gorm.Open(mysql.Open(u.DSN), &gorm.Config{})
if err != nil {
fmt.Println(err)
return
}
_, err = gorm.Open(mysql.Open(u.DSN), &gorm.Config{})
if err != nil {
fmt.Println(err)
return
}
Brody
Brody17mo ago
^
Byron
ByronOP17mo ago
im checking. i just started the project yesterday, so it shouldnt be
Brody
Brody17mo ago
Brody
Brody17mo ago
im using gorm.io/gorm yeah thats a fork of gorm from 2021 i know what the issue is now, but first update your code to use gorm.io/gorm
Byron
ByronOP17mo ago
alright, I'm updated to gorm.io
Byron
ByronOP17mo ago
tested it locally and its good there. Automigrate now failing on the railway db though
Brody
Brody17mo ago
okay next issue you are throwing out the *gorm.DB return value while on railway
Brody
Brody17mo ago
Brody
Brody17mo ago
also, instead of setting your own ENV variable, just use the one automatically provided by railway https://docs.railway.app/develop/variables#railway-provided-variables
Byron
ByronOP17mo ago
ah good catch. copy / paste failure on my part. we are up and running! thanks for your help! and yeah, i'll switch that over
Brody
Brody17mo ago
yeah a nil pointer is not going to be a valid database source lol
Want results from more Discord servers?
Add your server