ファイル作成
ery@DESKTOP:/project/myapp$ php artisan make:migration db_table_name
.envの設定
- project\myapp.env
DB_CONNECTION=mysql
DB_HOST=mysql
DB_PORT=3306
DB_DATABASE=default
DB_USERNAME=default
DB_PASSWORD=secret
MySQLをdockerで実行できるようにpluginを設定
- 参考 : https://qiita.com/ntakuyan/items/42cbe7f45e85e16fe639
-
caching_sha2_password
->mysql_native_password
root@efd99ab69d3e:/# mysql -uroot -p
mysql> SELECT user, host, plugin FROM mysql.user;
+------------------+-----------+-----------------------+
| user | host | plugin |
+------------------+-----------+-----------------------+
| default | % | caching_sha2_password |
| root | % | caching_sha2_password |
| mysql.infoschema | localhost | caching_sha2_password |
| mysql.session | localhost | caching_sha2_password |
| mysql.sys | localhost | caching_sha2_password |
| root | localhost | caching_sha2_password |
+------------------+-----------+-----------------------+
6 rows in set (0.00 sec)
mysql> ALTER USER 'root'@'localhost' IDENTIFIED WITH mysql_native_password BY 'root';
mysql> ALTER USER 'root'@'%' IDENTIFIED WITH mysql_native_password BY 'root';
mysql> ALTER USER 'default'@'%' IDENTIFIED WITH mysql_native_password BY 'secret';
mysql> SELECT user, host, plugin FROM mysql.user;
+------------------+-----------+-----------------------+
| user | host | plugin |
+------------------+-----------+-----------------------+
| default | % | mysql_native_password |
| root | % | mysql_native_password |
| mysql.infoschema | localhost | caching_sha2_password |
| mysql.session | localhost | caching_sha2_password |
| mysql.sys | localhost | caching_sha2_password |
| root | localhost | mysql_native_password |
+------------------+-----------+-----------------------+
6 rows in set (0.00 sec)
laradockのmysqlの設定
- laradock/mysql/my.cnf に下記の通り追加
[mysqld]
sql-mode="STRICT_TRANS_TABLES,NO_ZERO_IN_DATE,ERROR_FOR_DIVISION_BY_ZERO,NO_ENGINE_SUBSTITUTION"
character-set-server=utf8
default_authentication_plugin= mysql_native_password # <- 追加
アプリが動いているコンテナに入って実行。
- ※laravelのドキュメントより、マイグレーションはコンテナ内に入って実行するとのこと。
ery@DESKTOP:/project/laradock$ docker exec -it laradock_workspace_1 bash
root@ery0606060:/var/www# php artisan migrate -v
実行後mysqlのコンテナ入ってテーブルを確認
ery@DESKTOP:/project/laradock$ docker exec -it laradock_mysql_1 bash
root@ery0606060:/# mysql -udefault -psecret default
mysql> show tables;
+-------------------------------------+
| Tables_in_default |
+-------------------------------------+
| db_table_name |
| failed_jobs | <- laravelデフォルトで必要なテーブル
| migrations |
+-------------------------------------+
3 rows in set (0.00 sec)
mysql> show create table db_table_name;
+-------------------------------------+---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| Table | Create Table |
+-------------------------------------+---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| db_table_name | CREATE TABLE `db_table_name` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`hogehoge` int(11) NOT NULL,
`created_at` datetime NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci |
+-------------------------------------+---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
1 row in set (0.00 sec)