gpt4 book ai didi

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

转载 作者:IT老高 更新时间:2023-10-28 12:32:12 24 4
gpt4 key购买 nike

我正在开发一个 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 驱动程序本身。还有没有其他方法可以访问数据库。

最佳答案

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

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

关于android - 无法使用 java 在 Amazon EC2 实例上连接 MongoDB,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32254072/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com