LoginSignup
20
16

More than 5 years have passed since last update.

Laradock+MySQL マイグレーションするまで

Last updated at Posted at 2018-08-21

はじめに

事前準備

コンテナを起動する

laradock$ docker-compose up -d php-fpm nginx mysql workspace
laradock_mysql_1 is up-to-date
laradock_workspace_1 is up-to-date
laradock_docker-in-docker_1 is up-to-date
laradock_php-fpm_1 is up-to-date
laradock_nginx_1 is up-to-date

コンテナ立ち上がってましたね:airplane:

コンテナ(ワークスペース)に入る

laradoc$ docker exec -it laradock_workspace_1 /bin/bash

別タブでMySQLコンテナに入る

laradoc$ docker exec -it laradock_mysql_1 /bin/bash

MySQLにログイン

root@b17755e83e64:/var/www# mysql -u root -p
Enter password: 
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 9
Server version: 8.0.12 MySQL Community Server - GPL

Copyright (c) 2000, 2018, 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.

バージョン8系なんだと今気づく:eyes:

test_dbを作成

mysql> create database test_db;
Query OK, 1 row affected (0.09 sec)

テーブルを作成する

コンテナ(ワークスペース)にて、以下を実施する。

migrationファイルを作成する

root@1058e6da0b91:/var/www# php artisan make:migration create_test_db_table

migrationを実行する

root@b17755e83e64:/var/www# php artisan migrate

こんなエラーが出ました:smiling_imp:

SQLSTATE[HY000] [2054] The server requested authentication method unknown to the client
PDO::__construct(): The server requested authentication method unknown to the client [caching_sha2_password] 

なるほど:thinking:
ユーザーの認証方法が変わったのか:point_up:

Before:認証方式をチェックする

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)

defaultとrootの認証方式を変更する

mysql> ALTER USER 'default'@'%' IDENTIFIED WITH mysql_native_password BY 'secret';`
mysql> ALTER USER 'root'@'%' IDENTIFIED WITH mysql_native_password BY 'secret';

After:認証方式をチェックする

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 | caching_sha2_password |
+------------------+-----------+-----------------------+
6 rows in set (0.00 sec)

変わってますね:thinking:
うん。一旦解決。改めて、、、

migrationファイルを作成する

root@1058e6da0b91:/var/www# php artisan make:migration create_test_db_table
Created Migration: 2018_08_21_103343_create_sample_table

なんか命名規則あったような・・・:thinking:

laravelプロジェクト配下の.envを修正

以下のように変更。
root@b17755e83e64:/var/www# vim .env

DB_CONNECTION=mysql:relaxed:
DB_HOST=mysql
DB_PORT=3306
DB_DATABASE=test_db
DB_USERNAME=root
DB_PASSWORD=secret

migrationを実行する

root@1058e6da0b91:/var/www# 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_08_21_152517_create_test_db_table
Migrated:  2018_08_21_152517_create_test_db_table

お。良さげ!!

テーブルができたかを確認する

mysql> use test_db;
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> 
mysql> 
mysql> 
mysql> show tables;
+-------------------+
| Tables_in_test_db |
+-------------------+
| migrations        |
| password_resets   |
| test_db           |
| users             |
+-------------------+
4 rows in set (0.00 sec)

おしまい

ちなみに現時点で、Sequel proはMySQL8系に対応してなかったです:cop_tone5:

20
16
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
20
16