Unable to connect to MongoLab from java driver - mongodb

I have Mongolab account , there is only one user in that account.
I can connect to the DB using MongoChef tool, however when I try and connect to that DB from within Eclipse using the java driver 3.2.0 , I continuously get an authentication failure. I get the following error.I am on Java 7 .
EDIT
Java Code used to connect to the MongoLab hosted MongoDB instance.
String mongoURL = new StringBuilder().append("mongodb://").append(mongoUserName).append(":").append(mongoPassword).append("#").append(mongoServer).append(":").append(mongoPort).append("/").append(dbName).toString();
System.out.println(" The Mongo URL is " +mongoURL);
MongoClientURI uri = new MongoClientURI(mongoURL);
MongoClient mongo = new MongoClient(uri);
Exception received when trying to connect.
com.mongodb.MongoTimeoutException: Timed out after 30000 ms while
waiting for a server that matches
ReadPreferenceServerSelector{readPreference=primary}. Client view of
cluster state is {type=UNKNOWN,
servers=[{address=ds061974.mongolab.com:61974, type=UNKNOWN,
state=CONNECTING, exception={com.mongodb.MongoSecurityException:
Exception authenticating MongoCredential{mechanism=null,
userName='xxxx', source='hidden', password=<hidden>,
mechanismProperties={}}}, caused by
{com.mongodb.MongoCommandException: Command failed with error 18:
'Authentication failed.' on server ds061974.mongolab.com:61974. The
full response is { "ok" : 0.0, "code" : 18, "errmsg" : "Authentication
failed." }}}]

try this code:
ServerAddress addr = new ServerAddress(mongoServer, mongoPort);
MongoCredential credential = MongoCredential.createMongoCredential(
mongoUserName, mongoPassword, dbName);
MongoClient mongoClient = new MongoClient(addr, Arrays.asList(credential), null);

Related

Creating a local connection in Oracle SQL developer 12c

Not sure if I'm missing anything.
I'm trying to create a local connection in order to create a new user in Oracle SQL developer 12c. (AVOIDING CDB connection)
After browsing for an hour, came across 2 methods.
Using connection type as "Local/Bequeath"
Throws following error (Status : Failure -Test failed: ORA-01017: invalid username/password; logon denied)
Using connection type as "Basic", Hostname as "localhost", Port as "1521", and Service name as "XEPDB1"
Throws following error (Status : Failure -Test failed: Listener refused the connection with the following error:
ORA-12514, TNS:listener does not currently know of service requested in connect descriptor)
Thanks.

Unable to connect to MongoDB Atlas using Vert.x MongoClient

I'm trying to connect to a MongoDB database hosted in MongoDB Atlas using Vert.x MongoClient:
I'm creating MongoClient with this code (Kotlin):
MongoClient.create(vertx, jsonObjectOf(
"connection_string" to "mongodb+srv://<username>:<password>#<cluster_address>/my_db"
))
(with real values, obviously)
But I'm unable to perform any operation agains database. I get a connection timeout error:
com.mongodb.MongoTimeoutException: Timed out after 30000 ms while
waiting for a server that matches
ReadPreferenceServerSelector{readPreference=primary}. Client view of
cluster state is {type=REPLICA_SET,
servers=[{address=<server1_address>, type=UNKNOWN, state=CONNECTING},
{address=<server2_address>, type=UNKNOWN, state=CONNECTING},
{address=<server3_address>, type=UNKNOWN, state=CONNECTING}]
If I try to connect using mongosh (CLI tool) and MongoDB Compass using the same connection string, it works fine.
I have tried confguring the client with parameters (hosts, ports, etc...) instead of using a connection string and I get this error when I make a query:
MongoSocketReadException: Prematurely reached end of stream
After 11 days struggling with this, I have just solved it.
I have included "trustAll" : true option when creating MongoClient and it's working now. It's not the best solution, but anyway...

Azure Cosmos DB Emulator : Unable to connect to MongoDb via MongoChef

After following the instructions from the following official reference "Use the Azure Cosmos DB Emulator for local development and testing", we attempted to connect to MongoDb using MongoChef using the connection string pasted below:
mongodb://localhost:C2y6yDjf5/R+ob0N8A7Cgv30VRDJIWEHLM+4QDU5DE2nQ9nDuVTqobD4b8mGGyPMbIZnqyMsEcaGQy67XIw/Jw==#localhost:10255/admin?ssl=true
But when we test the connection through MongoChef we get the error pasted below:
Connection failed.
SERVER [localhost:10255] (Type: UNKNOWN)
|_/ Connection error (MongoSocketOpenException): Exception opening socket
|____/ Socket error: Connection refused: connect
Details:
Timed out after 30000 ms while waiting to connect. Client view of cluster state is {type=UNKNOWN, servers=[{address=localhost:27018, type=UNKNOWN, state=CONNECTING, exception={com.mongodb.MongoSocketOpenException: Exception opening socket}, caused by {java.net.ConnectException: Connection refused: connect}}]
If we check the ports currently in use on our system, we do not see 10255 being used at all.
Could someone please help us understand what's wrong here.
The connection string is correct. Studio 3T has a very nasty bug - when you use From URI function while creating new connection it cuts the "+" characters that are present in the key, that's why you need to copy the key manually to the corresponding field in connection properties.
Also, make sure the Emulator is actually launched (tray area icon should be present).

Jongo connect to remote MongoDB server

Is it possible to connect to a remote MongoDB when using Jongo (jongo.org)?
I saw a piece of code where MongoClientURI was used like this:
MongoClientURI uri = new MongoClientURI("mongodb://IP_ADDRESS:27017/DB_NAME");
I have the following code:
if(client != null) {
db = client.getDatabase("StockApp");
database = client.getDB("StockApp");
jongo = new Jongo(database);
}
In this example, StockApp is the name of my database. It will connect to my local database (127.0.0.1:27017/StockApp). When I try to change StockApp to uri.getDatabase() in both lines, I get the following exception:
com.mongodb.MongoSocketOpenException: Exception opening socket
I can also see that it tries to connect to localhost (127.0.0.1).
When I change the uri to new MongoClientURI("IP_ADDRESS") or new MongoClientURI("IP_ADDRESS:27017) I get the error that the uri should start with mongodb://
Does anyone know if it is possible to connect to a remote MongoDB server using Jongo?
You can initialize Jongo from a MongoClient like this:
MongoClient mongoClient = new MongoClient("host", 27017);
DB db = mongoClient.getDB("theDB");
Jongo jongo = new Jongo(db);
You can check the MongoClient constructor detail here

Unable to connect to MongoDb using MongoClientSettings as parameter to MongoClient

I am developing a C# MVC Web API which uses MongoDb as backend.I tried connecting to my mongodb database using
MongoClient mongoClient = new MongoClient(connectionString)
where connectionstring is in format : mongodb://Username:Password#hostname.eastus.cloudapp.azure.com
Mongo db is hosted in a virtual machine in Azure.I am able to connect to the database and all works good.But I am getting frequent exceptions:
"MongoDb.driver.MongoConnectionException".An exception occurred while
receivinf a message from server--->System.IO.IOException:Unable to
read data from the transport connection : A connection attempt failed
because the connected party did not properly respond after a period of
time,......"
So after a bit of research I have learnt that Azure is killing idle connections and I have to set MaxConnectionIdleTime.
In order to set MaxConnectionIdleTime I decided to connect to Mongodb in the below way
var credential = MongoCredential.CreateCredential("dbname", "UserName", "Password");
var settings = new MongoClientSettings
{
Credentials = new[] { credential },
Server = new MongoServerAddress("HostName", 27017),
MaxConnectionIdleTime = new TimeSpan(0, 3, 0)
};
MongoClient mongoClient = new MongoClient(settings);
In this case I am using the same username,password combination given in the connection string which I used to connect before.
While trying to connect here I am getting inner Exception:
MongoDB.Driver.MongoAuthenticationException: "Unable to authenticate
using sasl protocol mechanism SCRAM-SHA-1".
"MongoDb.driver.MongoConnectionException".An exception occurred while receivinf a message from server--->System.IO.IOException:Unable to read data from the transport connection : A connection attempt failed because the connected party did not properly respond after a period of time,....
The reason behind this exception is when hosted in Azure,Azure tries to kill the idle connections but the C# driver is not aware of this.The driver tries to execute queries on the killed connections without knowing the connection is not existing.
The solution that worked out for me is to set maxIdleTimeMS=45000 in connection string.
This way driver will not use a connection which has been idle for long time.
Here is the connection string that worked out for me
connectionString="Username:Password#hostname.eastus.cloudapp.azure.com/?connectTimeoutMS=30000&socketTimeoutMS=30000&waitQueueTimeoutMS=30000&maxIdleTimeMS=45000"
I have had a similar error with my Azure hosted MongoDb (Cosmos Db). It turned out to be network settings such that I had blocked all access. Changing it to Allow access from "All networks" fixed the issue.
The error is very misleading, I would have expected a connection timeout.
A timeout occured after 30000ms selecting a server using
CompositeServerSelector{ Selectors =
MongoDB.Driver.MongoClient+AreSessionsSupportedServerSelector,
LatencyLimitingServerSelector{ AllowedLatencyRange = 00:00:00.0150000
} }. Client view of cluster state is { ClusterId : "1", ConnectionMode
: "ReplicaSet", Type : "ReplicaSet", State : "Disconnected", Servers :
[{ ServerId: "{ ClusterId : 1, EndPoint :
"Unspecified/XXXX.documents.azure.com:10255" }", EndPoint:
"Unspecified/XXXX.documents.azure.com:10255", State: "Disconnected",
Type: "Unknown", HeartbeatException:
"MongoDB.Driver.MongoConnectionException: An exception occurred while
opening a connection to the server. --->
MongoDB.Driver.MongoAuthenticationException: Unable to authenticate
using sasl protocol mechanism SCRAM-SHA-1. --->
MongoDB.Driver.MongoCommandException: Command saslContinue failed: Not
Authenticated.
To troubleshot I tried from MongoDb Compass as well and that didn't work either, showing me it wasn't the code.