1

I'm making a project to school with few of my friends. Right now im trying to setup our symfony project with already existing mongodb, but got problem with finding it.

I'm trying to follow official guide on how to setup that combo from www.symfony.com with bundle DoctrineMongoDBBundle.

My doctrine:

doctrine_mongodb:
  auto_generate_proxy_classes: '%kernel.debug%'
  auto_generate_hydrator_classes: '%kernel.debug%'
  connections:
    default:
      server: "%mongodb_server%"
      options:
        username: "%mongodb_username%"
        password: "%mongodb_password%"
        authSource: "%mongodb_db%"
  default_database: "%mongodb_auth_db%"
  document_managers:
    default:
      auto_mapping: true
      mappings:
        App:
          is_bundle: false
          type: annotation
          dir: '%kernel.project_dir%/src/Document'
          prefix: App\Document\
          alias: App

The values that are taken from services.yaml

parameters:
    locale: 'en'
    mongodb_server: "mongodb://mongodb:27017"
    mongodb_username: "admin"
    mongodb_password: "my_password"
    mongodb_auth_db: "admin"
    mongodb_db: "users"

We are working in docker environment, so my symfony project and database are on two different dockers (I need to connect through them, so localhost is not working).

As first function im working on controller for users, im trying to make registering user working, this is the controller that im currently using (still with mocked data):

/**
 * @Route("/api/security/register", name="register")
 * @Method("GET")
 * @return JsonResponse
 */
public function registerAction(): JsonResponse
{
    $user = new User();
    $user->setUsername('test');
    $user->setEmail('test@test.test');
    $user->setPlainPassword('testtest');

    $dm = $this->get('doctrine_mongodb')->getManager();
    $dm->persist($user);
    $dm->flush();
    return new JsonResponse(array('Status' => 'OK'));
}

Also in database, I want to connect to admin user, who have permission to write into "users" database (there I want to save my new user).

Error im getting trying to send request:

request.CRITICAL: Uncaught PHP Exception MongoConnectionException: "No suitable servers found (serverSelectionTryOnce set): [Failed to resolve 'mongodb']" at /var/www/html/vendor/alcaeus/mongo-php-adapter/lib/Alcaeus/MongoDbAdapter/ExceptionConverter.php line 79 {"exception":"[object] (MongoConnectionException(code: 13053): No suitable servers found (serverSelectionTryOnce set): [Failed to resolve 'mongodb'] at /var/www/html/vendor/alcaeus/mongo-php-adapter/lib/Alcaeus/MongoDbAdapter/ExceptionConverter.php:79, MongoDB\Driver\Exception\ConnectionTimeoutException(code: 13053): No suitable servers found (serverSelectionTryOnce set): [Failed to resolve 'mongodb'] at /var/www/html/vendor/mongodb/mongodb/src/Collection.php:867)"} []

Megami
  • 109
  • 2
  • 13
  • do you use docker-compose? If so please share your docker-compose.yaml... If not you have to make sure your containers are in the same network... https://stackoverflow.com/questions/27563460/let-two-containers-getting-linked-to-eachother – Brucie Alpha Aug 14 '19 at 07:32
  • @BrucieAlpha now that I look at them (two different docker composers) they don't share the same network, as im pretty new to using docker I didn't know it will block connection between them, thanks for advice, gonna try to add them to same network, and also i'll update ticket with docker-composer's – Megami Aug 14 '19 at 08:31

1 Answers1

1

The problem was indeed in my docker compose file, I didnt connect both dockers with network, thanks for help @BrucieAlpha

Megami
  • 109
  • 2
  • 13