Skip to main content

Connecting with Go

This guide explains how to establish a connection between a Go application and a MySQL database using the go-sql-driver/mysql package. It walks through the necessary setup, configuration, and execution of a simple SQL query.

Variables

Certain parameters must be provided to establish a successful connection to a MySQL database. Below is a breakdown of each required variable, its purpose, and where to find it. Here’s what each variable represents:

Variable

Description

Purpose

USER

MySQL username, from the Elestio service overview page

Identifies the database user who has permission to access the MySQL database.

PASSWORD

MySQL password, from the Elestio service overview page

The authentication key is required for the specified USER to access the database.

HOST

Hostname for MySQL connection, from the Elestio service overview page

The address of the server hosting the MySQL database.

PORT

Port for MySQL connection, from the Elestio service overview page

The network port used to connect to MySQL. The default port is 3306.

DATABASE

Database Name for MySQL connection, from the Elestio service overview page

The name of the database being accessed. A MySQL instance can contain multiple databases.

These values can usually be found in the Elestio service overview details, as shown in the image below. Make sure to take a copy of these details and add them to the code moving ahead.

image.png

Prerequisites

  • Install Go
    • Check if Go is installed by running: go version
    • If not installed, download it from golang.org and install.
  • Install the MySQL Driver
    • Use the following command to install the go-sql-driver/mysql driver: go get -u github.com/go-sql-driver/mysql

Code

Once all prerequisites are set up, create a new file named mysql_connect.go and add the following code:

package main

import (
	"database/sql"
	"fmt"
	"log"

	_ "github.com/go-sql-driver/mysql"
)

func main() {
	user := "USER"
	password := "PASSWORD"
	host := "HOST"
	port := "PORT"
	database := "DATABASE"

	// Construct DSN (Data Source Name)
	dsn := fmt.Sprintf("%s:%s@tcp(%s:%s)/%s", user, password, host, port, database)

	// Open a connection
	db, err := sql.Open("mysql", dsn)
	if err != nil {
		log.Fatalf("Connection failed: %v", err)
	}
	defer db.Close()

	// Ping to verify connection
	if err := db.Ping(); err != nil {
		log.Fatalf("Ping failed: %v", err)
	}
	fmt.Println("Connected to MySQL")

	// Run a test query to check the MySQL version
	var version string
	err = db.QueryRow("SELECT VERSION()").Scan(&version)
	if err != nil {
		log.Fatalf("Query execution failed: %v", err)
	}
	fmt.Printf("MySQL Version: %s\n", version)
}

To execute the script, open the terminal and navigate to the directory where mysql_connect.go is located. Once in the correct directory, run the script with the commands:

go mod init example.com/mysqlconnect
go run mysql_connect.go

If the connection is successful, the terminal will display output similar to:

Connected to MySQL
MySQL Version: 8.0.36