【Laravel】mysqlに接続できず、migrateできなかった


Laravelをインストールして、migrateしようとしたら、

[ec2-user@ip-172-31-39-229 mosaku]$ php artisan migrate

   Illuminate\Database\QueryException  : SQLSTATE[HY000] [2002] No such file or directory (SQL: select * from information_schema.tables where table_schema = mosaku and table_name = migrations and table_type = 'BASE TABLE')

  at /var/www/html/mosaku/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   Doctrine\DBAL\Driver\PDO\Exception::("SQLSTATE[HY000] [2002] No such file or directory")
      /var/www/html/mosaku/vendor/doctrine/dbal/lib/Doctrine/DBAL/Driver/PDO/Exception.php:18

  2   Doctrine\DBAL\Driver\PDO\Exception::new()
      /var/www/html/mosaku/vendor/doctrine/dbal/lib/Doctrine/DBAL/Driver/PDOConnection.php:43

  Please use the argument -v to see more details.

これに数時間取られてしまったので、備忘録として記載。

.envにDB_SOCKETを追記

結局これだけでmigrateできるようになったのですが、mysql.sockの場所をずっと勘違いしてました。
私の環境は、ec2のAWS Linux2なのですが、正解はここ。

/var/lib/mysql/mysql.sock

/var/run/mysqld/mysqlx.sock がありましたが、違いました。
これを.envのDB_SOCKETに追記してあげれば動きました。

.env
...
DB_SOCKET=/var/run/mysqld/mysqlx.sock
...