Laravel Cron php_network_getaddresses:getaddrinfo失败

时间:2017-09-06 15:58:17

标签: mysql laravel docker laravel-5 cron

您好!

所以我在为Laravel调度设置cron作业时遇到了这个烦人的问题。 要理解这个问题,这是项目架构:

  • 使用docker设置项目(我们有一个容器用于php,和 mysql和nginx)
  • Web应用程序基于Laravel 5.3构建

我们希望自动化某些任务,因此我们创建了3个命令,我们将它们收集到日程表中以便每天运行它们,这是kernel.php的样子:

    protected $commands = [
        'App\Console\Commands\IncompleteAccount',
        'App\Console\Commands\TirageSoon',
        'App\Console\Commands\WinzExpiration',
    ];

    protected function schedule(Schedule $schedule)
    {
        $schedule->command('incompleteaccount:run')->everyFiveMinutes()->sendOutputTo(storage_path('logs/cron.log'));

        $schedule->command('tiragesoon:run')->everyFiveMinutes()->sendOutputTo(storage_path('logs/cron.log'));

        $schedule->command('winzExpire:run')->everyFiveMinutes()->sendOutputTo(storage_path('logs/cron.log'));
    }

我们的crontab看起来像:

* * * * * php /var/www/artisan schedule:run >> /var/www/storage/logs/cron.log 2>&1

在日志文件中,我们得到了这个:

  [Illuminate\Database\QueryException]                                                                                                                                    
  SQLSTATE[HY000] [2002] php_network_getaddresses: getaddrinfo failed: Name or service not known (SQL: select * from `booking` where `created_at` > 2017-08-27 16:08:50)  



  [Doctrine\DBAL\Driver\PDOException]                                                             
  SQLSTATE[HY000] [2002] php_network_getaddresses: getaddrinfo failed: Name or service not known  



  [PDOException]                                                                                  
  SQLSTATE[HY000] [2002] php_network_getaddresses: getaddrinfo failed: Name or service not known  



  [PDOException]                                                                               
  PDO::__construct(): php_network_getaddresses: getaddrinfo failed: Name or service not known  


No scheduled commands are ready to run.
No scheduled commands are ready to run.
No scheduled commands are ready to run.
No scheduled commands are ready to run.

最后.env文件看起来像:

APP_ENV=prod
APP_KEY=base64:gPDQqknmG9YSOMueb/Ii0xXWQvrzOPsVFVRHVIzXXD0=
APP_DEBUG=true
APP_LOG_LEVEL=debug
APP_URL=http://www.ourSite.com
DB_CONNECTION=mysql
DB_HOST=mysql
DB_PORT=3306
DB_DATABASE=ourDB
DB_USERNAME=homestead
DB_PASSWORD=secret

BROADCAST_DRIVER=log
CACHE_DRIVER=file
SESSION_DRIVER=file

另外,我可以从容器内部ping到mysql,这里是docker compose文件:

version: '2'

services:

    volumes_source:
      image: tianon/true
      volumes:
          - ${SRCDIR}:/var/www
          - ${DATADIR}/data/uploads:/var/www/public/img/uploads




    nginx:
        image: nginx
        volumes_from:
          - volumes_source
        volumes:
            - ${SRCDIR}/dockers/etc/nginx/sites-enabled:/etc/nginx/sites-enabled
            - ${SRCDIR}/dockers/etc/nginx/nginx.conf:/etc/nginx/nginx.conf
        ports:
           - "${HTTP_PORT}:80"
        links:
           - php:phpfpm

    wait:
        image: aanand/wait
        links:
           - mysql:mysql
### Workspace Utilities Container ###########################

    php:
        build: dockers/winsu
        volumes_from:
          - volumes_source
        command: php-fpm7 -F
        working_dir: /var/www
        environment:
            CONFIG_ENV: ${CONFIG_ENV}
        entrypoint: /var/www/dockers/winsu/run.sh
        volumes:
           - ${SRCDIR}/dockers/winsu/crontab:/crontab.file
        links:
           - mysql:mysql
           - wait:wait

### MySQL Container #########################################
    mysql:
        image: mysql
        volumes_from:
            - volumes_data
        environment:
            MYSQL_DATABASE: winsu
            MYSQL_USER: homestead
            MYSQL_PASSWORD: root
            MYSQL_ROOT_PASSWORD: root

### Databases Data Container ################################

    volumes_data:
        image: tianon/true
        volumes:
            - ${DATADIR}/data/mysql:/var/lib/mysql
            - ${DATADIR}/data/storage:/var/www/storage

到目前为止已经面临这个问题3天了!如果你能提供帮助,我将非常感激! 我已经看到了类似的问题,但没有任何效果!提前致谢:)

2 个答案:

答案 0 :(得分:1)

您可以尝试使用工匠来证明您的代码。

在php artisan commant中你可以放一个-v | vv | vvv, - verbose增加消息的详细程度:1表示正常输出,2表示更详细的输出,3表示调试

例如,你可以这样做:

<script src="https://cdnjs.cloudflare.com/ajax/libs/react/15.1.0/react.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/react/15.1.0/react-dom.min.js"></script>
<div id="root"></div>

如果代码有错误,使用此代码,该命令会返回错误信息以及该行及其相关信息。

答案 1 :(得分:1)

您的问题似乎与未获取的环境变量有关。当您在cron中运行命令时,两件事情是不同的

  • 当前工作目录
  • 环境变量

所以将命令更改为

* * * * * cd /var/www && /var/www/artisan schedule:run ...

并在命令

之前使用X = Y格式定义任何其他所需变量
* * * * * cd /var/www && X=Y Z=A /var/www/artisan schedule:run ...