LoginSignup
42
40

More than 5 years have passed since last update.

LaradockのプロジェクトコンテナとMySQLコンテナを接続する方法

Last updated at Posted at 2018-03-22

以前書いた、Laradorkを使用したLaravelプロジェクトの開発環境のまとめの続きです。
今回はLaravelプロジェクトのコンテナとMySQLのコンテナを接続してみます。


2019年5月8日追記
タグの変更
「Laravel5.6」⇨「Laravel」


2019年5月1日追記

・MySQLコンテナへのアクセス時に下記のエラーメッセージが表示された場合
「No container found for mysql_1」

~/app/laradock $ docker-compose exec mysql bash
ERROR: No container found for mysql_1

⇨「~/.laradock/data」を削除するかリネームすることで解消出来ます。

~/app/laradock $ rm -r ~/.laradock/data

全てのコンテナを再起動かける事でmysqlコンテナに入れました。

・ワークスペースのコンテナのIPアドレス取得について補足
「IPAddress」でgrepをかけると、2つのIPアドレスが取得出来ます。
MySQLのユーザーを作成する際は2つのIPアドレス分のユーザーを作成します。

~/app/laradock $ docker inspect b14487f63923 | grep IPAddress
            "SecondaryIPAddresses": null,
            "IPAddress": "",
                    "IPAddress": "IPアドレス1",
                    "IPAddress": "IPアドレス2",

・MySQL8系の設定について
特に設定を弄らなければデフォルトではMySQL8系がインストールされますが、ユーザー作成の際にパスワードの生成方法が変更されており単純にユーザーを作成するだけではMySQLコンテナにアクセス出来ない様になります。
MySQLのコンテナにて「my.cnf」に下記の通り追記することで解消出来ます。

root@318151507c08:/# vim /etc/mysql/my.cnf
[mysqld]
default_authentication_plugin=mysql_native_password

*コンテナの再起動が必要です。

ユーザーごとのパスワードの生成方法は、下記のコマンドで確認出来ます。

mysql> select User, Plugin from mysql.user;
+------------------+-----------------------+
| User             | Plugin                |
+------------------+-----------------------+
| default          | caching_sha2_password |
| root             | caching_sha2_password |
| root             | mysql_native_password |
| root             | mysql_native_password |
| mysql.infoschema | caching_sha2_password |
| mysql.session    | caching_sha2_password |
| mysql.sys        | caching_sha2_password |
| root             | caching_sha2_password |
+------------------+-----------------------+
8 rows in set (0.00 sec)

また、以前は必要なかったと認識しているのですが、作成したユーザーにGRANT文で権限を付与しないとマイグレーションが出来ない様になっています。
権限を特に気にする必要が無い場合はユーザーを作成した後に下記の通りに権限を付与してください。

grant all privileges on *.* to 'ユーザー名'@'ワークスペースコンテナのIPアドレス';

動作環境について

OS:macOS Sierra
version: 10.12.6

Docker(Engine):17.12.0-ce
Compose:1.18.0

git: 2.12.2

参考資料

laradockでlaravelの開発環境構築
Laradock/Documentation

環境構築方法

任意のDBを使える様に下記の通りlaradockディレクトリ側の「.env」ファイルを編集します。
「MYSQL_DATABASE」の値を変更します。
今回は「test_db1」というデータベースにしたいとします。

~/app/laradock $ vim .env
MYSQL_DATABASE=default
⇨MYSQL_DATABASE=test_db1

次にコンテナを起動させます。

~/app/laradock $ docker-compose up -d nginx mysql redis beanstalkd
Starting laradock_applications_1 ... 
Starting laradock_redis_1 ... 
Starting laradock_applications_1 ... done
Starting laradock_workspace_1 ... done
Starting laradock_php-fpm_1 ... done
Starting laradock_beanstalkd_1 ... 
Starting laradock_nginx_1 ... done

ワークスペースのコンテナに入ります。

~/app/laradock $ docker exec -it laradock_workspace_1 /bin/bash
root@e43ece0f6a55:/var/www# 

別タブでMySQLのコンテナに入ります。

~/app/laradock $ docker-compose exec mysql bash
root@34b570825d7f:/# 

*リファレンス通り上記のコマンドでは「mysql」と指定するすればコンテナに入れます。
ワークスペースのコンテナの場合は「laradock_workspace_1」と指定する必要があります。

MySQLコマンドを使って動作の確認をしてみます。

root@34b570825d7f:/# mysql -u root -p
Enter password: 
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 8
Server version: 8.0.3-rc-log MySQL Community Server (GPL)

Copyright (c) 2000, 2017, Oracle and/or its affiliates. All rights reserved.

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> show databases;
+--------------------+
| Database           |
+--------------------+
| default            |
| information_schema |
| mysql              |
| performance_schema |
| sys                |
+--------------------+
5 rows in set (0.02 sec)

mysql> 

ワークスペースのコンテナからMySQLにアクセス出来る様に専用のユーザーを作ります。

一旦ホスト側に戻り、コンテナ情報を取得するコマンドを打ちます。
IPアドレスが分かります。

~/app/laradock $ docker inspect e43ece0f6a55

"IPAddress": "IPアドレス",

ユーザーを作ります。

mysql> create user 'root'@'IPアドレス' identified by 'root';
Query OK, 0 rows affected (0.01 sec)

データベースを作ります。

mysql> create database test_db1 charset=utf8;
Query OK, 1 row affected (0.01 sec)

mysql> show databases;
+--------------------+
| Database           |
+--------------------+
| default            |
| information_schema |
| mysql              |
| performance_schema |
| sys                |
| test_db1           |
+--------------------+
6 rows in set (0.00 sec)

mysql> 

データベースが出来ました。

ワークスペースのコンテナに移り、マイグレーションをしてみます。

root@e43ece0f6a55:/var/www/test# php artisan make:migration test_db1_table
Created Migration: 2018_03_21_134052_test_db1_table

マイグレーションファイルを作成出来ました。

次に「test」ディレクトリ下にある「.env」を編集します。
データベースに関する記述を下記の通りに変更します。

root@e43ece0f6a55:/var/www/test# vim .env
DB_CONNECTION=mysql
DB_HOST=mysql
DB_PORT=3306
DB_DATABASE=test_db1
DB_USERNAME=root
DB_PASSWORD=root

マイグレーションを実行します。

root@e43ece0f6a55:/var/www/test# php artisan migrate
Migration table created successfully.
Migrating: 2014_10_12_000000_create_users_table
Migrated:  2014_10_12_000000_create_users_table
Migrating: 2014_10_12_100000_create_password_resets_table
Migrated:  2014_10_12_100000_create_password_resets_table
Migrating: 2018_03_21_134052_test_db1_table
Migrated:  2018_03_21_134052_test_db1_table
root@e43ece0f6a55:/var/www/test# 

テーブルが出来ているかの確認を行います。

*「test_db1」のマイグレーションファイルは生成後に特に何も記述していない為、テーブルが出来ません。

mysql> use test_db1;
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -A

Database changed
mysql> show tables;
+--------------------+
| Tables_in_test_db1 |
+--------------------+
| migrations         |
| password_resets    |
| users              |
+--------------------+
3 rows in set (0.01 sec)

mysql> 

テーブルが出来ました。

とりあえず、今回はここまで。
LaravelのプロジェクトコンテナとMysqlのコンテナを接続することが出来ました。

42
40
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
42
40