我是 Symfony 5.4/Docker 的新手,我正在尝试了解如何连接到 docker mysql 数据库。
我正在启动一个项目,其中我将使用在带有 Docker (mysql) 的远程计算机上运行的数据库。所以,我已经配置了这个数据库,并且我已经正确导入了其中的所有数据。所以我开始创建存储库和实体以推送到我的数据库中。
问题是当我在每个配置中设置 Mysql 时,symfony 继续看到 PostgreSql DB。但我有两件奇怪的事情。
他继续将 Postgre 视为数据库。
怎么可能? .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.
# https://symfony.com/doc/current/configuration/secrets.html
#
# 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=84cac6cb4e921f9349604ecf6784d220
###< symfony/framework-bundle ###
###> symfony/webapp-pack ###
MESSENGER_TRANSPORT_DSN=doctrine://default?auto_setup=0
###< symfony/webapp-pack ###
###> doctrine/doctrine-bundle ###
# Format described at https://www.doctrine-project.org/projects/doctrine-dbal/en/latest/reference/configuration.html#connecting-using-a-url
# IMPORTANT: You MUST configure your server version, either here or in config/packages/doctrine.yaml
#
# DATABASE_URL="sqlite:///%kernel.project_dir%/var/data.db"
DATABASE_URL="mysql://root:[email protected]:3306/d2b48de2d4398d46a2faa17526b8e3d2a0f1d9110dc06cc2c53f54127dfeec37?serverVersion=5.7&charset=utf8mb4"
# DATABASE_URL="postgresql://symfony:[email protected]:5432/app?serverVersion=13&charset=utf8"
# database credentials
DB_USER=root
DB_PASS=example # this is the secret password
###< doctrine/doctrine-bundle ###
###> symfony/messenger ###
# Choose one of the transports below
# MESSENGER_TRANSPORT_DSN=doctrine://default
# MESSENGER_TRANSPORT_DSN=amqp://guest:guest@localhost:5672/%2f/messages
# MESSENGER_TRANSPORT_DSN=redis://localhost:6379/messages
###< symfony/messenger ###
###> symfony/mailer ###
# MAILER_DSN=null://null
###< symfony/mailer ###
###> blackfireio/blackfire-symfony-meta ###
# Get your SERVER credentials at https://blackfire.io/my/settings/credentials
# BLACKFIRE_SERVER_ID=xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx
# BLACKFIRE_SERVER_TOKEN=yyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyy
###< blackfireio/blackfire-symfony-meta ###
doctrine.yaml
doctrine:
dbal:
url: '%env(resolve:DATABASE_URL)%'
# IMPORTANT: You MUST configure your server version,
# either here or in the DATABASE_URL env var (see .env file)
server_version: '8.0'
orm:
auto_generate_proxy_classes: true
naming_strategy: doctrine.orm.naming_strategy.underscore_number_aware
auto_mapping: true
mappings:
App:
is_bundle: false
dir: '%kernel.project_dir%/src/Entity'
prefix: 'App\Entity'
alias: App
when@test:
doctrine:
dbal:
# "TEST_TOKEN" is typically set by ParaTest
dbname_suffix: '_test%env(default::TEST_TOKEN)%'
when@prod:
doctrine:
orm:
auto_generate_proxy_classes: false
query_cache_driver:
type: pool
pool: doctrine.system_cache_pool
result_cache_driver:
type: pool
pool: doctrine.result_cache_pool
framework:
cache:
pools:
doctrine.result_cache_pool:
adapter: cache.app
doctrine.system_cache_pool:
adapter: cache.system
docker-compose.yaml
version: '3'
services:
###> doctrine/doctrine-bundle ###
database:
image: mysql:8
command: mysqld
restart: always
environment:
MYSQL_ROOT_PASSWORD: password
adminer:
image: adminer
restart: always
ports:
- 8080:8080
###< doctrine/doctrine-bundle ###
您在这个项目及其周围使用过 postgres 吗?因为我也有类似的问题,但我的问题是因为我的机器上有一个处于活动状态的 Postgres 容器,所以我必须在 Symfony 使用我的 MySQL 连接之前停止它。根据记录,我不知道它为什么会这样做。