1. 程式人生 > >mysql8.0身份認證方式變更

mysql8.0身份認證方式變更

在使用docker搭建wordpress時,連線資料庫異常,wordpress容器無法正常執行,檢視日誌提示如下錯誤:

[root@swarm-worker2 ~]# docker logs -f  wordpress.1.wyn0h32nbjgl07htkpqxwcqdv
WordPress not found in /var/www/html - copying now...
Complete! WordPress has been successfully copied to /var/www/html

Warning: mysqli::__construct(): The server requested authentication method unknown to the client [
caching_sha2_password] in Standard input code on line 22 Warning: mysqli::__construct(): (HY000/2054): The server requested authentication method unknown to the client in Standard input code on line 22 MySQL Connection Error: (2054) The server requested authentication method unknown to the client

當使用最新版本的mysql8.0版本時,預設身份驗證外掛已經改為caching_sha2_password,而不是以往的mysql_native_password。
解決方法1:


使用舊版本mysql,比如mysql5.7:

docker service create \
     --name mysql \
     --network mysql_private \
     --secret source=mysql_root_password,target=mysql_root_password \
     --secret source=mysql_password,target=mysql_password \
     -e MYSQL_ROOT_PASSWORD_FILE="/run/secrets/mysql_root_password" \
     -
e MYSQL_PASSWORD_FILE="/run/secrets/mysql_password" \ -e MYSQL_USER="wordpress" \ -e MYSQL_DATABASE="wordpress" \ # mysql:latest mysql:5.7

解決方法2:
或者以yml方式執行容器服務時加入以下命令,改為之前的預設認證方式:
command: ‘–default-authentication-plugin=mysql_native_password’

[root@swarm-manager ~]# cat wordpress.yml 
version: '3.1'

services:
    db:
        image: mysql:latest
        command: '--default-authentication-plugin=mysql_native_password'
        volumes:
            - db_data:/var/lib/mysql
        environment:
            MYSQL_ROOT_PASSWORD_FILE: /run/secrets/db_root_password
            MYSQL_DATABASE: wordpress
            MYSQL_USER: wordpress
            MYSQL_PASSWORD_FILE: /run/secrets/db_password
        secrets:
            - db_root_password
            - db_password
 ......