LoginSignup
0
0

More than 3 years have passed since last update.

Dockerでlaravel環境を構築した際にMysqlに接続拒否された話

Last updated at Posted at 2021-04-25

Dockerは起動済み
appコンテナ内でマイグレーションを行おうとすると下記のエラーが出る。

Illuminate\Database\QueryExceptionSQLSTATE[HY000] [1045] 
Access denied for user 'test'@'172.19.0.2' (using password: YES) 
(SQL: select count(*) as aggregate from `articles`)

結論

docker-compose down --volumes
docker-compose build
docker-compose up -d

このコマンドで

Access denied for user 'test'@'172.19.0.2' (using password: YES) 

部分のエラーは解決

後半のエラー部分は

.env (laravelの)
DB_PORT=3306
docker-compose.yml
    ports:
      - '4306:3306'

上記のようにすることで解決。
元々は.envファイルの中のDB_PORTが4306になっていたので3306に修正。
docker-compose.ymlをいじったらコンテナの起動をし直すことを忘れずに。

考えられる原因

前半部分
docker-compose down --volumes

このコマンドはコンテナ・イメージ・ボリューム・ネットワーク・未定義コンテナを全てを一括消去するコマンドなので何か履歴が残っていたことが原因?

後半部分

DBに接続する際のport番号の認識が誤っていたことが原因。
.envのport番号とdocker-compose.ymlの右側のport番号がコンテナのport番号に対応(同じにする)
docker-compose.ymlの左側のport番号はホストのport番号(Mk-2やSequel Proに接続する際に入力するport番号はこっち)

0
0
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
0