使用Docker部署laravel项目时获取mysql错误

时间:2017-01-09 10:56:29

标签: mysql laravel docker

我想用远程服务器之类的相同的环境来测试我的laravel项目。所以我尝试使用Docker(以前从未知道)。我安装了docker,并按照项目中的说明使用docker进行部署。 设置

将.env.example文件复制到.env,docker-compose.yml.example到docker-compose.yml

修改.env配置文件(可选)。如果修改.env文件中的mysql,mongo,redis配置,请记住修改docker-compose.yml文件中的配置。 安装或运行Docker

docker-compose up -d

//停止

docker-compose stop

chmod缓存文件夹

chmod -R 777 storage

chmod -R 777 bootstrap/cache

部署

sh deploy.sh

运行sh doploy.sh后出现此错误:

   [Illuminate\Database\QueryException]                                                              
  SQLSTATE[HY000] [2002] No such file or directory (SQL: select * from information_schema.tables where table_schema = awesome_teacher and table_name = migrations)
 [Doctrine\DBAL\Driver\PDOException]               
  SQLSTATE[HY000] [2002] No such file or directory  

  [PDOException]                                    
  SQLSTATE[HY000] [2002] No such file or directory  

  [Illuminate\Database\QueryException]                                                              
  SQLSTATE[HY000] [2002] No such file or directory (SQL: select * from information_schema.tables w  
  here table_schema = homestead_test and table_name = migrations)                                                               
  [Doctrine\DBAL\Driver\PDOException]               
  SQLSTATE[HY000] [2002] No such file or directory
[PDOException]                                    
    SQLSTATE[HY000] [2002] No such file or directory  

文件“docker-compose.yml”:

version: '2'

### Change the `teacher` with your own teacher name ###
services:
    application:
        container_name: teacher_application
        image: debian
        volumes:
            - ./:/var/www/laravel
    workspace:
        container_name: teacher_workspace
        restart: always
        image: my_company/laravel-workspace
        volumes_from:
            - application
        tty: true
    php-fpm:
        container_name: teacher_php-fpm
        restart: always
        image: my_company/laravel-php-fpm
        volumes_from:
            - application
        expose:
            - "9000"
        links:
            - workspace
    nginx:
        container_name: teacher_nginx
        restart: always
        image: my_company/laravel-nginx
        volumes_from:
            - data
            - logs
            - application
        ports:
            - "8000:80"
        links:
            - php-fpm
    data:
        container_name: teacher_data
        image: debian
        volumes:
            - .docker/mysql:/var/lib/mysql
            - .docker/data:/data
    data_test:
        container_name: teacher_data_test
        image: debian
        volumes:
            - .docker/mysql_test:/var/lib/mysql
            - .docker/data_test:/data
    logs:
        container_name: teacher_logs
        image: debian
        volumes:
            - .docker/logs/nginx:/var/log/nginx
            - .docker/logs/mongodb:/var/log/mongodb
    mysql:
        container_name: teacher_mysql
        restart: always
        image: mysql
        volumes_from:
            - data
            - logs
        expose:
            - "3306"
        environment:
            MYSQL_DATABASE: awesome_teacher
            MYSQL_USER: root
            MYSQL_PASSWORD: blablabla
            MYSQL_ROOT_PASSWORD: blablabla
    mysql_test:
        container_name: teacher_mysql_test
        restart: always
        image: mysql
        volumes_from:
            - data_test
        expose:
            - "3306"
        environment:
            MYSQL_DATABASE: homestead_test
            MYSQL_USER: homestead_test
            MYSQL_PASSWORD: secret
            MYSQL_ROOT_PASSWORD: root

档案“.env”:

APP_ENV=local
APP_KEY=base64:hl4qkfCdHiJrCG4sGfYfMU8faq3WywMHkH+mr/FiDu9PM=
APP_DEBUG=true
APP_LOG_LEVEL=debug
APP_URL=awesome-teacher.local
APP_DOMAIN='awesome-teacher.local'


DB_CONNECTION=mysql
DB_HOST=localhost
DB_PORT=3306
DB_DATABASE=awesome_teacher
DB_USERNAME=root
DB_PASSWORD=blablabla

DB_TEST_CONNECTION=mysql_test
DB_TEST_HOST=localhost
DB_TEST_PORT=3308
DB_TEST_DATABASE=homestead_test
DB_TEST_USERNAME=homestead_test
DB_TEST_PASSWORD=secret

BROADCAST_DRIVER=log
CACHE_DRIVER=file
SESSION_DRIVER=file
QUEUE_DRIVER=sync

PUSHER_APP_ID=
PUSHER_KEY=
PUSHER_SECRET=

1 个答案:

答案 0 :(得分:2)

对于简单的laravel应用程序,您有相当复杂的设置。问题是您的laravel应用程序尝试连接到自己容器中的MySQL服务器实例。

DB_HOST=localhost更改为DB_HOST=mysql以改为连接到mysql容器。