Use MySQL with GoLang

Maneesha Indrachapa
6 min readFeb 23, 2024

In this article we are going into the process of establishing a connection to a MySQL database in the Go programming language, using the capabilities of the Gorm package.

Using MySQL Server with Docker

Docker simplifies the process of setting up and managing databases, including MySQL. Follow the below steps to use MySQL server with Docker:

  1. Pull the MySQL Docker Image
    Open a terminal and run the following command to pull the official MySQL Docker image and check that the image is downloaded successfully.
> docker pull mysql/mysql-server
> docker images
Fig-1.1. Downloading the latest image of “mysql/mysql-server”

2. Run MySQL Container
Start a MySQL container using the pulled image. Replace your-mysql-container with your desired container name and your-mysql-password with your preferred MySQL root password.

> docker run -d --name your-mysql-container -e MYSQL_ROOT_PASSWORD=your-mysql-password -e MYSQL_ROOT_HOST='%' -p 3306:3306 mysql/mysql-server:latest
> docker ps

3. Access MySQL Container
You can access the MySQL container using a MySQL client or a tool like MySQL Workbench. Make sure to use the…

--

--