
ユウ
こんにちは、ユウです。
DockerでMySQL環境を構築したので、忘備録がてらまとめておきます。
本記事を書くにあたり、以下の記事を参考にさせていただきました。ありがとうございます!
環境情報
今回、作業した環境は以下のとおりです。
| 分類 | 環境 | 
| PC | MacBook Pro(M1, 2020) | 
| OS | macOS Sonoma(バージョン14.5) | 
| Docker | 26.1.4 | 
| Docker Compose | v2.27.1-desktop.1 | 
MySQL環境の構築
MySQLコンテナの設定は以下とします。
| 項目 | 設定 | 
| コンテナ名 | mysql-container | 
| データベース名 | test | 
| rootのパスワード | root | 
| TimeZone | Asia/Tokyo | 
Docker Composeを使います。docker-compose.ymlを以下のように作成します。
# docker-compose.yml
volumes:
  mysql-data:
services:
  mysql:
    image: mysql
    container_name: mysql-container
    ports:
      - "3306:3306"
    environment:
      MYSQL_ROOT_PASSWORD: root
      MYSQL_DATABASE: test
      TZ: Asia/Tokyo
    volumes:
      - mysql-data:/var/lib/mysql以下の各コマンドを実行します。
# docker-composeでコンテナを起動
docker-compose up -d
# 実行中のコンテナ一覧
docker ps
# docker-composeでコンテナの停止と削除
docker-compose down実行結果は以下のとおりです。
% docker-compose up -d
[+] Running 11/11
 ✔ mysql Pulled                                                                                                    77.1s
   ✔ 4b19fb6eb45f Pull complete                                                                                    65.6s
   ✔ 51bcce6402af Pull complete                                                                                    65.6s
   ✔ 6133ef8f5f93 Pull complete                                                                                    65.6s
   ✔ 8e4d202196eb Pull complete                                                                                    65.8s
   ✔ 3c44f653f1c4 Pull complete                                                                                    65.8s
   ✔ b394d9fd8cd5 Pull complete                                                                                    65.8s
   ✔ c130709c94e5 Pull complete                                                                                    67.2s
   ✔ 06ef2860059f Pull complete                                                                                    67.3s
   ✔ f6c4e968e873 Pull complete                                                                                    73.9s
   ✔ 5d02ede93afa Pull complete                                                                                    73.9s
[+] Running 1/1
 ✔ Container mysql-container  Started
 
% docker ps
CONTAINER ID   IMAGE                           COMMAND                   CREATED         STATUS         PORTS                               NAMES
0158f014bef2   mysql                           "docker-entrypoint.s…"   7 seconds ago   Up 6 seconds   0.0.0.0:3306->3306/tcp, 33060/tcp   mysql-containerMySQLコンテナの確認
MySQLコンテナの中に入り、状態を確認します。
# コンテナの中に入る
% docker exec -it mysql-container bash
# ログインユーザーの確認
id
uid=0(root) gid=0(root) groups=0(root)
# MySQLのversion確認
mysql --version
mysql  Ver 9.0.1 for Linux on aarch64 (MySQL Community Server - GPL)
# MySQLにrootユーザーとしてログイン
# passwordを聞かれるのでrootと入力
mysql -u root -p
Enter password:
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 8
Server version: 9.0.1 MySQL Community Server - GPL
Copyright (c) 2000, 2024, Oracle and/or its affiliates.
Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
mysql>MySQLで下記を実行してみます。
# detabaseの一覧表示
mysql> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| mysql              |
| performance_schema |
| sys                |
| test               |
+--------------------+
5 rows in set (0.03 sec)
# databaseとして今回作成したtestを使う
mysql> use test
Database changed
# テーブル一覧を確認
mysql> show tables;
Empty set (0.01 sec)無事、中身を確認できました。
Sequel AceでMySQLコンテナに接続する
データベース管理アプリケーションのSequel AceでMySQLに接続してみます。
以下のように入力すると、MySQLに接続できます。

Passwordはrootです。
MySQLへの接続後、データベースとして今回作成したもの(test)を選択します。

テーブルはまだないですが、クエリ(例:show databases;)を実行すると結果が出力されることが確認できます。

  
  
  
  

コメント