8
votes

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?

1
did you start the local mongo db?Sajeetharan
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
you don't need to set localhost to mongodb. Just specify localhost:27017 in the connection stringAtish
thanks for the response. unfortunately using mongodb://localhost:C2y6yDjf5%2FR%2Bob0N8A7Cgv30VRDJIWEHLM%2B4QDU5DE2nQ9nDuVTqobD4b8mGGyPMbIZnqyMsEcaGQy67XIw%2FJw%3D%3D@localhost:27017/admin?ssl=true still doesn't workhalshing

1 Answers

17
votes

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.