duanfan5012 2016-02-19 20:28
浏览 65

如何在铁工作者任务中使用PHP的MongoDB驱动程序?

I'm using iron.io and its worker tasks for a background process. My code is fairly simple, just like:

<?php
require 'vendor/autoload.php';

$uri = "my_connection_string";
$client = new MongoClient($uri);
:
:

composer.json is like:

{
    "require": {
        "iron-io/iron_worker": "2.0.4",
        "wp-cli/php-cli-tools": "~0.10.3"
    }
}

Then I first install all dependencies,

docker run --rm -it -v "$PWD":/worker -w /worker iron/php:dev composer install

compress my code,

zip -r worker.zip .

upload it,

iron --env dev worker upload --name task-name -zip ./worker.zip iron/php:dev php ./worker.php

then this error output.

PHP Fatal error:  Class 'MongoClient' not found in /mnt/task/worker.php on line xxx

I also tried MongoDB\Driver\Manager, MongoDB\Driver\Client, the same error occurs. When I tried running php -m, it shows:

[PHP Modules]
Core
curl
date
ereg
fileinfo
filter
hash
json
libxml
mbstring
mysqlnd
pcre
readline
Reflection
session
SimpleXML
SPL
standard
tokenizer
xmlwriter

[Zend Modules]

It looks even MongoDB extension is not installed, while they say it's installed. http://dev.iron.io/worker/languages/php/

Am I missing something? How can I use MongoDB driver in PHP with iron worker?

  • 写回答

1条回答 默认 最新

  • duandaotuo5542 2016-07-15 22:05
    关注

    For those who may be in the same situation as I was, this happens because Iron.io now integrates Docker and we have to specify an image we use when running our code, but iron/php image doesn't include any additional middlewares they used to support.

    So we have to write our own DockerFile to install MongoDB or MySQL or whatever necessary middleware & software we need.

    As for MongoDB, this is an example of a Dockerfile.

    iron/php:5.6.14
    RUN apk update
    RUN apk upgrade
    RUN apk add alpine-sdk
    RUN apk add zlib-dev
    RUN apk add curl
    RUN apk add bash
    RUN apk add perl
    RUN apk add re2c
    RUN apk add pcre-dev
    RUN apk add openssl-dev
    RUN apk add php-dev autoconf
    RUN git clone https://github.com/mongodb/mongo-php-driver.git
    WORKDIR ./mongo-php-driver
    RUN which php
    RUN git submodule sync && git submodule update --init
    RUN phpize
    RUN ./configure
    RUN make all -j 5
    RUN make install
    RUN echo 'extension=mongo.so' >> /etc/php.ini
    

    Please note that this isn't optimized at all in terms of an image size.

    I think it's very helpful if Iron.io'd provide such images.

    评论

报告相同问题?

悬赏问题

  • ¥15 目详情-五一模拟赛详情页
  • ¥15 有了解d3和topogram.js库的吗?有偿请教
  • ¥100 任意维数的K均值聚类
  • ¥15 stamps做sbas-insar,时序沉降图怎么画
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看
  • ¥15 关于#Java#的问题,如何解决?
  • ¥15 加热介质是液体,换热器壳侧导热系数和总的导热系数怎么算
  • ¥100 嵌入式系统基于PIC16F882和热敏电阻的数字温度计
  • ¥15 cmd cl 0x000007b
  • ¥20 BAPI_PR_CHANGE how to add account assignment information for service line