R
Railway16mo 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
Brody16mo ago
can you show a screenshot of your service variables
Byron
Byron16mo ago
Byron
Byron16mo ago
Tht DB_CONNECTION one isn't being used. I just added it to test
Brody
Brody16mo ago
show me the code that connects to the database please
Byron
Byron16mo ago
Byron
Byron16mo 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
Brody16mo ago
everything looks fine, i will do some tests and get back to you
Byron
Byron16mo ago
Okay thanks
Brody
Brody16mo 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
Byron16mo 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
Byron16mo 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
Brody16mo ago
can you show me where that error comes from @agi_marvin
Byron
Byron16mo ago
its also in the deploy logs
Brody
Brody16mo ago
i meant where in the code does that error get logged
Byron
Byron16mo ago
it doesnt. you're looking at the entire project pretty much in those 3 images above. this is the full main.go
Brody
Brody16mo ago
wdym it doesnt?? where is that error logged from??
Byron
Byron16mo ago
its not getting logged from anything in my project
Byron
Byron16mo ago
i dont know where its coming from. If its a gorm error, a railway error. searching it I get nothing
Brody
Brody16mo ago
you are printing an err returned from a function call, what print is printing that error
Byron
Byron16mo ago
Byron
Byron16mo ago
from gorm.Open
Brody
Brody16mo ago
the code in the production if block is not the same as mine
Brody
Brody16mo ago
gorm.Open(mysql.Open(u.DSN), &gorm.Config{})
gorm.Open(mysql.Open(u.DSN), &gorm.Config{})
Brody
Brody16mo ago
are you useing an outdated gorm package or something?
Byron
Byron16mo 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
Brody16mo ago
^
Byron
Byron16mo ago
im checking. i just started the project yesterday, so it shouldnt be
Brody
Brody16mo ago
Brody
Brody16mo 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
Byron16mo ago
alright, I'm updated to gorm.io
Byron
Byron16mo ago
tested it locally and its good there. Automigrate now failing on the railway db though
Brody
Brody16mo ago
okay next issue you are throwing out the *gorm.DB return value while on railway
Brody
Brody16mo ago
Brody
Brody16mo 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
Byron16mo 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
Brody16mo ago
yeah a nil pointer is not going to be a valid database source lol
Want results from more Discord servers?
Add your server