简体   繁体   English

如何连接到用于MongoDB的Azure CosmosDB仿真器?

[英]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: 尝试使用MongoDB连接字符串mongodb://localhost:C2y6yDjf5%2FR%2Bob0N8A7Cgv30VRDJIWEHLM%2B4QDU5DE2nQ9nDuVTqobD4b8mGGyPMbIZnqyMsEcaGQy67XIw%2FJw%3D%3D@localhost:10255/admin?ssl=true以下错误:

{ 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 我已经从Microsoft下载中心安装了最新版本的仿真器(当前为v2.4.5),并按照Microsoft文档的说明进行了安装

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. 我尝试使用Mongoose与Node.js(与Azure云配置完美配合)进行连接,并使用VS Code Azure CosmosDB扩展进行连接。 Both yield the same error. 两者都产生相同的错误。

What is the proper way to connect to the local emulator? 连接到本地仿真器的正确方法是什么?

Turns out I needed to run the emulator from the command-line and enable the mongodb endpoint. 原来,我需要从命令行运行模拟器并启用mongodb端点。 No need to run MongoDB separately. 无需单独运行MongoDB。

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. 运行这些命令后,我能够使用Azure Emulator for MongoDB提供的连接字符串,并成功连接和读取/写入本地数据库。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM