Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I connect to the Azure CosmosDB Emulator for MongoDB?

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?

like image 778
halshing Avatar asked Aug 10 '19 02:08

halshing


People also ask

How do I connect to Azure Cosmos DB?

Access Azure Cosmos DB Explorer Sign in to Azure portal. From All resources, find and navigate to your Azure Cosmos DB account, select Keys, and copy the Primary Connection String. Go to https://cosmos.azure.com/, paste the connection string and select Connect.

How do I connect my cosmos emulator?

Begin typing Azure Cosmos DB Emulator, and select the emulator from the list of applications. When the emulator has started, you'll see an icon in the Windows taskbar notification area. It automatically opens the Azure Cosmos DB data explorer in your browser at this URL https://localhost:8081/_explorer/index.html URL.

Is Cosmos DB compatible with MongoDB?

The Azure Cosmos DB for MongoDB is compatible with MongoDB server version 3.6 by default for new accounts. The supported operators and any limitations or exceptions are listed below. Any client driver that understands these protocols should be able to connect to Azure Cosmos DB for MongoDB.


1 Answers

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.

like image 87
halshing Avatar answered Oct 12 '22 01:10

halshing