R
Railway15mo ago
jd

failed opening connection to sql: default addr for network

Hi, I am trying to deploy my golang web service which connects to mysql. I have added the mysql url reference variable in my web service but currently see this error in my web service logs: failed opening connection to sql: default addr for network
19 Replies
Percy
Percy15mo ago
Project ID: 5b1457c9-94bf-4b38-9366-6949b7e8aa19
jd
jdOP15mo ago
project id - 5b1457c9-94bf-4b38-9366-6949b7e8aa19
Brody
Brody15mo ago
can I see a screenshot of your service variables please?
jd
jdOP15mo ago
Sure only the one variable at the moment
No description
Brody
Brody15mo ago
looks good to me show me the code that connects to the database please
jd
jdOP15mo ago
No description
Brody
Brody15mo ago
still looks good do you happen to be using the private network?
jd
jdOP15mo ago
Not that I am aware of . New to railway to be honest. Just created to new services, one the web app and another the mysql service.
Brody
Brody15mo ago
then that's basically a no go into the service settings and disable private networking
jd
jdOP15mo ago
Thanks done that and redeployed but still no joy. Same error in logs.
No description
Brody
Brody15mo ago
hmmm link me the library you are using for SQL and I'll see if I can whip something up
jd
jdOP15mo ago
Appreciate your help. https://entgo.io/docs/sql-integration/#use-pgx-with-postgresql Using second option currently under configure sql.DB
sql.DB Integration | ent
The following examples show how to pass a custom sql.DB object to ent.Client.
No description
Brody
Brody15mo ago
I think I know the problem, so I will get back to you with a definitive answer soon
jd
jdOP15mo ago
Amazing. Thanks again.
Brody
Brody15mo ago
alright, so sql.Open wants a connection string in DSN format, you are providing a URL format and URL and DSN formats are not interchangeable, unfortunately railway doesn’t offer a DSN connection string variable so we have to construct our own. set this service variable in the raw editor
MYSQL_DSN=${{MySQL.MYSQLUSER}}:${{MySQL.MYSQLPASSWORD}}@tcp(${{MySQL.MYSQLHOST}}:${{MySQL.MYSQLPORT}})/${{MySQL.MYSQLDATABASE}}
MYSQL_DSN=${{MySQL.MYSQLUSER}}:${{MySQL.MYSQLPASSWORD}}@tcp(${{MySQL.MYSQLHOST}}:${{MySQL.MYSQLPORT}})/${{MySQL.MYSQLDATABASE}}
and heres some example code
mysqlDSN := os.Getenv("MYSQL_DSN")

db, err := sql.Open("mysql", mysqlDSN)
// ...
mysqlDSN := os.Getenv("MYSQL_DSN")

db, err := sql.Open("mysql", mysqlDSN)
// ...
you of cource can still append your query parameters to mysqlDSN like you where originally doing
jd
jdOP15mo ago
Ahh good spot. I will give that a go now! Brilliant worked a treat.
Brody
Brody15mo ago
awsome
jd
jdOP15mo ago
Thank you.
Brody
Brody15mo ago
no problem!
Want results from more Discord servers?
Add your server