Qiita Teams that are logged in
You are not logged in to any team

Log in to Qiita Team
Community
OrganizationAdvent CalendarQiitadon (β)
Service
Qiita JobsQiita ZineQiita Blog
0
Help us understand the problem. What is going on with this article?
@erySanDriver

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

More than 1 year has passed since last update.

ファイル作成

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)
0
Help us understand the problem. What is going on with this article?
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
erySanDriver
へなちょこえんじにあ。にせんじゅうろくねんうまれ。

Comments

No comments
Sign up for free and join this conversation.
Sign Up
If you already have a Qiita account Login
0
Help us understand the problem. What is going on with this article?