DEV Community

box
box

Posted on

MySQL + Docker: Initial Setup

Environment

  • Mac

    • CPU: Apple M1
    • OS: Sonoma 14.6.1
  • Docker Desktop

Dir Structure

dir-structure

* Drawn by mermaid.

graph TB;
    subgraph "top"
        A["docker-compose.yml"]
        B[".env"]
    end
Enter fullscreen mode Exit fullscreen mode

docker-compose.yml

services:
  mysql:
    image: mysql:8.0.39
    environment:
      MYSQL_ROOT_PASSWORD: ${MYSQL_ROOT_PASSWORD}
    ports:
      - "${MYSQL_HOST_PORT}:3306"
    volumes:
      - db-data:/var/lib/mysql

volumes:
  db-data:
Enter fullscreen mode Exit fullscreen mode

Structure

First, Define required attributes.

services:
  mysql:
    image: mysql:8.0.39
    environment:
      MYSQL_ROOT_PASSWORD: ${MYSQL_ROOT_PASSWORD}
Enter fullscreen mode Exit fullscreen mode

Next, Mapping ports.

services:
  mysql:
    image: mysql:8.0.39
    environment:
      MYSQL_ROOT_PASSWORD: ${MYSQL_ROOT_PASSWORD}
    # new attribute.
    ports:
      - "${MYSQL_HOST_PORT}:3306"
Enter fullscreen mode Exit fullscreen mode

Finally, Make DB data persistent.

services:
  mysql:
    image: mysql:8.0.39
    environment:
      MYSQL_ROOT_PASSWORD: ${MYSQL_ROOT_PASSWORD}
    ports:
      - "${MYSQL_HOST_PORT}:3306"
    # new attribute.
    volumes:
      - db-data:/var/lib/mysql

# new element.
volumes:
  db-data:
Enter fullscreen mode Exit fullscreen mode

.env

MYSQL_HOST_PORT={host-port}
MYSQL_ROOT_PASSWORD={root-user-password}
Enter fullscreen mode Exit fullscreen mode

Docker

Create Container, Image, and Volume.

  1. Open the terminal.

  2. Move the current-dir to the location where docker-compose.yml is located.

  3. Execute docker compose create.

    docker compose create
    

Check the Version of MySQL.

  1. Start the container.

    docker compose start
    
  2. Check the name of the container.

    docker ps
    

    The Names column corresponds to it.

  3. Check the version in the container.

    docker exec <container-name> mysql -V
    

    For example, the following response is returned.

    mysql  Ver 8.0.39 for Linux on aarch64 (MySQL Community Server - GPL)
    
  4. Stop the container.

    docker compose stop
    

MySQL Workbench

Download.

  1. Download. workbench_download The version to be downloaded is 8.0.38. The M1 chip uses the ARM architecture, so the appropriate one is macOS (ARM, 64-bit), DMG Archive.

Add Connection.

  1. Start the container to connect to MySQL.

    docker compose start
    
  2. Open it and Click the PLUS button.
    Workbench_top

  3. The following dialog opens.
    workbench_setup-new-connection

    Enter any value for Connection Name, and update Port with .env.MYSQL_HOST_PORT.

  4. Click the Test Connection button.
    After entering .env.MYSQL_ROOT_PASSWORD, a connection test will be conducted.
    If successful, click the OK button to close this dialog.

  5. Stop the container.

    docker compose stop
    

Operate.

  1. Start the container.

    docker compose start
    
  2. Click the connection.
    workbench_top_after

  3. Enter .env.MYSQL_ROOT_PASSWORD if not saved in keychain.

  4. Operate until satisfied.
    workbench_main

  5. Stop the container.

    docker compose stop
    

Top comments (0)