简体   繁体   English

如何在Java中远程连接MongoDB?

[英]How to connect MongoDB remotely in java?

I am trying to connect MongoDB at Remote server connected in LAN. 我正在尝试在LAN中连接的远程服务器上连接MongoDB。 I tried to connect using MongoClient() method by using IP of remote machine: 我尝试通过使用远程计算机的IP使用MongoClient()方法进行连接:

Mongo m=new MongoClient("192.168.1.38",27017);

However, it gives the exception: 但是,它给出了例外:

java.net.NoRouteToHostException: No route to host. java.net.NoRouteToHostException:没有到主机的路由。

You are probably connecting fine but don't have sufficient privileges to run show dbs. 您可能连接正常,但没有足够的特权来运行show dbs。

You don't need to run the db.auth if you pass the auth in the command line: 如果在命令行中通过auth,则无需运行db.auth:

mongo somewhere.mongolayer.com:10011/my_database -u username -p password mongo某处。mongolayer.com:10011/my_database -u用户名-p密码

Once you connect are you able to see collections? 连接后,您是否可以看到收藏集?

show collections 显示收藏

If so all is well and you just don't have admin privileges to the database and can't run the show dbs 如果这样的话,一切都很好,而您只是对该数据库没有管理员权限,并且无法运行show dbs

You can use MongoClientUri to connect remotely in JAVA 您可以使用MongoClientUri在JAVA中进行远程连接

For eg MongoClientUri client=new MongoClientUri(machine addres); 例如,MongoClientUri client = new MongoClientUri(machine addres);

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

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