0
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

[Windowsユーザ向け] DockerでMySQLを扱った際のエラー集

Posted at

普段はWebフロントの開発や組み込み系の開発をすることが多いのですが、サーバサイドの開発についても学び始めたところ、Windowsを原因としたDockerのエラーで躓くことが多かったため、それらについてまとめます。また、今後もエラーが起きるたびに追記していこうと思います。

また、Windowsでの具体的なDocker環境構築については、すでに多くの方が記事等を出していますので、そちらをご覧ください。私は主に以下の記事を参考に環境構築させていただきました。

環境

  • Windows 11 Home 23H2
  • Docker Desktop 4.28.0
  • WSL Ubuntu

lohalhostと127.0.0.1は別もの

-h 127.0.0.1と指定しても接続できない。

C:\Users>mysql -u docker -h 127.0.0.1 -p
Enter password: ****************
ERROR 1045 (28000): Access denied for user 'docker'@'localhost' (using password: YES)

-h localhostなら接続できる。

C:\Users>mysql -u docker -h localhost -p 
Enter password: ****************
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 4
Server version: 5.7.44 MySQL Community Server (GPL)

Copyright (c) 2000, 2023, 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>

WindowsでDockerをインストールする際には、Windows OS環境ではなく、WSLのLinux OS環境にDockerをインストールすることになるため、そこにWindows OS環境から接続しようとしているのが原因か?

ダブルクォーテーションが必要

以下docker-compose.yamlファイルを一部抜粋。
ダブルクォーテーションがないとエラーが起きる (友人のMac環境だと起こらないらしい) 。

services:
  mysql:
    # コンテナ内で使用する環境変数
    environment:
      MYSQL_ROOT_USER: root # MySQLのルートユーザ名
      MYSQL_ROOT_PASSWORD: pass # MySQLのルートユーザのパスワード
      MYSQL_DATABASE: sampledb # MySQLに用意されている初期データベースの名前
      MYSQL_USER: docker # MySQLのユーザ名
      MYSQL_PASSWORD: docker # MySQLのユーザパスワード
      TZ: Asia/Tokyo

Windowsユーザはダブルクォーテーションを付ける必要がある。本来は付けるのが正確らしい...

services:
  mysql:
    # コンテナ内で使用する環境変数
    environment:
      MYSQL_ROOT_USER: "root" # MySQLのルートユーザ名
      MYSQL_ROOT_PASSWORD: "pass" # MySQLのルートユーザのパスワード
      MYSQL_DATABASE: "sampledb" # MySQLに用意されている初期データベースの名前
      MYSQL_USER: "docker" # MySQLのユーザ名
      MYSQL_PASSWORD: "docker" # MySQLのユーザパスワード
      TZ: "Asia/Tokyo"
0
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?