【メモ】Laravel Docker環境構築 マイグレーション時のエラーとmysql8.0でのエラー

マイグレーション時のエラー

```
root@**********:/work# php artisan migrate

   Illuminate\Database\QueryException  : SQLSTATE[HY000] [1045] Access denied for user 'ogge'@'172.26.0.3' (using password: YES) (SQL: select * from information_schema.tables where table_schema = laravel_local and table_name = migrations and table_type = 'BASE TABLE')

  at /work/vendor/laravel/framework/src/Illuminate/Database/Connection.php:669
    665|         // If an exception occurs when attempting to run a query, we'll format the error
    666|         // message to include the bindings with SQL, which will make this exception a
    667|         // lot more helpful to the developer instead of just the database's errors.
    668|         catch (Exception $e) {
  > 669|             throw new QueryException(
    670|                 $query, $this->prepareBindings($bindings), $e
    671|             );
    672|         }
    673|

  Exception trace:

  1   PDOException::("SQLSTATE[HY000] [1045] Access denied for user 'ogge'@'172.26.0.3' (using password: YES)")
      /work/vendor/laravel/framework/src/Illuminate/Database/Connectors/Connector.php:70

  2   PDO::__construct("mysql:host=db;port=3306;dbname=laravel_local", "ogge", "secret", [])
      /work/vendor/laravel/framework/src/Illuminate/Database/Connectors/Connector.php:70

  Please use the argument -v to see more details.

```
.env

```
DB_CONNECTION=mysql
DB_HOST=db
DB_PORT=3306
DB_DATABASE=laravel_local
DB_USERNAME=*****
DB_PASSWORD=*****

```
.env.example

```
DB_CONNECTION=mysql
DB_HOST=db
DB_PORT=3306
DB_DATABASE=laravel_local
DB_USERNAME=*****
DB_PASSWORD=*****

```



```database.php
        'mysql' => [
            'driver' => 'mysql',
            'url' => env('DATABASE_URL'),
            'host' => env('DB_HOST', 'db'),
            'port' => env('DB_PORT', '3306'),
            'database' => env('DB_DATABASE', 'laravel_local'), 追記
            'username' => env('DB_USERNAME', ''),
            'password' => env('DB_PASSWORD', ''),
            'unix_socket' => env('DB_SOCKET', ''),

```


以上3つを修正してマイグレーション

```

root@:*********:/work# php artisan migrate

Migration table created successfully.
Migrating: 2014_10_12_000000_create_users_table
Migrated:  2014_10_12_000000_create_users_table (0.14 seconds)
Migrating: 2014_10_12_100000_create_password_resets_table
Migrated:  2014_10_12_100000_create_password_resets_table (0.06 seconds)
Migrating: 2019_08_19_000000_create_failed_jobs_table
Migrated:  2019_08_19_000000_create_failed_jobs_table (0.03 seconds)

```
成功だね！！！


その後コンテナに入ってmysqlを立ち上げ？

```
C:\Users\****\*****\****>docker-compose exec db bash
root@*******:/# mysql -u ***** -p
Enter password:

Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 15
Server version: 8.0.23 MySQL Community Server - GPL

Copyright (c) 2000, 2021, Oracle and/or its affiliates.

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> select user, host from mysql.user;

ERROR 1142 (42000): SELECT command denied to user '****'@'localhost' for table 'user'
```

rootで入り直して

```
root@*******:/# mysql -u root -p
Enter password:

Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 16
Server version: 8.0.23 MySQL Community Server - GPL

Copyright (c) 2000, 2021, Oracle and/or its affiliates.

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.

```

どのユーザーがいるか確認。
hostに%が付いていると，User項目が使えなくなる為，さっきの場合だとコマンドが受け付けてくれなかった。
因みに%はワイルドカードと言うらしく，どんなホストでもサーバーに繋げれるらしい。
※間違ってたらごめんね。

【参照】https://rfs.jp/server/mysql/m02/04-11.html

```
mysql> select host,user from mysql.user;
+-----------+------------------+
| host      | user             |
+-----------+------------------+
| %         | *****            |
| %         | root             |
| localhost | mysql.infoschema |
| localhost | mysql.session    |
| localhost | mysql.sys        |
| localhost | root             |
+-----------+------------------+
6 rows in set (0.01 sec)
```

権限の確認を行って

```
mysql> SHOW GRANTS for '*****'@'%';

+-----------------------------------------------------------+
| Grants for *****@%                                        |
+-----------------------------------------------------------+
| GRANT USAGE ON *.* TO `*****`@`%`                         |
| GRANT ALL PRIVILEGES ON `laravel\_local`.* TO `*****`@`%` |
+-----------------------------------------------------------+
2 rows in set (0.01 sec)

```

終了！！