Skip to content
Deploying an application in Go, MySQL
ednobremix
OP

10 months ago

I'm trying to set up an API in Go, with HTML and JavaScript as the frontend. It works locally, but I'd like support to deploy it to a server.

Solved$10 Bounty

Pinned Solution

ilyassbreth

10 months ago

heres who to deploy go + mysql on railway:

quick steps:

  1. add mysql database to your project (click + new → database → mysql)
  2. deploy your go app from github repo (+ new → github repo). railway auto-detects go projects from go.mod
  3. super important , make sure your go app listens on railway's PORT variable:

go

port := os.Getenv("PORT")
if port == "" {
    port = "8080"
}
http.ListenAndServe(":"+port, handler)
  1. connect to mysql using the env variables railway gives you automatically. in your go app use:

go

dsn := fmt.Sprintf("%s:%s@tcp(%s:%s)/%s",
    os.Getenv("MYSQLUSER"),
    os.Getenv("MYSQLPASSWORD"),
    os.Getenv("MYSQLHOST"),
    os.Getenv("MYSQLPORT"),
    os.Getenv("MYSQLDATABASE"),
)
  1. in your go service settings → variables tab, reference the mysql service variables
  2. generate domain under settings → networking

your html/js frontend just needs to be served by your go app and it'll deploy together. the PORT thing catches most people , railway assigns a random port so you gotta use their variable.

3 Replies

ilyassbreth

10 months ago

heres who to deploy go + mysql on railway:

quick steps:

  1. add mysql database to your project (click + new → database → mysql)
  2. deploy your go app from github repo (+ new → github repo). railway auto-detects go projects from go.mod
  3. super important , make sure your go app listens on railway's PORT variable:

go

port := os.Getenv("PORT")
if port == "" {
    port = "8080"
}
http.ListenAndServe(":"+port, handler)
  1. connect to mysql using the env variables railway gives you automatically. in your go app use:

go

dsn := fmt.Sprintf("%s:%s@tcp(%s:%s)/%s",
    os.Getenv("MYSQLUSER"),
    os.Getenv("MYSQLPASSWORD"),
    os.Getenv("MYSQLHOST"),
    os.Getenv("MYSQLPORT"),
    os.Getenv("MYSQLDATABASE"),
)
  1. in your go service settings → variables tab, reference the mysql service variables
  2. generate domain under settings → networking

your html/js frontend just needs to be served by your go app and it'll deploy together. the PORT thing catches most people , railway assigns a random port so you gotta use their variable.


ilyassbreth

10 months ago

lmk if you hit any snags, happy to help further!


ednobremix
OP

10 months ago

Thank you very much, I managed to solve it.


Status changed to Open brody • 11 months ago


Status changed to Solved brody • 11 months ago


Welcome!

Sign in to your Railway account to join the conversation.

Loading...