communicate from powershell (Windows) to mongodb server in linux

沟通从PowerShell(Windows)到MongoDB服务器在Linux

问题 (Question)

my question is very simple, I have:

  • Mongodb server running on a Linux server
  • Windows machine with powershell

how can I connect my powershell to insert/add/update documents from my Windows (powershell) server to mongodb database instance running on a remote linux server?

I tried using C++ drivers but does not connect.

this is my code:

$mongoDbDriverPath = "C:\scripts\CSharpDriver-1.9-rc0\";

Add-Type -Path "$($mongoDbDriverPath)\MongoDB.Bson.dll"
Add-Type -Path "$($mongoDbDriverPath)\MongoDB.Driver.dll"

$dbName = "mydb"
$collectionName = "backupData"
$db = [MongoDB.Driver.MongoDatabase]::Create("mongodb://<username>:<passsword>@<IP>:27017/$($dbName)")
$collection = $db[$collectionName]

$document = new-object MongoDB.Bson.BsonDocument
$document.Add("PreName",[MongoDB.Bson.BsonValue]::Create("Daniel"))
$document.Add("LastName",[MongoDB.Bson.BsonValue]::Create("Weber"))
$collection.save($document)

This is what I get

Exception calling "Save" with "1" argument(s): "Unable to connect to server <IP>:27017: A connection attempt failed because the connected party did not properly respond after a period of time, or established 
connection failed because connected host has failed to respond <IP>:27017."
At C:\scripts\mongo_test.ps1:14 char:17
+ $collection.save <<<< ($document)
    + CategoryInfo          : NotSpecified: (:) [], MethodInvocationException
    + FullyQualifiedErrorId : DotNetMethodException

Any suggestion?

thank you very much

我的问题很简单,我有:

  • MongoDB服务器在Linux服务器上运行
  • Windows机器PowerShell

我如何能连接我的PowerShell的插入/添加/更新我的Windows文件(脚本)服务器MongoDB数据库实例运行在远程Linux服务器?

我试着用C + +司机但不连接。

这是我的代码:

$mongoDbDriverPath = "C:\scripts\CSharpDriver-1.9-rc0\";

Add-Type -Path "$($mongoDbDriverPath)\MongoDB.Bson.dll"
Add-Type -Path "$($mongoDbDriverPath)\MongoDB.Driver.dll"

$dbName = "mydb"
$collectionName = "backupData"
$db = [MongoDB.Driver.MongoDatabase]::Create("mongodb://<username>:<passsword>@<IP>:27017/$($dbName)")
$collection = $db[$collectionName]

$document = new-object MongoDB.Bson.BsonDocument
$document.Add("PreName",[MongoDB.Bson.BsonValue]::Create("Daniel"))
$document.Add("LastName",[MongoDB.Bson.BsonValue]::Create("Weber"))
$collection.save($document)

这是我所得到的

Exception calling "Save" with "1" argument(s): "Unable to connect to server <IP>:27017: A connection attempt failed because the connected party did not properly respond after a period of time, or established 
connection failed because connected host has failed to respond <IP>:27017."
At C:\scripts\mongo_test.ps1:14 char:17
+ $collection.save <<<< ($document)
    + CategoryInfo          : NotSpecified: (:) [], MethodInvocationException
    + FullyQualifiedErrorId : DotNetMethodException

任何建议吗?

非常感谢你

最佳答案 (Best Answer)

SOLUTION:

NOTE: my linux distribution is Ubuntu 11.04

  1. edit your config file /etc/mongodb.conf file with:

    bind_ip = 0.0.0.0

  2. restart yout mongodb service:

    /etc/init.d/mondogb restart

  3. Add following rules to your IP tables:

    iptables -A INPUT -s -p tcp --destination-port 27017 -m state --state NEW,ESTABLISHED -j ACCEPT iptables -A OUTPUT -d -p tcp --source-port 27017 -m state --state ESTABLISHED -j ACCEPT

  4. make ip tables changes persistent

    iptables-save > /etc/iptables.conf

Hope it helpds!

解决方案

笔记:我的Linux分布是Ubuntu 11.04

  1. 编辑您的配置文件/etc/ mongodb.conf文件:

    bind_ip = 0.0.0.0

  2. 重新启动你的MongoDB服务:

    init.d/mondogb /重启

  3. 添加下面的规则,你的IP表:

    iptables -输入-S-P TCP——目的港27017米的状态——新建立的国家,J接受 iptables -输出- D - P的TCP——源端口27017 M州建立的国家——J接受

  4. 使IP表变化的持续性

    /etc/iptables.conf">iptables保存> / iptables.conf

希望这helpds!

答案 (Answer) 2

If you only want make queries against your MongoDB server, you can download MongoDB for Windows and use MongoDB Shell (mongo.exe).

mongo.exe server/port

Then you will be able to make queries, insert documents, etc.

If you want use MongoDB directly from PowerShell, you can use C# driver. Here is an example.

如果你只想在你的服务器中进行查询,您可以下载Windows使用MongoDB shell MongoDB(芒果。EXE)。

mongo.exe server/port

然后你就可以进行查询,插入文件,等等。

如果你想使用MongoDB直接从脚本,您可以使用C #司机。这里是一个例子。

本文翻译自StackoverFlow,英语好的童鞋可直接参考原文:http://stackoverflow.com/questions/22108577