10

When attempting to connect to the local emulator for Azure CosmosDB with the MongoDB connection string mongodb://localhost:C2y6yDjf5%2FR%2Bob0N8A7Cgv30VRDJIWEHLM%2B4QDU5DE2nQ9nDuVTqobD4b8mGGyPMbIZnqyMsEcaGQy67XIw%2FJw%3D%3D@localhost:10255/admin?ssl=true, I am receiving the following error:

{ MongoNetworkError: failed to connect to server [localhost:10255] on first connect [MongoNetworkError: connect ECONNREFUSED 127.0.0.1:10255]
    at Pool.<anonymous> (D:\github\myapp\node_modules\mongodb-core\lib\topologies\server.js:431:11)
    at Pool.emit (events.js:189:13)
    at connect (D:\github\myapp\node_modules\mongodb-core\lib\connection\pool.js:557:14)
    at makeConnection (D:\github\myapp\node_modules\mongodb-core\lib\connection\connect.js:39:11)
    at callback (D:\github\myapp\node_modules\mongodb-core\lib\connection\connect.js:261:5)
    at TLSSocket.err (D:\github\myapp\node_modules\mongodb-core\lib\connection\connect.js:286:7)
    at Object.onceWrapper (events.js:277:13)
    at TLSSocket.emit (events.js:189:13)
    at emitErrorNT (internal/streams/destroy.js:82:8)
    at emitErrorAndCloseNT (internal/streams/destroy.js:50:3)
    at process._tickCallback (internal/process/next_tick.js:63:19)
  name: 'MongoNetworkError',
  errorLabels: [ 'TransientTransactionError' ],
  [Symbol(mongoErrorContextSymbol)]: {} }

I have installed the latest version of the emulator (currently v2.4.5) from the Microsoft Download Center and followed the installation instructions according to the Microsoft docs

I have tried connecting using Mongoose connect with Node.js (which works perfectly with the Azure cloud configuration), and using the VS Code Azure CosmosDB extension. Both yield the same error.

What is the proper way to connect to the local emulator?

halshing
  • 612
  • 2
  • 6
  • 17
  • did you start the local mongo db? – Sajeetharan Aug 10 '19 at 03:19
  • yes, i ran `C:\Program Files\MongoDB\Server\3.6\bin\mongod.exe` from the command line successfully (it says "waiting for connections on port 27017"). but i'm still getting mongo network errors, even when updating the port in the connection string to 27017 and changing `localhost` to `mongodb`. – halshing Aug 10 '19 at 03:56
  • you don't need to set `localhost` to `mongodb`. Just specify `localhost:27017` in the connection string – Atish Aug 10 '19 at 07:17
  • thanks for the response. unfortunately using `mongodb://localhost:C2y6yDjf5%2FR%2Bob0N8A7Cgv30VRDJIWEHLM%2B4QDU5DE2nQ9nDuVTqobD4b8mGGyPMbIZnqyMsEcaGQy67XIw%2FJw%3D%3D@localhost:27017/admin?ssl=true` still doesn't work – halshing Aug 10 '19 at 19:26

1 Answers1

19

Turns out I needed to run the emulator from the command-line and enable the mongodb endpoint. No need to run MongoDB separately.

The following worked for me:

cd "C:\Program Files\Azure Cosmos DB Emulator"

then

.\CosmosDB.Emulator.exe /EnableMongoDbEndpoint

After running these commands, I was able to use the connection string provided by the Azure Emulator for MongoDB and successfully connect and read/write to the local database.

halshing
  • 612
  • 2
  • 6
  • 17
  • 1
    With the current version the parameter is "/EnableMongoDbEndpoint=3.6". The old parameter start the legacy version 3.2 of MongoDB. – Horcrux7 Mar 01 '21 at 16:11
  • You will also need to specify `/MongoPort=10255` as the default port of service is different than what is shown on web UI. Refer [this](https://github.com/MicrosoftDocs/azure-docs/issues/48295) thread for details. – CᴴᴀZ Jul 28 '22 at 08:02