symfony4连接

30byixjq  于 2021-06-17  发布在  Mysql
关注(0)|答案(1)|浏览(407)

我使用docker for mysql和adminer接口,adminer和mysql之间的连接正常,但symfony连接被拒绝。symfony不在容器里。
我能为symfony4和mysql container works之间的连接做些什么?
这是我的docker-compose.yml:

version: '3'
services:

  mysql:
    image: mysql:5.7
    volumes:
    - ./data:/var/lib/mysql
    environment:
      MYSQL_ROOT_PASSWORD: "rootpwd"
      MYSQL_USER: 'testuser'
      MYSQL_PASSWORD: 'testpassword'

  adminer:
    image: adminer
    restart: always
    ports:
    - 8080:8080
    links:
      - mysql:db

volumes:
  mysql:

symfony 4上的my.env:


# This file defines all environment variables that the application needs.

# DO NOT DEFINE PRODUCTION SECRETS IN THIS FILE.

# Use ".env.local" for local overrides during development.

# Use real environment variables when deploying to production.

# https://symfony.com/doc/current/best_practices/configuration.html#infrastructure-related-configuration

### > symfony/framework-bundle ###

APP_ENV=dev
APP_SECRET=825aa371242c34bdcc0c693ac4241bcf

# TRUSTED_PROXIES=127.0.0.1,127.0.0.2

# TRUSTED_HOSTS='^localhost|example\.com$'

### < symfony/framework-bundle ###

### > doctrine/doctrine-bundle ###

# Format described at http://docs.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"

# Configure your db driver and server_version in config/packages/doctrine.yaml

DATABASE_URL=mysql://testuser:testpassword@127.0.0.1:3306/dbname

### < doctrine/doctrine-bundle ###

还有我的教义

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: utf8mb4
        default_table_options:
            charset: utf8mb4
            collate: utf8mb4_unicode_ci

        url: '%env(resolve:DATABASE_URL)%'
        unix_socket: /tmp/mysql.sock
    orm:
        auto_generate_proxy_classes: true
        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

更新
好吧,谢谢你的帮助它工作了,symfony或者更特别的是没有找到容器,就像你说的我暴露了mysql端口,但是我必须做一个其他的改变:这行代码对我有用:

DATABASE_URL=mysql://root:root@127.0.0.1:3306/dbname

不是这句话:

DATABASE_URL=mysql://testuser:testpassword@127.0.0.1:3306/dbname

事实上,似乎我的docker compose up工作不太好,而且是用旧版本编写的,docker没有在docker-compose.yml上“看到”我的更改,所以密码和用户保持“root”。我要试试这个:https://forums.docker.com/t/how-to-delete-cache/5753/2 还有这个:https://github.com/docker/compose/issues/1487
有时,数据库需要删除=>警告:docker特定的变量只有在用空的数据目录启动容器时才会起作用;任何预先存在的数据库在容器启动时都将保持不变。https://hub.docker.com/_/postgres/

3bygqnnd

3bygqnnd1#

您需要像eg一样公开数据库端口。

version: '3'
services:

  mysql:
    image: mysql:5.7
    volumes:
    - ./data:/var/lib/mysql
    environment:
      MYSQL_ROOT_PASSWORD: "rootpwd"
      MYSQL_USER: 'testuser'
      MYSQL_PASSWORD: 'testpassword'
    ports:
        - 3306:3306
  adminer:
    image: adminer
    restart: always
    ports:
    - 8080:8080
    links:
      - mysql:db

volumes:
  mysql:

然后在 .env 文件

DATABASE_URL=mysql://testuser:testpassword@127.0.0.1:3306/dbname

确保ip地址与docker主机的ip匹配。

相关问题