doubijiao2094 2018-05-15 09:20
浏览 86
已采纳

使用`urlencode`连接Symfony 4 Flex数据库

I am starting a new project using Symfony 4 + Flex. At this time I'm trying to connect my new app to a MySQL database.

I'm following the Symfony Documentation, and I have already add doctrine to my dependencies :

composer require doctrine
composer require maker --dev

Then I've added the database connection information in the environment variable DATABASE_URL defined inside .env like this :

###> doctrine/doctrine-bundle ###
DATABASE_URL=mysql://myUser:myPasswordWithSpecialChars@127.0.0.1:3306/myDbName
###< doctrine/doctrine-bundle ###

At this point I am facing an issue :

DBALException

Malformed parameter "url".

I'm thinking this is caused by the fact that my MySQL password is using some special characters. The documentation is talking about it :

If the username, password or database name contain any character considered special in a URI (such as !, @, $, #), you must encode them. See RFC 3986 for the full list of reserved characters or use the urlencode function to encode them.

I don't really understand how or where to use the urlencode function because the .env file is not a PHP one (as doctrine.yaml).

Is someone have already use the urlencode function to encode a MySQL password containing special chars ?

Thanks.

EDIT

This is my doctrine.yaml file :

parameters:
    # Adds a fallback DATABASE_URL if the env var is not set.
    # This allows you to run cache:warmup even if your
    # environment variables are not available yet.
    # You should not need to change this value.
    env(DATABASE_URL): ''

doctrine:
    dbal:
        # configure these for your database server
        driver: 'pdo_mysql'
        server_version: '5.7'
        charset: utf8
        default_table_options:
            charset: utf8
            collate: utf8_unicode_ci

        url: '%env(resolve:DATABASE_URL)%'
    orm:
        auto_generate_proxy_classes: '%kernel.debug%'
        naming_strategy: doctrine.orm.naming_strategy.underscore
        auto_mapping: true
        mappings:
            App:
                is_bundle: false
                type: annotation
                dir: '%kernel.project_dir%/src/Entity'
                prefix: 'App\Entity'
                alias: App

I've not edited the database url parameter because it is already defined in .env. Am I right ?

  • 写回答

1条回答 默认 最新

  • douxieti6851 2018-05-15 09:38
    关注

    try to change this:

    url: '%env(resolve:DATABASE_URL)%'
    

    to this:

    url: '%env(DATABASE_URL)%'
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 #MATLAB仿真#车辆换道路径规划
  • ¥15 java 操作 elasticsearch 8.1 实现 索引的重建
  • ¥15 数据可视化Python
  • ¥15 要给毕业设计添加扫码登录的功能!!有偿
  • ¥15 kafka 分区副本增加会导致消息丢失或者不可用吗?
  • ¥15 微信公众号自制会员卡没有收款渠道啊
  • ¥100 Jenkins自动化部署—悬赏100元
  • ¥15 关于#python#的问题:求帮写python代码
  • ¥20 MATLAB画图图形出现上下震荡的线条
  • ¥15 关于#windows#的问题:怎么用WIN 11系统的电脑 克隆WIN NT3.51-4.0系统的硬盘