api-platform 尝试连接数据库时出错

Getting error with api-platform while trying to connect database

我在 mac 上安装了 mysql8,创建了一个空数据库以继续 api-平台教程,但我无法启动连接。当我使用

bin/console doctrine:database:create

只是为了检查数据库是否已经创建。但是我得到这个错误。

In AbstractMySQLDriver.php line 106:

  An exception occurred in driver: SQLSTATE[HY000] [2054] The server requested authentication method unknown to the client  


In PDOConnection.php line 31:

  SQLSTATE[HY000] [2054] The server requested authentication method unknown to the client  


In PDOConnection.php line 27:

  SQLSTATE[HY000] [2054] The server requested authentication method unknown to the client  


In PDOConnection.php line 27:

  PDO::__construct(): The server requested authentication method unknown to the client [caching_sha2_password]  

我刚刚编辑了我的 env 文件:

# In all environments, the following files are loaded if they exist,
# the latter taking precedence over the former:
#
#  * .env                contains default values for the environment variables needed by the app
#  * .env.local          uncommitted file with local overrides
#  * .env.$APP_ENV       committed environment-specific defaults
#  * .env.$APP_ENV.local uncommitted environment-specific overrides
#
# Real environment variables win over .env files.
#
# DO NOT DEFINE PRODUCTION SECRETS IN THIS FILE NOR IN ANY OTHER COMMITTED FILES.
#
# Run "composer dump-env prod" to compile .env files for production use (requires symfony/flex >=1.2).
# https://symfony.com/doc/current/best_practices.html#use-environment-variables-for-infrastructure-configuration

###> symfony/framework-bundle ###
APP_ENV=dev
APP_SECRET=bcd5845fdd72a47f771c43b27daf2fb2
#TRUSTED_PROXIES=127.0.0.0/8,10.0.0.0/8,172.16.0.0/12,192.168.0.0/16
#TRUSTED_HOSTS='^localhost|example\.com$'
###< symfony/framework-bundle ###

###> symfony/mailer ###
# MAILER_DSN=smtp://localhost
###< symfony/mailer ###

###> doctrine/doctrine-bundle ###
# Format described at https://www.doctrine-project.org/projects/doctrine-dbal/en/latest/reference/configuration.html#connecting-using-a-url
# For an SQLite database, use: "sqlite:///%kernel.project_dir%/var/data.db"
# For a PostgreSQL database, use: "postgresql://db_user:db_password@127.0.0.1:5432/db_name?serverVersion=11&charset=utf8"
# IMPORTANT: You MUST configure your server version, either here or in config/packages/doctrine.yaml
DATABASE_URL=mysql://root:a123.s.d@localhost:3306/book_api
###< doctrine/doctrine-bundle ###

###> nelmio/cors-bundle ###
CORS_ALLOW_ORIGIN=^https?://(localhost|127\.0\.0\.1)(:[0-9]+)?$
###< nelmio/cors-bundle ###

我在研究mysql的连接问题。我看到 mysql8 有不同的密码格式,这就是 mysql 无法与 api 建立连接的原因。但是,我找不到任何绝对的解决方案。所以,我通过将 mysql8 降级到 mysql5.7

解决了这个问题

这是我做的

docker-compose.yml

 db:
    image: mysql:8
    command: mysqld --default-authentication-plugin=mysql_native_password
    restart: always
    environment:
        - MYSQL_DATABASE=api
        - MYSQL_USER=api-platform
        # You should definitely change the password in production
        - MYSQL_ROOT_PASSWORD=root
        - MYSQL_PASSWORD=!ChangeMe!
    volumes:
      - ./api/docker/db/data:/var/lib/mysql
    ports:
      - target: 3306
        published: 3306
        protocol: tcp

\api.env

DATABASE_URL=mysql://api-platform:!ChangeMe!@db:3306/api?server_version=8

\api\Dockerfile

pdo_pgsql更改为pdo_mysql

添加这个应该可以解决问题:

命令:--default-authentication-plugin=mysql_native_password