Featured image of post 解決 MySQL 8 Docker 無法登入錯誤:Authentication plugin 'caching_sha2_password' cannot be loaded

解決 MySQL 8 Docker 無法登入錯誤:Authentication plugin 'caching_sha2_password' cannot be loaded

解決 MySQL 8 Docker 無法登入錯誤:Authentication plugin 'caching_sha2_password' cannot be loaded

原因

在使用 docker-compose 安裝 MySQL 8 (mysql/mysql-server:8) 後,要連線登入時,會出現下列訊息

Authentication plugin ‘caching_sha2_password’ cannot be loaded

version: '3.9'

networks:
    local-proxy-network:
        driver: bridge


services:
    mysql:
        container_name: local_mysql
        image: 'mysql/mysql-server:8'
        ports:
          - '3306:3306'
        environment:
          MYSQL_ROOT_PASSWORD: "password"
          MYSQL_ROOT_HOST: "%"
          MYSQL_DATABASE: "kj"
          MYSQL_USER: "kj"
          MYSQL_PASSWORD: "kj"
          MYSQL_ALLOW_EMPTY_PASSWORD: 1
        volumes:
          - './storage/docker/mysql/:/var/lib/mysql'
        networks:
          - local-proxy-network
        restart: always

這個是 MySQL 升級至 8 後有修改密碼驗證方式導致的問題

版本 驗證方式
MySQL 8 caching_sha2_password
MySQL 5.7 mysql_native_password

解決方式 1:修改連線方式

使用 Terminal 連線到 MySQL 後將連線方式修改為 mysql_native_password

ALTER USER 'root'@'localhost' IDENTIFIED WITH mysql_native_password BY 'your-password';

解決方式 2:修改 my.cnf 設定檔案

編輯 MySQL 8 的 my.cnf 檔案

vim my.cnf

設定預設密碼驗證方式為 mysql_native_password

[mysqld]
default_authentication_plugin=mysql_native_password

Reference

comments powered by Disqus
All rights reserved,未經允許不得隨意轉載
Built with Hugo
Theme Stack designed by Jimmy