Laravel无法使用Docker Compose连接到数据库

时间:2018-12-21 14:34:15

标签: laravel docker docker-compose

我正在开发一个Laravel应用程序,尝试将Docker作为开发环境。我正在使用Docker Compose来编排Docker。我现在在连接数据库时遇到麻烦。到目前为止,这是我所做的。

我已经在项目根文件夹中创建了docker-compose.yml

version: '3'
services:
    apache:
        container_name: easy_eat_apache
        image: webdevops/apache:ubuntu-16.04
        environment:
            WEB_DOCUMENT_ROOT: /var/www/public
            WEB_ALIAS_DOMAIN: easy-eat.localhost
            WEB_PHP_SOCKET: php-fpm:9000
        volumes: # Only shared dirs to apache (to be served)
        - ./public:/var/www/public:cached
        - ./storage:/var/www/storage:cached
        networks:
        - easy-eat-network
        ports:
        - "80:80"
        - "443:443"
    php-fpm:
        container_name: easy_eat_php
        image: jguyomard/laravel-php:7.2
        volumes:
        - ./:/var/www/
        - ./ci:/var/www/ci:cached
        - ./vendor:/var/www/vendor:delegated
        - ./storage:/var/www/storage:delegated
        - ./node_modules:/var/www/node_modules:cached
        - ~/.ssh:/root/.ssh:cached
        - ~/.composer/cache:/root/.composer/cache:delegated
        networks:
        - easy-eat-network
    db:
        container_name: easy_eat_db
        image: mariadb:10.2
        environment:
            MYSQL_ROOT_PASSWORD: root
            MYSQL_DATABASE: easy_eat
            MYSQL_USER: easy_eat
            MYSQL_PASSWORD: secret
        volumes:
        - db_data:/var/lib/mysql
        networks:
        - easy-eat-network
        ports:
        - "33060:3306"
networks:
    easy-eat-network:
        driver: "bridge"
volumes:
    db_data:
        driver: "local"

然后我运行以下命令来启动环境。

docker-compose up --build -d

我这样修改了env文件中的数据库凭据。

DB_CONNECTION=mysql
DB_HOST=db
DB_PORT=3306
DB_DATABASE=easy_eat
DB_USERNAME=easy_eat
DB_PASSWORD=secret

为了确保我的laravel应用程序可以连接到数据库,请按以下方式运行迁移命令。

docker-compose exec php-fpm php artisan migrate:fresh --seed

我遇到以下错误。

Illuminate\Database\QueryException  : SQLSTATE[HY000] [1045] Access denied for user 'easy_eat'@'172.25.0.4' (using password: YES) (SQL: SHOW FULL TABLES WHERE table_type = 'BASE TABLE')

  at /var/www/vendor/laravel/framework/src/Illuminate/Database/Connection.php:664
    660|         // If an exception occurs when attempting to run a query, we'll format the error
    661|         // message to include the bindings with SQL, which will make this exception a
    662|         // lot more helpful to the developer instead of just the database's errors.
    663|         catch (Exception $e) {
  > 664|             throw new QueryException(
    665|                 $query, $this->prepareBindings($bindings), $e
    666|             );
    667|         }
    668| 

  Exception trace:

  1   PDOException::("SQLSTATE[HY000] [1045] Access denied for user 'easy_eat'@'172.25.0.4' (using password: YES)")
      /var/www/vendor/laravel/framework/src/Illuminate/Database/Connectors/Connector.php:70

  2   PDO::__construct("mysql:host=db;port=3306;dbname=easy_eat", "easy_eat", "secret", [])
      /var/www/vendor/laravel/framework/src/Illuminate/Database/Connectors/Connector.php:70

设置有什么问题,我该如何解决?

1 个答案:

答案 0 :(得分:0)

我发现了问题。以前已经有一个使用Docker的项目。在docker-compose文件中,db容器正在使用db_data卷。因此,它与现有项目引起冲突。因此,我不得不将配置更改为如下。

enter image description here