无法使用java在Amazon EC2实例上连接MongoDB

S A*_*A R 5 android mms amazon-ec2 mongodb amazon-web-services

我正在开发一个使用mongoDB进行数据存储的android应用程序,目前我正在使用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");
Run Code Online (Sandbox Code Playgroud)

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

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
Run Code Online (Sandbox Code Playgroud)

注意:我没有在错误中公开主机和端口值.

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

Cle*_*ath 1

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

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

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

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

4) PEM 文件无效

有用的链接:

如何使用 Java 驱动程序连接到 EC2 上的 MongoDB

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