0

We have an external mongodb instance and we need to query data from it and bring it to Drupal 8 site.

On shell this is how we connect (mongodb is running in a different container than drupal) mongo --host docker_mongo --ssl --sslCAFile=/etc/ssl/mongodb-cert.crt

Adding the following at settings.php does not work and status report shows "No database aliases found in settings. Did you actually configure your settings ?"

$configureMongoDb = function (array $settings): array {
  $settings['mongodb'] = [
    'clients' => [
      // Client alias => connection constructor parameters.
      'mymongo' => [
        'uri' => 'mongodb://docker_mongo:27017',
        'uriOptions' => ['ssl=true&sslCAFile=/etc/ssl/mongodb-cert.crt'],
        'driverOptions' => [],
      ],
    ],
    'databases' => [
      // Database alias => [ client_alias, database_name ].
      'tutorial2' => ['mymongo', 'tutorial2'],
    ],
  ];

  return $settings;
};

Could anyone suggest how to connect to a mongodb instance?

Cross-posting this question at drupal-mongodb issue as well

1 Answer 1

0

You don't need to set a function in settings.php to define the connection. The documentation lists this as an example:

https://fgm.github.io/mongodb/install/#settings-configuration

// In sites/default/settings.local.php.
$settings['mongodb'] = [
  'clients' => [
    // Client alias => connection constructor parameters.
    'default' => [
      'uri' => 'mongodb://localhost:27017',
      'uriOptions' => [],
      'driverOptions' => [],
    ],
  ],
  'databases' => [
    // Database alias => [ client_alias, database_name ]
    'default' => ['default', 'drupal'],
    'keyvalue' => ['default', 'keyvalue'],
    'logger' => ['default', 'logger'],
  ],
];
2
  • Irrespective of where we put this, the status report shows that "No database aliases found in settings. Did you actually configure your settings ?" So something is off
    – amit
    Commented Dec 5, 2018 at 3:43
  • Thanks, I had another issue as pecl install mongodb was not installing ssl which caused failure to connect with mongoldb
    – amit
    Commented Dec 14, 2018 at 18:51

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.