使用 `urlencode` 的 Symfony 4 Flex 数据库连接
Symfony 4 Flex Database connection using `urlencode`
我正在使用 Symfony 4 + Flex 开始一个新项目。此时我正在尝试将我的新应用程序连接到 MySQL 数据库。
我正在关注 Symfony Documentation,并且我已经将 doctrine
添加到我的依赖项中:
composer require doctrine
composer require maker --dev
然后我在 .env
中定义的环境变量 DATABASE_URL
中添加了数据库连接信息,如下所示:
###> doctrine/doctrine-bundle ###
DATABASE_URL=mysql://myUser:myPasswordWithSpecialChars@127.0.0.1:3306/myDbName
###< doctrine/doctrine-bundle ###
此时我遇到了一个问题:
DBALException
Malformed parameter "url".
我认为这是因为我的 MySQL 密码使用了一些特殊字符。文档正在谈论它:
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.
我真的不明白如何或在哪里使用 urlencode 函数,因为 .env
文件不是 PHP 文件(如 doctrine.yaml
)。
是否有人已经使用 urlencode
函数对包含特殊字符的 MySQL 密码进行编码?
谢谢。
编辑
这是我的 doctrine.yaml
文件:
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
我没有编辑数据库 url 参数,因为它已在 .env
中定义。我说得对吗?
尝试改变这个:
url: '%env(resolve:DATABASE_URL)%'
对此:
url: '%env(DATABASE_URL)%'
我正在使用 Symfony 4 + Flex 开始一个新项目。此时我正在尝试将我的新应用程序连接到 MySQL 数据库。
我正在关注 Symfony Documentation,并且我已经将 doctrine
添加到我的依赖项中:
composer require doctrine
composer require maker --dev
然后我在 .env
中定义的环境变量 DATABASE_URL
中添加了数据库连接信息,如下所示:
###> doctrine/doctrine-bundle ###
DATABASE_URL=mysql://myUser:myPasswordWithSpecialChars@127.0.0.1:3306/myDbName
###< doctrine/doctrine-bundle ###
此时我遇到了一个问题:
DBALException
Malformed parameter "url".
我认为这是因为我的 MySQL 密码使用了一些特殊字符。文档正在谈论它:
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.
我真的不明白如何或在哪里使用 urlencode 函数,因为 .env
文件不是 PHP 文件(如 doctrine.yaml
)。
是否有人已经使用 urlencode
函数对包含特殊字符的 MySQL 密码进行编码?
谢谢。
编辑
这是我的 doctrine.yaml
文件:
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
我没有编辑数据库 url 参数,因为它已在 .env
中定义。我说得对吗?
尝试改变这个:
url: '%env(resolve:DATABASE_URL)%'
对此:
url: '%env(DATABASE_URL)%'