简体   繁体   中英

Cannot connect with mongodb through doctrine

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)"} []

问题确实在我的docker compose文件中,我没有将两个docker都与网络连接,感谢@BrucieAlpha的帮助

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM