快轉到主要內容

解決Laravel連接MySQL8驗證失敗錯誤

·406 字·1 分鐘
Y Cheung
作者
Y Cheung
Blogger, Programer & Traveler.

💡 2023-06-14 update

MySQL升級至8.4後這篇文章的方法就失效咯,請移步《解決 MySQL 升級 8.4 Docker 容器啟動問題》查看最新解決方案。

之前一直用MariaDB沒感覺,這次因為需求變動改用最新的MySQL 8 後, Laravel 就無法連接上資料庫了,囧。

對資料庫進行任何操作都出現了 The server requested authentication method unknown to the client 錯誤:

1Illuminate\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)

查了一下,原來是 MySQL 8.0 後預設儲存密碼的方式由原來的 mysql_native_password 改為 caching_sha2_password 了,而 Y Cheung 目前使用的PHP版本7.3 是不支持caching_sha2_password plugin的,據說php7.4開始支持caching_sha2_password了

解決方案:

  • Plan A: 升級PHP版本到7.4
  • Plan B: 降級MySQL版本
  • Plan C: 啟動MySQL Docker的時候使用命令 --default-authentication-plugin=mysql_native_password

完整 Laravel docker-compose.yml文件如下:

 1version: '3'
 2services:
 3  mysql:
 4    image: mysql:latest
 5    restart: always
 6    volumes:
 7      - '~/Documents/Datas/Laravel/mysql:/var/lib/mysql'
 8    ports:
 9      - '127.0.0.1:33306:3306'
10    environment:
11      MYSQL_ALLOW_EMPTY_PASSWORD: 'yes'
12      MYSQL_DATABASE: 'laravel'
13    command: --default-authentication-plugin=mysql_native_password
14  laravel:
15    image: ycheung/laravel-dev:latest
16    restart: always
17    ports:
18      - '9086:80'
19    volumes:
20      - '~/Documents/Laravel:/var/www/html'
21    depends_on:
22      - mysql

相關文章

Laravel 7 配置郵件服務 Mailgun

·595 字·2 分鐘
2016年的時候,Mailgun 免費註冊帳號還能添加自定義域名(當時Y Cheung 曾寫過一篇blog:Centos7 安裝 Postfix 並配置 mailgun 服務),想不到現在新註冊帳號已經是需要先綁定銀行卡才能使用自定義域名,而註冊分配給你的測試域名Sandbox subdomain壓根就不能用,你會遇到類似以下的報錯信息:

MYSQL外鍵導致無法清空表

·132 字·1 分鐘
最近設計的數據庫表含有外鍵,結果在開發時truncate table的時候報錯了, 1SQL错误(1701):Cannot truncate a table referenced in a foreign key constraint (`rollcall2`.`oncall`, CONSTRAINT `FK__calls` FOREIGN KEY (`calls_id`) REFERENCES `rollcall2`.`calls` (`id`)) 解決辦法如下: 1SET FOREIGN_KEY_CHECKS = 0; 2TRUNCATE table; 3SET FOREIGN_KEY_CHECKS = 1; 當然也可以通過移除外鍵之類的辦法,但是感覺還是用上面幾行命令來解決比較方便。

Aliyun MySQL Aborting

·331 字·1 分鐘
放了wordpress的主機最近經常掛掉,提示無法連結數據庫,檢查log信息發現是因為內存不足,把InnoDB的buffer pool size改小後似乎好了一些,可今天還是出錯了,log信息如下: