Help us understand the problem. What is going on with this article?

laradockでマイグレーション実行できるようにする

ファイル作成

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を設定

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)
erySanDriver
へなちょこえんじにあ。にせんじゅうろくねんうまれ。
Why not register and get more from Qiita?
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away
Comments
No comments
Sign up for free and join this conversation.
If you already have a Qiita account
Why do not you register as a user and use Qiita more conveniently?
You need to log in to use this function. Qiita can be used more conveniently after logging in.
You seem to be reading articles frequently this month. Qiita can be used more conveniently after logging in.
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away
ユーザーは見つかりませんでした