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

猜你喜欢

转载自blog.csdn.net/networken/article/details/84562997