Hi @Joshua_Wang
Thank you for sharing the detailed logs and code snipped
I tried to connect to an M0 with X509 Certificate enabled cluster using the following:
> spring.data.mongodb.uri=
> mongodb+srv://cluster0.jrhrm.mongodb.net/myFirstDatabase?authSource=%24external&authMechanism=MONGODB-X509&retryWrites=true&w=majority
and I was able to connect to the database, Please refer to the logs below for validation:
> 2022-03-08 15:17:05.678 INFO 72205 --- [ restartedMain] com.springtest.demo.DemoApplication : Starting DemoApplication using Java 17.0.2 on Aasawaris-MacBook-Pro.local with PID 72205 (/Users/aasawari.sahasrabuddhe/Downloads/demo/target/classes started by aasawari.sahasrabuddhe in /Users/aasawari.sahasrabuddhe/Downloads/demo)
> 2022-03-08 15:17:05.679 INFO 72205 --- [ restartedMain] com.springtest.demo.DemoApplication : No active profile set, falling back to default profiles: default
> 2022-03-08 15:17:05.735 INFO 72205 --- [ restartedMain] .e.DevToolsPropertyDefaultsPostProcessor : Devtools property defaults active! Set 'spring.devtools.add-properties' to 'false' to disable
> 2022-03-08 15:17:05.735 INFO 72205 --- [ restartedMain] .e.DevToolsPropertyDefaultsPostProcessor : For additional web related logging consider setting the 'logging.level.web' property to 'DEBUG'
> 2022-03-08 15:17:06.185 INFO 72205 --- [ restartedMain] .s.d.r.c.RepositoryConfigurationDelegate : Bootstrapping Spring Data MongoDB repositories in DEFAULT mode.
> 2022-03-08 15:17:06.218 INFO 72205 --- [ restartedMain] .s.d.r.c.RepositoryConfigurationDelegate : Finished Spring Data repository scanning in 30 ms. Found 1 MongoDB repository interfaces.
> 2022-03-08 15:17:06.588 INFO 72205 --- [ restartedMain] o.s.b.w.embedded.tomcat.TomcatWebServer : Tomcat initialized with port(s): 8093 (http)
> 2022-03-08 15:17:06.595 INFO 72205 --- [ restartedMain] o.apache.catalina.core.StandardService : Starting service [Tomcat]
> 2022-03-08 15:17:06.595 INFO 72205 --- [ restartedMain] org.apache.catalina.core.StandardEngine : Starting Servlet engine: [Apache Tomcat/10.0.16]
> 2022-03-08 15:17:06.632 INFO 72205 --- [ restartedMain] o.a.c.c.C.[Tomcat].[localhost].[/] : Initializing Spring embedded WebApplicationContext
> 2022-03-08 15:17:06.633 INFO 72205 --- [ restartedMain] w.s.c.ServletWebServerApplicationContext : Root WebApplicationContext: initialization completed in 897 ms
> 2022-03-08 15:17:06.861 INFO 72205 --- [ restartedMain] org.mongodb.driver.cluster : Cluster created with settings {hosts=[127.0.0.1:27017], srvHost=cluster0.jrhrm.mongodb.net, mode=MULTIPLE, requiredClusterType=REPLICA_SET, serverSelectionTimeout='30000 ms', requiredReplicaSetName='atlas-292w65-shard-0'}
> 2022-03-08 15:17:06.978 INFO 72205 --- [hrm.mongodb.net] org.mongodb.driver.cluster : Adding discovered server cluster0-shard-00-02.jrhrm.mongodb.net:27017 to client view of cluster
> 2022-03-08 15:17:07.011 INFO 72205 --- [hrm.mongodb.net] org.mongodb.driver.cluster : Adding discovered server cluster0-shard-00-00.jrhrm.mongodb.net:27017 to client view of cluster
> 2022-03-08 15:17:07.012 INFO 72205 --- [hrm.mongodb.net] org.mongodb.driver.cluster : Adding discovered server cluster0-shard-00-01.jrhrm.mongodb.net:27017 to client view of cluster
> 2022-03-08 15:17:07.174 INFO 72205 --- [ restartedMain] o.s.b.d.a.OptionalLiveReloadServer : LiveReload server is running on port 35729
> 2022-03-08 15:17:07.519 INFO 72205 --- [ngodb.net:27017] org.mongodb.driver.connection : Opened connection [connectionId{localValue:5, serverValue:26636}] to cluster0-shard-00-01.jrhrm.mongodb.net:27017
> 2022-03-08 15:17:07.519 INFO 72205 --- [ngodb.net:27017] org.mongodb.driver.connection : Opened connection [connectionId{localValue:6, serverValue:24652}] to cluster0-shard-00-02.jrhrm.mongodb.net:27017
> 2022-03-08 15:17:07.519 INFO 72205 --- [ngodb.net:27017] org.mongodb.driver.connection : Opened connection [connectionId{localValue:1, serverValue:24652}] to cluster0-shard-00-02.jrhrm.mongodb.net:27017
> 2022-03-08 15:17:07.519 INFO 72205 --- [ngodb.net:27017] org.mongodb.driver.connection : Opened connection [connectionId{localValue:3, serverValue:25610}] to cluster0-shard-00-00.jrhrm.mongodb.net:27017
> 2022-03-08 15:17:07.519 INFO 72205 --- [ngodb.net:27017] org.mongodb.driver.connection : Opened connection [connectionId{localValue:2, serverValue:25610}] to cluster0-shard-00-00.jrhrm.mongodb.net:27017
> 2022-03-08 15:17:07.519 INFO 72205 --- [ngodb.net:27017] org.mongodb.driver.connection : Opened connection [connectionId{localValue:4, serverValue:26520}] to cluster0-shard-00-01.jrhrm.mongodb.net:27017
> 2022-03-08 15:17:07.519 INFO 72205 --- [ngodb.net:27017] org.mongodb.driver.cluster : Monitor thread successfully connected to server with description ServerDescription{address=cluster0-shard-00-02.jrhrm.mongodb.net:27017, type=REPLICA_SET_SECONDARY, state=CONNECTED, ok=true, minWireVersion=0, maxWireVersion=13, maxDocumentSize=16777216, logicalSessionTimeoutMinutes=30, roundTripTimeNanos=183920181, setName='atlas-292w65-shard-0', canonicalAddress=cluster0-shard-00-02.jrhrm.mongodb.net:27017, hosts=[cluster0-shard-00-02.jrhrm.mongodb.net:27017, cluster0-shard-00-00.jrhrm.mongodb.net:27017, cluster0-shard-00-01.jrhrm.mongodb.net:27017], passives=[], arbiters=[], primary='cluster0-shard-00-01.jrhrm.mongodb.net:27017', tagSet=TagSet{[Tag{name='nodeType', value='ELECTABLE'}, Tag{name='provider', value='AWS'}, Tag{name='region', value='AP_SOUTH_1'}, Tag{name='workloadType', value='OPERATIONAL'}]}, electionId=null, setVersion=7, topologyVersion=TopologyVersion{processId=62263dc244611ef569db1e07, counter=3}, lastWriteDate=Tue Mar 08 15:17:07 IST 2022, lastUpdateTimeNanos=196121665163299}
> 2022-03-08 15:17:07.519 INFO 72205 --- [ngodb.net:27017] org.mongodb.driver.cluster : Monitor thread successfully connected to server with description ServerDescription{address=cluster0-shard-00-01.jrhrm.mongodb.net:27017, type=REPLICA_SET_PRIMARY, state=CONNECTED, ok=true, minWireVersion=0, maxWireVersion=13, maxDocumentSize=16777216, logicalSessionTimeoutMinutes=30, roundTripTimeNanos=206546822, setName='atlas-292w65-shard-0', canonicalAddress=cluster0-shard-00-01.jrhrm.mongodb.net:27017, hosts=[cluster0-shard-00-02.jrhrm.mongodb.net:27017, cluster0-shard-00-00.jrhrm.mongodb.net:27017, cluster0-shard-00-01.jrhrm.mongodb.net:27017], passives=[], arbiters=[], primary='cluster0-shard-00-01.jrhrm.mongodb.net:27017', tagSet=TagSet{[Tag{name='nodeType', value='ELECTABLE'}, Tag{name='provider', value='AWS'}, Tag{name='region', value='AP_SOUTH_1'}, Tag{name='workloadType', value='OPERATIONAL'}]}, electionId=7fffffff0000000000000089, setVersion=7, topologyVersion=TopologyVersion{processId=62263c95fc481516f34bd6f1, counter=6}, lastWriteDate=Tue Mar 08 15:17:07 IST 2022, lastUpdateTimeNanos=196121665168063}
> 2022-03-08 15:17:07.519 INFO 72205 --- [ngodb.net:27017] org.mongodb.driver.cluster : Monitor thread successfully connected to server with description ServerDescription{address=cluster0-shard-00-00.jrhrm.mongodb.net:27017, type=REPLICA_SET_SECONDARY, state=CONNECTED, ok=true, minWireVersion=0, maxWireVersion=13, maxDocumentSize=16777216, logicalSessionTimeoutMinutes=30, roundTripTimeNanos=261042452, setName='atlas-292w65-shard-0', canonicalAddress=cluster0-shard-00-00.jrhrm.mongodb.net:27017, hosts=[cluster0-shard-00-02.jrhrm.mongodb.net:27017, cluster0-shard-00-00.jrhrm.mongodb.net:27017, cluster0-shard-00-01.jrhrm.mongodb.net:27017], passives=[], arbiters=[], primary='cluster0-shard-00-01.jrhrm.mongodb.net:27017', tagSet=TagSet{[Tag{name='nodeType', value='ELECTABLE'}, Tag{name='provider', value='AWS'}, Tag{name='region', value='AP_SOUTH_1'}, Tag{name='workloadType', value='OPERATIONAL'}]}, electionId=null, setVersion=7, topologyVersion=TopologyVersion{processId=62263b5055c8d646e1c81237, counter=4}, lastWriteDate=Tue Mar 08 15:17:07 IST 2022, lastUpdateTimeNanos=196121665201937}
> 2022-03-08 15:17:07.521 INFO 72205 --- [ngodb.net:27017] org.mongodb.driver.cluster : Setting max election id to 7fffffff0000000000000089 from replica set primary cluster0-shard-00-01.jrhrm.mongodb.net:27017
> 2022-03-08 15:17:07.521 INFO 72205 --- [ngodb.net:27017] org.mongodb.driver.cluster : Setting max set version to 7 from replica set primary cluster0-shard-00-01.jrhrm.mongodb.net:27017
> 2022-03-08 15:17:07.521 INFO 72205 --- [ngodb.net:27017] org.mongodb.driver.cluster : Discovered replica set primary cluster0-shard-00-01.jrhrm.mongodb.net:27017
> 2022-03-08 15:17:07.557 INFO 72205 --- [ restartedMain] o.s.b.w.embedded.tomcat.TomcatWebServer : Tomcat started on port(s): 8093 (http) with context path ''
> 2022-03-08 15:17:07.565 INFO 72205 --- [ restartedMain] com.springtest.demo.DemoApplication : Started DemoApplication in 2.143 seconds (JVM running for 2.532)
> 2022-03-08 15:17:07.829 INFO 72205 --- [ restartedMain] ConditionEvaluationReportLoggingListener :
Looking at your connection string URI, you’re using the SRV address, but the prefix for the connection format is only showing mongodb://
, try changing this to mongodb+srv://
.
Please refer to the documentation for URI Format
However would like to have a few more inputs
- With the uri mentioned in your code snippet, are you able to connect to the Atlas database through shell?
- Which cluster are you using for your project?
Please let us know so that we could provide you with more input.
Thanks
Aasawari