R
Railway9mo 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
Percy9mo ago
Project ID: 5b1457c9-94bf-4b38-9366-6949b7e8aa19
jd
jd9mo ago
project id - 5b1457c9-94bf-4b38-9366-6949b7e8aa19
Brody
Brody9mo ago
can I see a screenshot of your service variables please?
jd
jd9mo ago
Sure only the one variable at the moment
No description
Brody
Brody9mo ago
looks good to me show me the code that connects to the database please
jd
jd9mo ago
No description
Brody
Brody9mo ago
still looks good do you happen to be using the private network?
jd
jd9mo 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
Brody9mo ago
then that's basically a no go into the service settings and disable private networking
jd
jd9mo ago
Thanks done that and redeployed but still no joy. Same error in logs.
No description
Brody
Brody9mo ago
hmmm link me the library you are using for SQL and I'll see if I can whip something up
jd
jd9mo 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
Brody9mo ago
I think I know the problem, so I will get back to you with a definitive answer soon
jd
jd9mo ago
Amazing. Thanks again.
Brody
Brody9mo 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
jd9mo ago
Ahh good spot. I will give that a go now! Brilliant worked a treat.
Brody
Brody9mo ago
awsome
jd
jd9mo ago
Thank you.
Brody
Brody9mo ago
no problem!