By looking at your issue, I can think of two areas to investigate:
1.) Check if the User - ‘useradmin’ is created under Database - ‘testdb’ 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.
One thing that I wanted to add is the utilization of AuthDatabase 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.
Thanks @Ankit_Singh 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:
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, seem legit but I’ve added both `AuthScheme` and `AuthDatabase` but it still gives the same error 😪
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?
Hi Team,
I am trying to connect my MongoDB Cluster using JDBC driver.
But I am getting below exception.
(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)
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? 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. 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:
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
If you have any other questions feel free to reach out to the CData support team at support@cdata.com.
This website stores cookies on your computer. These cookies are used to collect information about how you interact with our website and allow us to remember you. We use this information in order to improve and customize your browsing experience and for analytics and metrics about our visitors both on this website and other media.
Review section III of our Privacy Policy for more information on cookies.
To opt out of “sale” or “sharing” of non-cookie data, visit our Privacy Rights Request Form.