简体   繁体   中英

Can't connect to Cosmos DB Emulator with Node.js MongoDB Driver

I'm trying to connect a dummy node.js app to a local Cosmos DB Emulator instance through MongoDB drivers. The dummy app tries to connect, and if successful tries to open a DB. I've been able to do connect with same setup in a more complex app in the past, but that failed to connect now and hence I created the new dummy app, which is failing as well to connect.

I ran the Emulator with "C:\Program Files\Azure Cosmos DB Emulator\Microsoft.Azure.Cosmos.Emulator.exe" /EnableMongoDbEndpoint . Emulator admin panel shows at https://localhost:8081/_explorer/index.html. Studio 3T for MongoDB successfully connects to localhost@localhost:10255.

I exported Emulator certificate as per instructions , copied it to project root and got its path in code with const certFilePath = path.resolve(__dirname, '../', 'documentdbemulatorcert.cer');.

I've tried connecting both with Mongo Connection String (copied from admin panel):

const connectOptions = {
  useNewUrlParser: true,
  useUnifiedTopology: true,
  ssl: true,
  sslValidate: true,
  sslCA: certFilePath,
};
const client = new MongoClient(MONGODB_CONNECTION_STRING, connectOptions);

as well as with Mongo URI, username and unencoded password (again, copied from admin panel):

const connectOptions = {
  auth: {
    username: MONGODB_AUTH_USERNAME,
    password: MONGODB_AUTH_PASSWORD_NOT_ENCODED,
  },
  useNewUrlParser: true,
  useUnifiedTopology: true,
  ssl: true,
  sslValidate: true,
  sslCA: certFilePath,
};
const client = new MongoClient(MONGODB_URI, connectOptions);

In any case, trying to connect with this:

try {
  await client.connect();
  console.log('Connection to server successful');
} catch (error) {
  console.error('Connection to server failed:', error);
}

fails with this error:

Connection to server failed: MongoServerSelectionError: connect ECONNREFUSED ::1:10255
    at Timeout._onTimeout (...my-project-root...\node_modules\mongodb\lib\sdam\topology.js:292:38)
    at listOnTimeout (node:internal/timers:564:17)
    at process.processTimers (node:internal/timers:507:7) {
  reason: TopologyDescription {
    type: 'Unknown',
    servers: Map(1) { 'localhost:10255' => [ServerDescription] },
    stale: false,
    compatible: true,
    heartbeatFrequencyMS: 10000,
    localThresholdMS: 15,
    setName: null,
    maxElectionId: null,
    maxSetVersion: null,
    commonWireVersion: 0,
    logicalSessionTimeoutMinutes: null
  },
  code: undefined,
  [Symbol(errorLabels)]: Set(0) {}
}

I've also tried excluding useNewUrlParser , useUnifiedTopology , ssl , sslValidate , sslCA , with no effect. I've also tried setting NODE_TLS_REJECT_UNAUTHORIZED=0 before running the app: there's an additional warning about less safety, but then same error. I've tried copying connection string from Studio 3T as well, but apart from some trailing parameters they're the same as Emulator admin panel, and it fails in same way.

I've download the sample Node.js app linked in admin portal Quickstart page, which uses CosmosClient instead. It fails with basically same error:

Completed with error {"message":"request to https://localhost:8081/ failed, reason: connect ECONNREFUSED::1:8081","type":"system","errno":"ECONNREFUSED","code":"ECONNREFUSED","headers":{"x-ms-throttle-retry-count":0,"x-ms-throttle-retry-wait-time-ms":0}}

(although Emulator was still running with /EnableMongoDbEndpoint option)

Some context info: Windows 11 x64, node 19.1.0, mongodb 4.12.1, Cosmos Emulator 2.14.9.0.

I've been through related questions here on SO and elsewhere, with no help gained. Any hint? Thanks all

As always happens (thanks SO), after posting question, the solution comes up.

Running Emulator with option /EnableMongoDbEndpoint=3.6 or /EnableMongoDbEndpoint=4.0 solves the issue. Dummy app connects successfully and opens DB as well.

Command line arguments reference is here .

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