0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

LaravelのDB接続にかなり苦労した話。

Posted at

#MAMP環境でLravelのDB接続を試みる。

php artisan migrate したところ、

  Illuminate\Database\QueryException 

  SQLSTATE[HY000] [2054] The server requested authentication method unknown to the client (SQL: select * from information_schema.tables where table_schema = laravel and table_name = migrations and table_type = 'BASE TABLE')

  at vendor/laravel/framework/src/Illuminate/Database/Connection.php:703
    699▕         // If an exception occurs when attempting to run a query, we'll format the error
    700▕         // message to include the bindings with SQL, which will make this exception a
    701▕         // lot more helpful to the developer instead of just the database's errors.
    702▕         catch (Exception $e) {
  ➜ 703▕             throw new QueryException(
    704▕                 $query, $this->prepareBindings($bindings), $e
    705▕             );
    706▕         }
    707▕     }

      +33 vendor frames 
  34  artisan:37
      Illuminate\Foundation\Console\Kernel::handle(Object(Symfony\Component\Console\Input\ArgvInput), Object(Symfony\Component\Console\Output\ConsoleOutput))

上のようなエラーが、、

こちらのサイトや、
https://motomotosukiyaki.com/mysql-from-php-server-requested-authentication-method-unknown-to-the-client

こちらのサイトを参考に
https://takakisan.com/laravel-mysql-8-migration-error-fix/
調べてみると、

MySQL 8では、ユーザー作成時に設定される認証方式がデフォルトでcaching_sha2_passwordというものになっています。
この認証方式はMySQL 8から追加された、比較的新しいもので、Laravel5.6のDB接続機能ではこの方式に対応していません。

とのこと。

記載通り、mysqlにrootログインし
mysql> SELECT user, host, plugin FROM mysql.user;

で認証方式を調べたところ、
+---------------+-----------+-----------------------+
| user | host | plugin |
+---------------+-----------+-----------------------+
| root | localhost | mysql_native_password |
| mysql.session | localhost | mysql_native_password |
| mysql.sys | localhost | mysql_native_password |
| mamp | localhost | mysql_native_password |
| root | 127.0.0.1 | mysql_native_password |
| root | ::1 | mysql_native_password |
| test | % | mysql_native_password |
| test22 | localhost | mysql_native_password |
+---------------+-----------+-----------------------+

全て「mysql_native_password」になっており、認証に問題はないとわかる。

他にも色んな要因を探ったが解決せず途方に暮れているところ、

問題解決につながる記事を発見!!

$  ps ax | grep mysql

95785   ??  S      0:00.03 /bin/sh /usr/local/Cellar/mysql/8.0.27/bin/mysqld_safe --datadir=/usr/local/var/mysql --pid-file=/usr/local/var/mysql/sadomaru.pid
95897   ??  S      0:42.36 /usr/local/Cellar/mysql/8.0.27/bin/mysqld --basedir=/usr/local/Cellar/mysql/8.0.27 --datadir=/usr/local/var/mysql --plugin-dir=/usr/local/Cellar/mysql/8.0.27/lib/plugin --log-error=sadomaru.err --pid-file=/usr/local/var/mysql/sadomaru.pid
99857   ??  S      0:00.03 /bin/sh /Applications/MAMP/Library/bin/mysqld_safe --port=3306 --socket=/Applications/MAMP/tmp/mysql/mysql.sock --pid-file=/Applications/MAMP/tmp/mysql/mysql.pid --log-error=/Applications/MAMP/logs/mysql_error.log
99970   ??  S      0:04.72 /Applications/MAMP/Library/bin/mysqld --basedir=/Applications/MAMP/Library --datadir=/Applications/MAMP/db/mysql57 --plugin-dir=/Applications/MAMP/Library/lib/plugin --log-error=/Applications/MAMP/logs/mysql_error.log --pid-file=/Applications/MAMP/tmp/mysql/mysql.pid --socket=/Applications/MAMP/tmp/mysql/mysql.sock --port=3306
 4799 s001  S+     0:00.05 ./mysql -u test22 -p
 6576 s004  S+     0:00.03 grep mysql

どうやら以前MySQLがうまく動かなかったときに起動させていたmysqld_safeが邪魔をしていたらしい、、

 $ sudo kill -TERM 95785
 $ sudo kill -TERM 95897

こちらでとりあえず強制停止。

$ ps ax | grep mysql   
 4799 s001  S+     0:00.05 ./mysql -u test22 -p
 6853 s004  S+     0:00.00 grep mysql

必要なものだけ開かれていることを確認した上で、もう一度チャレンジ!

$  php artisan migrate  

Migration table created successfully.
Migrating: 2014_10_12_000000_create_users_table
Migrated:  2014_10_12_000000_create_users_table (87.81ms)
Migrating: 2014_10_12_100000_create_password_resets_table
Migrated:  2014_10_12_100000_create_password_resets_table (52.38ms)
Migrating: 2019_08_19_000000_create_failed_jobs_table
Migrated:  2019_08_19_000000_create_failed_jobs_table (59.08ms)
Migrating: 2019_12_14_000001_create_personal_access_tokens_table
Migrated:  2019_12_14_000001_create_personal_access_tokens_table (46.30ms)

いけた!!ありがとうございました!

0
0
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
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?