# CData for MongoDB JDBC  error Authentication failed

**URL:** <https://community.cdata.com/t/cdata-for-mongodb-jdbc-error-authentication-failed/469>\
**Category:** CData Drivers\
**Tags:** question, mongodb, jdbc\
**Created:** [October 19, 2023, 9:06am UTC](https://community.cdata.com/t/cdata-for-mongodb-jdbc-error-authentication-failed/469 "2023-10-19T09:06:30Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![quangdutran809](https://avatars.discourse-cdn.com/v4/letter/q/439d5e/32.png) [@quangdutran809](https://community.cdata.com/u/quangdutran809)\
**Post date:** [October 19, 2023, 9:06am UTC](https://community.cdata.com/t/cdata-for-mongodb-jdbc-error-authentication-failed/469/1 "2023-10-19T09:06:31Z")

</div>

I’ve downloaded JDBC driver for MongoDB but when using it with the correct username password, it throws the error:

> Exception in thread “main” java.sql.SQLException: Failed to authenticate database ‘testdb’, Error code: [18] - “Authentication failed.”..

```java
        Class.forName("cdata.jdbc.mongodb.MongoDBDriver"); Properties prop = new Properties(); prop.setProperty("Server","127.0.0.1"); prop.setProperty("Port","27017"); prop.setProperty("Database","testdb"); prop.setProperty("User","useradmin"); prop.setProperty("Password","welcome!"); Connection conn = DriverManager.getConnection("jdbc:mongodb:", prop); Statement stat = conn.createStatement(); boolean ret = stat.execute("SELECT name FROM users");

```

It throws error on the last line. These username password are correct and can be used in mongosh inside mongo server container

---

<div class="post-metadata">

**Author:** ![Ankit\_Singh](https://sea3.discourse-cdn.com/cdata/user_avatar/community.cdata.com/ankit_singh/32/1614_2.png) [@Ankit\_Singh](https://community.cdata.com/u/Ankit_Singh)\
**Post date:** [October 20, 2023, 7:46am UTC](https://community.cdata.com/t/cdata-for-mongodb-jdbc-error-authentication-failed/469/2 "2023-10-20T07:46:14Z")

</div>

Hi @quangdutran809

Thanks for reaching out on CData Community!

By looking at your issue, I can think of two areas to investigate:

1.) Check if the User - _‘useradmin’_ is created under Database - _‘testdb’_&nbsp;and has access to the database. Mostly, the users created under _‘admin’_ database face such issues.

2.) Check again if the credentials are correct, you are using right cases, and there are no whitespaces.

---

<div class="post-metadata">

**Author:** ![Luçjano\_Capo](https://sea3.discourse-cdn.com/cdata/user_avatar/community.cdata.com/lu%C3%A7jano_capo/32/1638_2.png) [@Luçjano\_Capo](https://community.cdata.com/u/Lu%C3%A7jano_Capo)\
**Post date:** [October 20, 2023, 1:16pm UTC](https://community.cdata.com/t/cdata-for-mongodb-jdbc-error-authentication-failed/469/3 "2023-10-20T13:16:57Z")

</div>

Hi @quangdutran809 ,

One thing that I wanted to add is the utilization of [AuthDatabase](https://cdn.cdata.com/help/DGJ/jdbc/RSBMongodb_p_AuthDatabase.htm) connection property. I was able to replicate the same issue as you, but after setting this property, I was able to get data from mongoDB.

 ![6931f0c7-4336-4717-bec1-f9b303f23b7d.png](https://us1.discourse-cdn.com/cdata/original/2X/e/e57332d1c9dc4a9b219967f8867bd0f5c227b5e2.png)

I would also recommend you specify the [AuthScheme](https://cdn.cdata.com/help/DGJ/jdbc/RSBMongodb_p_AuthScheme.htm) as well.

Give this method a try and let me know how it goes.

---

<div class="post-metadata">

**Author:** ![quangdutran809](https://avatars.discourse-cdn.com/v4/letter/q/439d5e/32.png) [@quangdutran809](https://community.cdata.com/u/quangdutran809)\
**Post date:** [October 23, 2023, 3:56am UTC](https://community.cdata.com/t/cdata-for-mongodb-jdbc-error-authentication-failed/469/4 "2023-10-23T03:56:51Z")

</div>

Thanks @Ankit_Singh &nbsp;I think the user that I am using is the root user, which should have permissions on all db, this is how I set up that user in Docker:

```auto
FROM mongo:7.0.2ENV MONGO_INITDB_ROOT_USERNAME adminuserENV MONGO_INITDB_ROOT_PASSWORD welcome!ENV MONGO_INITDB_DATABASE testdb

```

Also, using MongoDB Compass with that user, I can browse dbtest normally.

Thanks @[Luçjano Capo](https://community.cdata.com/members/lucjano-capo-437), seem legit but I’ve added both `AuthScheme` and `AuthDatabase` but it still gives the same error&nbsp;😪

 ![119c9a6a-989f-4c42-9ed5-14dc1cf9efe8.png](https://us1.discourse-cdn.com/cdata/original/2X/3/34d0372d16e210b7769a37acbbc5d481ecf2a223.png)

---

<div class="post-metadata">

**Author:** ![quangdutran809](https://avatars.discourse-cdn.com/v4/letter/q/439d5e/32.png) [@quangdutran809](https://community.cdata.com/u/quangdutran809)\
**Post date:** [October 23, 2023, 4:03am UTC](https://community.cdata.com/t/cdata-for-mongodb-jdbc-error-authentication-failed/469/5 "2023-10-23T04:03:45Z")

</div>

Ah I’ve just realized that I’ve use the wrong username but adding `AuthDatabase` really solves the issue. Thanks guys, maybe this should be added to the document?

---

<div class="post-metadata">

**Author:** ![Ankit\_Singh](https://sea3.discourse-cdn.com/cdata/user_avatar/community.cdata.com/ankit_singh/32/1614_2.png) [@Ankit\_Singh](https://community.cdata.com/u/Ankit_Singh)\
**Post date:** [October 23, 2023, 7:35am UTC](https://community.cdata.com/t/cdata-for-mongodb-jdbc-error-authentication-failed/469/6 "2023-10-23T07:35:20Z")

</div>

Hi @quangdutran809  
I think it is part of the documentation here:&nbsp;[CData JDBC Driver for MongoDB - Establishing a Connection](https://cdn.cdata.com/help/DGJ/jdbc/pg_connectionj.htm#authenticating-mongodb-connections). Since there are different Auth Schemes, it can be a bit confusing.

---

<div class="post-metadata">

**Author:** ![kumar88](https://avatars.discourse-cdn.com/v4/letter/k/eb9ed0/32.png) [@kumar88](https://community.cdata.com/u/kumar88)\
**Post date:** [January 30, 2025, 11:43am UTC](https://community.cdata.com/t/cdata-for-mongodb-jdbc-error-authentication-failed/469/7 "2025-01-30T11:43:35Z")

</div>

Hi Team,  
I am trying to connect my MongoDB Cluster using JDBC driver.  
But I am getting below exception.  
&nbsp;(java.sql.SQLException) java.sql.SQLException: Unable to connect to cluster0.yw4hs.mongodb.net:27017: System error: UnknownHostException - No such host is known ([cluster0.yw4hs.mongodb.net](http://cluster0.yw4hs.mongodb.net))

Can you please help me .

---

<div class="post-metadata">

**Author:** ![Ethem\_Q](https://sea3.discourse-cdn.com/cdata/user_avatar/community.cdata.com/ethem_q/32/1997_2.png) [@Ethem\_Q](https://community.cdata.com/u/Ethem_Q)\
**Post date:** [January 31, 2025, 10:09pm UTC](https://community.cdata.com/t/cdata-for-mongodb-jdbc-error-authentication-failed/469/8 "2025-01-31T22:09:27Z")

</div>

Hi ​ @kumar88

The error message you are getting "_Unable to connect to cluster0.yw4hs.mongodb.net:27017: System error: UnknownHostException - No such host is known"_ would indicate that your MongoDB instance is unreachable which would be due to an issue caused by the Server connection property. Can you confirm you are setting the correct value for the Server property?&nbsp;Can you confirm your MongoDB instance is running and accessible? You can verify this by executing a ping request against your server within the command prompt.

There are also a couple of other things to take in consideration.&nbsp;Perhaps you are you trying to connect to a Mongo instance using DNS seed list?

If so the Server connection should be set as follows:

Server=mongodb+srv://cluster0.\*\*\*\*.mongodb.net;

Please note that the the use of SRV records eliminates the requirement for every client to pass in a complete set of state information for the cluster. Instead, a single SRV record resolves all the nodes associated with the cluster (and their port numbers) through the DNS Server. You can read more here:

> **[CData JDBC Driver for MongoDB - Establishing a Connection](https://cdn.cdata.com/help/DGK/jdbc/pg_connectionj.htm#connecting-to-mongodb-using-dns-seed-lists)**
>
> CData JDBC Driver for MongoDB - RSBMongodb - Establishing a Connection:

Alternatively, it could be that you are connecting to a ReplicaSet and for this reason you might need to fill in the proper connection properties to allow the MongoDB driver to access secondary Mongo nodes as well. You can read more about this here: [CData JDBC Driver for MongoDB - Establishing a Connection](https://cdn.cdata.com/help/DGK/jdbc/pg_connectionj.htm#connecting-to-replica-sets)

If you have any other questions feel free to reach out to the CData support team at [support@cdata.com](mailto:support@cdata.com).
