In a lot of instances, you need to have the ability to wait for a MySQL database to startup (e.g. when using in a Docker container). This is something which can easily be done with a simple Go program:

package main

import (


type discardLogger struct {

func (d discardLogger) Print(args ...interface{}) {

func main() {


    host := getenvWithDefault("HOST", "")
    port := getenvWithDefault("PORT", "3306")
    user := getenvWithDefault("USER", "root")
    passwd := getenvWithDefault("PASSWD", "")
    dbName := getenvWithDefault("DB_NAME", "")

    connectionString := user + ":" + passwd + "@tcp(" + host + ":" + port + ")/" + dbName

    fmt.Println("Waiting for:", user+"@tcp("+host+":"+port+")/"+dbName)

    for {


        db, err := sql.Open("mysql", connectionString)
        if err != nil {

        err = db.Ping()
        if err == nil {

        time.Sleep(1 * time.Second)



func getenvWithDefault(name string, defaultVal string) string {
    if val := os.Getenv(name); val != "" {
        return val
    return defaultVal

What we first do is to register a custom logger for the mysql package so that it doesn't output anything. Then we read the environment variables which contain the connection details. Then, we just ping the database every second until it succeeds. If it does, the program exits.

You can run it like this:

$ HOST= DB_NAME=my_database USER=root go run wait_for_db.go
Waiting for: root@tcp(

It can be easily modified to do the same for any other supported database engine.

Related Posts

  • Assert vs require in testify
  • Handling Unix timestamps in JSON
  • Parsing a key pair from a PEM file in Go
  • Pointer vs value receivers
  • The beauty of io.Reader in Go