简体   繁体   English

尝试连接到 android 上的 mongodb 数据库时出错

[英]Getting an error trying to connect to mongodb database on android

The error that I am getting is我得到的错误是

com.mongodb.MongoTimeoutException: Timed out after 30000 ms while waiting to connect. Client view of cluster state is {type=UNKNOWN, servers=[{address=localhost:27017, type=UNKNOWN, state=CONNECTING, exception={com.mongodb.MongoSocketOpenException: Exception opening socket}, caused by {java.net.ConnectException: failed to connect to localhost/127.0.0.1 (port 27017) from /127.0.0.1 (port 43950) after 10000ms: isConnected failed: ECONNREFUSED (Connection refused)}, caused by {android.system.ErrnoException: isConnected failed: ECONNREFUSED (Connection refused)}}]

I'm assuming the issue here is that the AVD is using the same IP as my computer, but if I try to change it in the settings of the AVD it fails to connect to androidWifi.我假设这里的问题是 AVD 使用与我的计算机相同的 IP,但是如果我尝试在 AVD 的设置中更改它,它无法连接到 androidWifi。 (I tried changing it to 10.0.2.2, because that's the solution I found on annother stackoverflow post, it didn't work.) (我尝试将其更改为 10.0.2.2,因为这是我在另一篇 stackoverflow 帖子中找到的解决方案,但没有奏效。)

If it matters, my code looks like this:如果重要,我的代码如下所示:

    MongoClient client = MongoClients.create("mongodb://localhost:27017");
    MongoDatabase db = client.getDatabase("userData");
    MongoCollection<Document> users = db.getCollection("users");

    Document user = new Document("name", "user").append("password", "pass");

    users.insertOne(user);

Adding this answer for posterity - the reason why this error occurs is because in the line:为后代添加此答案 - 发生此错误的原因是因为在该行中:

 MongoClient client = MongoClients.create("mongodb://localhost:27017");

the DB address is being set to the ANDROID'S localhost and NOT the PC's that's hosting the AVD. DB 地址被设置为 ANDROID 的本地主机,而不是托管 AVD 的 PC。 To fix this, find your ipv4 address using the command ipconfig in CMD, then, when running the mongo shell type the following command: mongod --bind_ip "yourIPHere"要解决此问题,请使用 CMD 中的命令ipconfig找到您的 ipv4 地址,然后在运行 mongo shell 时键入以下命令: mongod --bind_ip "yourIPHere"

please do note, however that this will open your database up to external connections, if someone happens to have your internal IP address.请注意,如果有人碰巧拥有您的内部 IP 地址,这将打开您的数据库以进行外部连接。

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

相关问题 尝试将MySQL数据库连接到android时遇到NullPointerException错误 - Getting a NullPointerException error trying to connect a MySQL database to android 如何连接到Android中的MongoDB数据库? - How to connect to a MongoDB database, in Android? 尝试连接到 MYSQL 数据库时出现 EJBTransactionRolledbackException - Getting an EJBTransactionRolledbackException when trying to connect to a MYSQL database 我正在尝试使用休眠xml映射连接到mysql数据库,但出现此错误 - I am trying to connect to a mysql database with hibernate xml mapping but I am getting this error 尝试在Maven项目中使用OpenJPA连接到Derby Database,但出现“ java.sql.SQLException”错误。 - Trying to connect to Derby Database using OpenJPA in a Maven project, but getting a “java.sql.SQLException” error. 尝试连接到 H2 数据库时出错 - Error when trying to connect to H2 database 尝试将 Java 与数据库连接时出错 - Error while trying to connect Java with database 尝试连接到 Firestore 后出现 Android Java 错误 - Android Java Error after trying to connect to Firestore 使用@Component 后连接数据库时出错 - Getting error to connect database after using @Component 尝试将 Spring Boot 应用程序与 mongoDB 连接时出错 - Error when trying to connect Spring boot application with mongoDB
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM