How can I connect to MongoDB server using JAVA from OpenShift? - java

I have created a MongoDB instance in OpenShift. I can connect to it via RockMongo, which is a service offered by OpenShift.
I'm trying to connect to my instance using JAVA, but I just receive a Connection refuesed error. Moreover, I cannot connect it using RoboMongo.
In my RockMongo status tab, I see the following information:
Host: 127.11.201.2
Port: 27017
Using RoboMongo with MongoLab instance works just fine giving it the right credentials, but here with OpenShift it fails on connecting to the instance.
In my JAVA app I'm trying the following:
MongoCredential credential = MongoCredential.createCredential(
Const.MONGO_USERNAME, Cont.MONGO_DB,
Const.MONGO_PASSWORD.toCharArray());
mongo = new MongoClient(new ServerAddress(Const.MONGO_URI), Arrays.asList(credential));
With 127.11.201.2 as MONGO_URI.
Why am I failing to connect to my instance? What am I doing wrong?
P.S using putty I am able to connect to my mongo instance by just executing the command mongo.

OpenShift provides environment variables, which you should use to connect to your MongoDB.
OPENSHIFT_MONGODB_DB_HOST The MongoDB IP address
OPENSHIFT_MONGODB_DB_PORT The MongoDB port
OPENSHIFT_MONGODB_DB_USERNAME The MongoDB username
OPENSHIFT_MONGODB_DB_PASSWORD The MongoDB password
OPENSHIFT_MONGODB_DB_URL The MongoDB connection URL (e.g. mongodb://<username>:<password>#<hostname>:<port>/)
I'm using one line of code to connect to the database:
new MongoClient(new MongoClientURI(System.getenv("OPENSHIFT_MONGODB_DB_URL")));

Related

com.mongodb.MongoSocketException: No such host is known

I am trying to connect to MongoDB Atlas DB from my spring boot app. I set the below listed properties in my application properties file. When I try to connect I see this exception "com.mongodb.MongoSocketException: No such host is known". However I am able to connect to this using mongodb compass from the same machine. Am I missing something?
spring.data.mongodb.authentication-database=
spring.data.mongodb.host=
spring.data.mongodb.port=
spring.data.mongodb.username=
spring.data.mongodb.password=
spring.data.mongodb.database=
I am also able to connect when I use the below -
spring.data.mongodb.uri=
Take a look at this: https://developer.mongodb.com/article/srv-connection-strings/
Atlas is giving you a connection string with the mongodb+srv syntax where the "host" part is actually a DNS SRV record that gives you a replica set, not a host. This is compatible with the spring.data.mongodb.uri parameter and of course with MongoDb compass.
However setting the spring.data.mongodb.host instead gives you the unknown host exception.

Postgresql Java jdbc connect via SSL tunnel to a DB running in Virtualbox using .pem file

Hi I am trying to connect to a local Postgres Db running in Virtualbox Centos, which connects through SSL tunnel. Example of how I connect via DBeaver is below.
DBeaver I am using a SSL tunnel tab and
I am using a pem file, and user and password
Postgres local connection
The Virtualbox is set to forward port as follows
Postgres Virtualbox port forward
My Java code without ssl tunnel is this
DriverManager.getConnection("jdbc:postgresql://localhost:5432/dbName?user=postgresuser&password=givepassword");
This is failing saying pg_hba.conf not found etc or if I give ssl=true in connection string , says its not supported.. How do I connect to Local DB using ssl tunnel?
[ My assumption in that pg_hba.conf file would be available on the Virtualbox Host Vm only.]
You may try using jsch which is an implementation of SSH in Java.
Here is a complete example of how to do it https://cryptofreek.org/2012/06/06/howto-jdbc-over-an-ssh-tunnel/
Note that in this case all your traffic will go to the proxy hence you need to close the session after reading/writting to the database.

Connect to Openshift Mongodb remotely with Java, timeout error

I have turn on the port-forward with rhc, it shows
mongodb 127.0.0.1:27017 => xxx.x.xxx.x:27017
doc here port-forward
but I still have no luck connect to that mongodb cartridge. I've try both mongo shell 3.0 and java MongoClient.
mongo shell return error 10061
java return com.mongodb.MongoTimeoutException: Timed out after 10000 ms while waiting for a server that matches AnyServerSelector{}
what else could I try to connect to openshift mongodb remotely?
my code, I've test this on local mongodb which works fine
String mongoUri = "mongodb://admin:password#xxx.x.xxx.x:27017/";
MongoClient mongoClient;
try {
mongoClient = new MongoClient(new MongoClientURI(mongoUri));
DB db = mongoClient.getDB("mycoll");
DBCollection cc = db.getCollection("DBObject");
cc.insert(dbo);
}
You should be using the following connection string locally while you have port forwarding enabled:
String mongoUri = "mongodb://admin:password#127.0.0.1:27017/";
Since the connection is being forwarded over an ssh tunnel, you need to use the local port. See this answer for more information: OpenShift: How to connect to postgresql from my PC

How to add OpenShift MongoDB environment variables to my Java Application [duplicate]

I have created a MongoDB instance in OpenShift. I can connect to it via RockMongo, which is a service offered by OpenShift.
I'm trying to connect to my instance using JAVA, but I just receive a Connection refuesed error. Moreover, I cannot connect it using RoboMongo.
In my RockMongo status tab, I see the following information:
Host: 127.11.201.2
Port: 27017
Using RoboMongo with MongoLab instance works just fine giving it the right credentials, but here with OpenShift it fails on connecting to the instance.
In my JAVA app I'm trying the following:
MongoCredential credential = MongoCredential.createCredential(
Const.MONGO_USERNAME, Cont.MONGO_DB,
Const.MONGO_PASSWORD.toCharArray());
mongo = new MongoClient(new ServerAddress(Const.MONGO_URI), Arrays.asList(credential));
With 127.11.201.2 as MONGO_URI.
Why am I failing to connect to my instance? What am I doing wrong?
P.S using putty I am able to connect to my mongo instance by just executing the command mongo.
OpenShift provides environment variables, which you should use to connect to your MongoDB.
OPENSHIFT_MONGODB_DB_HOST The MongoDB IP address
OPENSHIFT_MONGODB_DB_PORT The MongoDB port
OPENSHIFT_MONGODB_DB_USERNAME The MongoDB username
OPENSHIFT_MONGODB_DB_PASSWORD The MongoDB password
OPENSHIFT_MONGODB_DB_URL The MongoDB connection URL (e.g. mongodb://<username>:<password>#<hostname>:<port>/)
I'm using one line of code to connect to the database:
new MongoClient(new MongoClientURI(System.getenv("OPENSHIFT_MONGODB_DB_URL")));

Java with MongoDb IP connecting issue

I write a Java program to connect MongoDb and write some data into it. For example I set up the MongoDb on 192.168.1.95 and run my program on that server. When I use the following code:
MongoClient mongo = new MongoClient("localhost", 27017);
It is working and no any problem. However, if I use
MongoClient mongo = new MongoClient("192.168.1.95", 27017);
It always throw a exception for me:
Exception in thread "main" com.mongodb.MongoException: unauthorized
at com.mongodb.CommandResult.getException(CommandResult.java:100)
at com.mongodb.CommandResult.throwOnError(CommandResult.java:134)
at com.mongodb.DBTCPConnector._checkWriteError(DBTCPConnector.java:142)
at com.mongodb.DBTCPConnector.say(DBTCPConnector.java:183)
at com.mongodb.DBTCPConnector.say(DBTCPConnector.java:155)
at com.mongodb.DBApiLayer$MyCollection.insert(DBApiLayer.java:270)
at com.mongodb.DBApiLayer$MyCollection.insert(DBApiLayer.java:226)
at com.mongodb.DBCollection.insert(DBCollection.java:75)
at com.mongodb.DBCollection.insert(DBCollection.java:59)
at com.mongodb.DBCollection.insert(DBCollection.java:104)
at com.starscriber.mongoCluster.Main.main(Main.java:29)
Why cannot I use the exactly Ip instead of "localhost"?? And I am pretty sure that I don't need any username and password to log into the MongoDb
Sounds like auth=true in your MongoDB server configuration. If so, you need to log in to your server by connecting over the localhost interface for the first time to create user credentials. For more info see section "Security Considerations" in the Run-time Database Configuration page of the MongoDB manual

Categories

Resources