【问题标题】:Unable to Connect MongoDB on Amazon EC2 Instance using java无法使用 java 在 Amazon EC2 实例上连接 MongoDB
【发布时间】:2015-09-01 15:51:19
【问题描述】:

我正在开发一个 android 应用程序,它使用 mongoDB 进行数据存储,目前我正在使用 Mongolab 沙箱来存储数据。我正在考虑将数据库移动到 Amazon Ec2 实例,并且我已经通过 MMS 服务对其进行了配置。

我已经创建了一个数据库用户,我可以通过 putty 连接到数据库并执行 CURD 操作。现在我正在探索如何从 MongoDB JAVA 驱动程序连接到 Ec2 实例。

我现在正在使用此代码连接到数据库

MongoClientURI uri = new MongoClientURI("mongodb://" + myUserName + ":" + myPassword + "@" + DB + "/" + DB_NAME + "?authMechanism=MONGODB-CR");
        MongoClient mongoClient = new MongoClient(uri);
        MongoDatabase blogDatabase = mongoClient.getDatabase(DB_NAME);
        MongoCollection<Document> usersCollection = blogDatabase.getCollection("users");

但我无法连接数据库。它给了我一个错误

Aug 27, 2015 9:12:14 PM com.mongodb.diagnostics.logging.JULLogger log
INFO: Cluster created with settings {hosts=[5fdef03a@host:port], mode=SINGLE, requiredClusterType=UNKNOWN, serverSelectionTimeout='30000 ms', maxWaitQueueSize=500}
Aug 27, 2015 9:12:14 PM com.mongodb.diagnostics.logging.JULLogger log
INFO: Exception in monitor thread while connecting to server 5fdef03a@host:port
com.mongodb.MongoSocketException: 5fdef03a@host
    at com.mongodb.ServerAddress.getSocketAddress(ServerAddress.java:188)
    at com.mongodb.connection.SocketStreamHelper.initialize(SocketStreamHelper.java:37)
    at com.mongodb.connection.SocketStream.open(SocketStream.java:53)
    at com.mongodb.connection.InternalStreamConnection.open(InternalStreamConnection.java:96)
    at com.mongodb.connection.DefaultServerMonitor$ServerMonitorRunnable.run(DefaultServerMonitor.java:127)
    at java.lang.Thread.run(Unknown Source)
Caused by: java.net.UnknownHostException: 5fdef03a@host
    at java.net.Inet6AddressImpl.lookupAllHostAddr(Native Method)
    at java.net.InetAddress$2.lookupAllHostAddr(Unknown Source)
    at java.net.InetAddress.getAddressesFromNameService(Unknown Source)
    at java.net.InetAddress.getAllByName0(Unknown Source)
    at java.net.InetAddress.getAllByName(Unknown Source)
    at java.net.InetAddress.getAllByName(Unknown Source)
    at java.net.InetAddress.getByName(Unknown Source)
    at com.mongodb.ServerAddress.getSocketAddress(ServerAddress.java:186)
    ... 5 more

Aug 27, 2015 9:12:17 PM com.mongodb.diagnostics.logging.JULLogger log
INFO: No server chosen by PrimaryServerSelector from cluster description ClusterDescription{type=UNKNOWN, connectionMode=SINGLE, all=[ServerDescription{address=5fdef03a@host:port, type=UNKNOWN, state=CONNECTING, exception={com.mongodb.MongoSocketException: 5fdef03a@host}, caused by {java.net.UnknownHostException: 5fdef03a@host}}]}. Waiting for 30000 ms before timing out

注意:我没有在错误中披露主机和端口值。

即使所有凭据都正确,任何人都可以帮助我弄清楚该怎么做。在我发现的大多数帖子中,他们要求创建一个 REST API 来连接,但我认为应该有任何其他方式来连接 MongoDB JAVA 驱动程序本身。还有没有其他方法可以访问数据库。

【问题讨论】:

  • 真的很可怜,因为任何好的答案都会向您解释为什么您不应该将移动设备直接连接到数据库。您和您的应用程序将更好地构建一个服务器端 API,您的移动应用程序可以与该 API 通信,然后它又与数据库通信。与将各地的远程客户端直接连接到数据库相比,这为您提供了更好的控制和安全性。
  • 错误本身说明主机名不正确。但除此之外,我只想指出,如果您使用 MongoDB 3.0+ 并在 3.0+ 中创建用户,那么您需要使用身份验证方案 SCRAM-SHA-1,因为它们完全删除了 MONGODB-CR。如果用户是在 3.0 之前的版本中创建的,那么它仍然具有身份验证方案 MONGODB-CR。 docs.mongodb.org/manual/core/authentication

标签: android mongodb amazon-web-services amazon-ec2 mms


【解决方案1】:

检查以下内容,可能对您有所帮助

1) 您访问 mongodb 的端口可能被您的网络安全阻止。

2) Mongo Server 没有在 SSH 服务器主机上运行

3) 您尝试连接的用户权限不合适

4) PEM 文件无效

有用的链接:

How to connect to MongoDB on EC2 using Java driver

http://docs.mongodb.org/manual/faq/diagnostics/#Troubleshooting-Socketerrorsinshardedclustersandreplicasets

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2016-09-22
    • 2013-07-20
    • 2017-02-28
    • 2013-01-06
    • 1970-01-01
    • 2016-08-20
    • 2018-01-21
    • 1970-01-01
    相关资源
    最近更新 更多