Go MySQL Tutorial [Part 1: Installation]

Photo by Tobias Fischer on Unsplash

Information on how to access SQL databases with the Go programming language is quite easy to find on the Internet. I fully benefited from it, and I encountered a few difficulties that I managed to work around with sometimes a bit of time and sweat. I felt it was important to share my experience on this topic.

This little tutorial guides you through setting up a MySQL/MariaDB server and simple programming in Go. The idea is to present the minimum needed to have a working application. It is part of a small series of articles I wanted to write about databases.

Table of Contents

Introduction

SQL stands for Structured Query Language. It is a programming language that allows processing of relational databases. I won't go into detail about how this language works here. There are several articles on the Internet that will do it much better than me. I chose to use MySQL or MariaDB (a fork of MySQL). Maybe I'll do another one with SQLite or even PostgreSQL later. That said, the "database/sql" package works with these servers too. To learn more, you can find SQL drivers here.

Go is also a programming language. It is developed by Google and is increasingly used. It is a simple language inspired by C: indeed one of its designers is Ken Thompson. For my part, I discovered it because several of its designers/contributors worked on Plan9.

Installing MySQL

On Linux Ubuntu, as on Debian, installing a MySQL server is done (among other ways) with the apt package manager:

xigh@dev:~$ sudo apt install mysql-server
Reading package lists... Done
Building dependency tree
Reading state information... Done
The following additional packages will be installed:
  libevent-core-2.1-6 mysql-client-5.7 mysql-client-core-5.7 mysql-server-5.7 mysql-server-core-5.7
...
Upgrade process completed successfully.
Checking if update is needed.
Setting up mysql-server (5.7.26-0ubuntu0.18.10.1) ...

Next, you need to configure and secure your server (at least the passwords). This is done with the mysql_secure_installation command, which I invite you to read the documentation for:

xigh@dev:~$ mysql_secure_installation

Then we verify that access works:

xigh@dev:~$ mysql -u root -p
Enter password:

And here, for some, it's a disaster because the connection is refused:

Access denied for user 'root'@'localhost'

Yet the password was entered correctly. I found a solution here. For my part, here's an alternative way to proceed. Connect to the server via the sudo command:

xigh@dev:~$ sudo mysql -u root -p

Now, type the following SQL commands:

USE mysql;

UPDATE user SET authentication_string=PASSWORD("admin") WHERE User='root';

UPDATE user SET plugin="mysql_native_password";

Obviously, replace admin with the password of your choice. I strongly recommend using a random password generator.

Ready to move on to connecting to this database with Go?

Go MySQL Tutorial

  1. Go MySQL Tutorial [Part 1: Installation]
  2. Go MySQL Tutorial [Part 2: Connection and Configuration]
  3. Go MySQL Tutorial [Part 3: Data Transfer]
  4. Go MySQL Tutorial [Part 4: Transactions]