Search code examples
phpmongodbdockerzend-framework2

Zend Framework 2 + docker: MongoDB ConnectionTimeoutException


I am using Zend Framework 2 and MongoDB v3.2.11.

When I start up my ZF app, get the following error:

Fatal error: Uncaught MongoDB\Driver\Exception\ConnectionTimeoutException: No suitable servers found (serverSelectionTryOnce set) in /var/www/project/vendor/mongodb/mongodb/src/Collection.php on line 360

Here's my docker-compose:

mongodb:
    image: mongo:3.2.11
    restart: always
    container_name: mongodb
    environment:
      - MONGO_DATA_DIR=/data/project
      - MONGO_INITDB_ROOT_USERNAME=root
      - MONGO_INITDB_ROOT_PASSWORD=mongopassword
    volumes:
      - mongodb-data:/data/project
    ports:
      - "27017:27017"
    command: mongod --smallfiles

Here's my ZF config:

'caches' => [
        'Project\Web\Cache\Sessions' => [
            'template' => 'mongodb',
            'adapter' => [
                'name' => 'Project\Common\Cache\Storage\Adapter\MongoDb',
                'options' => [
                    'server' => getenv('ZF_MONGO_HOST'),
                    'connection_options' => [
                        'connect' => true,
                        'connectTimeoutMS' => 5000,
                        'fsync' => true,
                        'readPreference' => 'primaryPreferred',
                        'replicaSet' => 'set-1',
                    ],
                    'database' => getenv('ZF_MONGO_DATABASE_NAME'),
                    'collection' => 'sessions',
                    'ttl' => 3600,
                ],
            ],
        ],
    ],

Here's my docker-compose.env:

## MONGO DB ##
ZF_MONGO_HOST=mongodb://my_user:mymongopassword@mongodb/my_db
ZF_MONGO_DATABASE_NAME=my_db

What I tried: * When I connect to the mongodb, it works * I tested the user credentials: works * When I change the ZF_MONGO_HOST I get the same error but in the brackets I see that he cannot connect to the - wrong - url. So the setup seems ok. * Googling for ~3h: All the errors are connected to offline mongodb servers...

Any ideas?

Update:

I tried out the bitnami image, following setup:

  mongodb:
    image: bitnami/mongodb:3.6.8
    restart: always
    container_name: mongodb
    environment:
      - MONGODB_USERNAME=my_user
      - MONGODB_PASSWORD=mymongopassword
      - MONGODB_DATABASE=my_db
    volumes:
      - mongodb-data:/data/db-bitnami
    ports:
      - "27017:27017"

Same error.


Solution

  • After docker docker-compose up, connect to MongoDB database and type rs.status() and it should have member field which indicates it's the slave. if it's not set, you need to defind it in mongodb:

    rs.initiate( {
       _id : "set-1",
       members: [
          { _id: 0, host: "mongodb0.example.net:27017" },
          { _id: 1, host: "mongodb1.example.net:27017" },
          { _id: 2, host: "mongodb2.example.net:27017" }
       ]
    })
    

    after this step your mongodb has been replicated and ready to use. for automating second part of this (config mongodb) you can write a bash script which work as Entrypoint in docker-compose file or use some replicated docker images like bitnami.