MongoDB atlas,连接不工作

0kjbasz6  于 2023-02-03  发布在  Go
关注(0)|答案(5)|浏览(237)

我刚刚创建了一个免费的MongoDB atlas集群示例,但不知何故,我无法将它从我的应用程序以及MongoDB Compas连接起来。
当我尝试运行我的应用程序时,我得到下面的错误。

Caused by: com.mongodb.MongoTimeoutException: Timed out after 30000 ms while waiting for a server that matches WritableServerSelector. Client view of cluster state is {type=UNKNOWN, servers=[{address=experimental-1-epdri.mongodb.net:27017, type=UNKNOWN, state=CONNECTING, exception={com.mongodb.MongoSocketException: experimental-1-epdri.mongodb.net}, caused by {java.net.UnknownHostException: experimental-1-epdri.mongodb.net}}]
    at com.mongodb.connection.BaseCluster.createTimeoutException(BaseCluster.java:377) ~[mongodb-driver-core-3.4.2.jar:na]
    at com.mongodb.connection.BaseCluster.selectServer(BaseCluster.java:104) ~[mongodb-driver-core-3.4.2.jar:na]
    at com.mongodb.binding.ClusterBinding$ClusterBindingConnectionSource.<init>(ClusterBinding.java:75) ~[mongodb-driver-core-3.4.2.jar:na]
    at com.mongodb.binding.ClusterBinding$ClusterBindingConnectionSource.<init>(ClusterBinding.java:71) ~[mongodb-driver-core-3.4.2.jar:na]
    at com.mongodb.binding.ClusterBinding.getWriteConnectionSource(ClusterBinding.java:68) ~[mongodb-driver-core-3.4.2.jar:na]
    at com.mongodb.operation.OperationHelper.withConnection(OperationHelper.java:411) ~[mongodb-driver-core-3.4.2.jar:na]
    at com.mongodb.operation.CreateIndexesOperation.execute(CreateIndexesOperation.java:144) ~[mongodb-driver-core-3.4.2.jar:na]
    at com.mongodb.operation.CreateIndexesOperation.execute(CreateIndexesOperation.java:71) ~[mongodb-driver-core-3.4.2.jar:na]
    at com.mongodb.Mongo.execute(Mongo.java:845) ~[mongodb-driver-3.4.2.jar:na]
    at com.mongodb.Mongo$2.execute(Mongo.java:828) ~[mongodb-driver-3.4.2.jar:na]
    at com.mongodb.MongoCollectionImpl.createIndexes(MongoCollectionImpl.java:491) ~[mongodb-driver-3.4.2.jar:na]
    at com.mongodb.MongoCollectionImpl.createIndex(MongoCollectionImpl.java:458) ~[mongodb-driver-3.4.2.jar:na]
    at org.axonframework.mongo.eventsourcing.eventstore.AbstractMongoEventStorageStrategy.ensureIndexes(AbstractMongoEventStorageStrategy.java:201) ~[axon-mongo-3.0.5.jar:3.0.5]
    at org.axonframework.mongo.eventsourcing.eventstore.MongoEventStorageEngine.ensureIndexes(MongoEventStorageEngine.java:123) ~[axon-mongo-3.0.5.jar:3.0.5]
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[na:1.8.0_111]
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) ~[na:1.8.0_111]
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) ~[na:1.8.0_111]
    at java.lang.reflect.Method.invoke(Method.java:498) ~[na:1.8.0_111]
    at org.springframework.beans.factory.annotation.InitDestroyAnnotationBeanPostProcessor$LifecycleElement.invoke(InitDestroyAnnotationBeanPostProcessor.java:366) ~[spring-beans-4.3.10.RELEASE.jar:4.3.10.RELEASE]
    at org.springframework.beans.factory.annotation.InitDestroyAnnotationBeanPostProcessor$LifecycleMetadata.invokeInitMethods(InitDestroyAnnotationBeanPostProcessor.java:311) ~[spring-beans-4.3.10.RELEASE.jar:4.3.10.RELEASE]
    at org.springframework.beans.factory.annotation.InitDestroyAnnotationBeanPostProcessor.postProcessBeforeInitialization(InitDestroyAnnotationBeanPostProcessor.java:134) ~[spring-beans-4.3.10.RELEASE.jar:4.3.10.RELEASE]
    ... 76 common frames omitted

Process finished with exit code 1

在Compas上,它只是说MongoDB示例不在这个地方运行。我检查了我的集群,我看到下面。

但我仍然无法连接到Mongo集群。进一步我尝试了'Mongo CLI以及我发现如下所示的错误。

下面是我从MongoDB atlas页面获得的连接字符串。
mongodb+srv://admin_eventdb:<PASSWORD>@experimental-1-epdri.mongodb.net/test?retryWrites=true
请帮忙解决这个问题。

lawou6xi

lawou6xi1#

请尝试:

Hostname: experimental-1-epdri.mongodb.net
SRV Record: checked
Authentication: Username / Password
Username: admin_eventdb
Password: <PASSWORD>
Authentication Database: admin
Replica Set Name:
Read Preference: Primary
SSL: System CA / Atlas Deployment
SSL Tunnel: None
soat7uwm

soat7uwm2#

我有相同的issue。通过使用Java驱动程序3.4的连接字符串,它将是:

mongodb://user:<PASSWORD>@cluster0-shard-00-00-ox90k.mongodb.net:27017,cluster0-shard-00-01-ox90k.mongodb.net:27017,cluster0-shard-00-02-ox90k.mongodb.net:27017/test?ssl=true&replicaSet=Cluster0-shard-0&authSource=admin&retryWrites=true

而不是<PASSWORD>我写自己的密码。admin1是我的管理员用户。

**更新:**如果你想使用驱动程序3.7+,你需要写而不是格式连接(并避免我上面的问题)

MongoClientURI uri = new MongoClientURI("mongodb+srv://admin:mypassword@cluster0-ox90k.mongodb.net/test?retryWrites=true");
MongoClient mongoClient = new MongoClient(uri);

使用MongoClients.create()(as of the 3.7 release),的另一变体:

MongoClient mongoClient = MongoClients.create("mongodb+srv://admin:mypassword@cluster0-ox90k.mongodb.net/test?retryWrites=true");

**注:**密码不需要像mongodb://user:<mypassword>@...那样书写,只需采用mongodb://user:mypassword@...,不带大括号<>

xuo3flqw

xuo3flqw3#

I point I mentioned

Please login to mongodb atlas
1.go to security section
2.network access
Then allow anywhere access that it's may be helps.
vcudknz3

vcudknz34#

对于Node.js: checkout Atlas MongoDB连接

const mongoose = require('mongoose');
const conStr = 'mongodb+srv://lord:<password>@cluster5-eeev8.mongodb.net/test?retryWrites=true&w=majority'

const DB = conStr.replace(
  '<password>',
  myPass
);

const DB = conStr.replace(
  'test',
  myDatabaseName
);
//remember mongoose.connect() return promise
mongoose
  .connect(DB, {
    usedNewUrlParser: true,
    useCreateIndex: true,
    useFindAndModify: false,
  })
  .then((con) => {
    console.log(con.connection);
    console.log('DB connection successful');
  });
kb5ga3dv

kb5ga3dv5#

刚刚尝试使用Atlas。这是我使用Sping Boot 2.3.1.RELEASE时使用的配置

spring:
  data:
    mongodb:
      database: sample_training 
      uri: mongodb+srv://<user>:<pass>@cluster0-hosturl.aws|gcp.mongodb.net/?retryWrites=true&w=majority

相关问题