loading...

Playing with MySQL using Docker

dendihandian profile image Dendi Handian ・3 min read

Docker is wonderful and yet there is so much I don't know about it. That's why I've decided to create a series about it with the purpose of 'practices makes perfect' and share the wonderfulness to you.

The first service or docker image I want to play is the most widely-used open-source relational database management system, and maybe it's the first RDBMS you ever try to use. So, let's nostalgia with MySQL but now with Docker!

Prerequisites

You have docker and docker-compose installed in your machine.

Precautions

Everything I show you here is only for local development, not for production!

Pulling MySQL image

The default way to pull the image is using docker command docker pull mysql and it will pull (download) the latest version of the image. But if you want to pull it with a version for the consistency, you can check the list of the tags at https://hub.docker.com/_/mysql?tab=tags. The latest version I got is 8.0.20. So if you want to use the same version as me, then execute this command:

docker pull mysql:8.0.20

After the pull finished, you can check it whether it's on image list or not using:

docker image ls

And you should see this on top of the list:

REPOSITORY                                      TAG                     IMAGE ID            CREATED             SIZE  
mysql                                           8.0.20                  a7a67c95e831        2 days ago          541MB

Running MySQL Container the simple way (Docker Run)

The simple way using only one command below and execute it right away:

docker run --name my-awesome-mysql -p 3306:3306 -e MYSQL_ROOT_PASSWORD=my-secret-pw -d mysql:8.0.20

After executing the above command, you should see your container running using this command:

docker container ls

And here is my result:

CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS              PORTS                               NAMES
d4f4309b8037        mysql:8.0.20        "docker-entrypoint.s…"   4 seconds ago       Up 3 seconds        0.0.0.0:3306->3306/tcp, 33060/tcp   my-awesome-mysql

And now you should be able to access/connect to it with root as the username and my-secret-pw as the password using a MySQL client like MySQL Workbench, Valentina Studio, etc. But later I will show you to set up a MySQL web-client such as PhpMyAdmin or Adminer.

Running MySQL Container the manageable way (Docker Compose)

Executing the docker run command can be quite long to type. So to make the command even simpler and manageable, we can use docker-compose to manage the parameters and configurations.

First, let's create a folder called 'my-docker-services' and create a file named docker-compose.yml inside it. So the structure will be like this:

my-docker-services
|_ docker-compose.yml

And then fill the docker-compose.yml with this:

version: '3.1'

services:

  mysql:
    image: mysql:8.0.20
    command: --default-authentication-plugin=mysql_native_password
    restart: always
    ports:
        - 3306:3306
    environment:
      MYSQL_ROOT_PASSWORD: my-secret-pw

To run the container, simply use this command:

docker-compose up -d

And then you can check whether the container is running or not using docker container ls or by using this command:

docker-compose ps

And here is my result:

                Name                               Command               State                 Ports
------------------------------------------------------------------------------------------------------------------
my-docker-services_mysql_1   docker-entrypoint.sh --def ...   Up      0.0.0.0:3306->3306/tcp, 33060/tcp

And you still be able to access/connect to it using MySQL client.

Adding PhpMyAdmin to Docker-Compose


Make database persistent with Docker Volume



Have fun exploring MySQL with Docker.

Posted on by:

dendihandian profile

Dendi Handian

@dendihandian

Please teach me how to prettify database schema, implement any design patterns and do TDD in Drupal / Wordpress and how to make them not slow as ever.

Discussion

markdown guide