How to add OpenShift MongoDB environment variables to my Java Application [duplicate] - 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.

I can connect to AWS MySQL RDS instance from workbench but not in my JDBC application

I am trying to create a Java application that connects to a MySQL database and I am using AWS to host it. So I created the AWS RDS instance and I got it to connect to the MySQL workbench just fine. My problem arises when I am trying to use JDBC to connect to it.
I have security groups that allow traffic from anywhere, but I also tried just allowing my IP.
I had it working when I was using a localhost but now I'm trying to move it to a server so keeping it localhost isn't an option (used localhost to test the application and such)
I made sure that my user was a remote user by doing
SELECT * from mysql.user;
And made sure that the host was a '%' and that it had all the privileges
So my code I'm trying to connect with in Java is
String connectionURL = "jdbc:mysql:/{hostname}:{port}/{database name}/?autoReconnect=true&useSSL=false";
String username = "username";
String password = "password";
con = DriverManager.getConnection(connectionURL, username, password);
Of course I have the actual host, port, and such in there just changed it for posting online.
When I try running it on Eclipse it says this java.lang.Exception: Database not found
I looked up some tutorials on AWS docs to make sure it lined up, which it all did.
Anyone have any idea why it might be connecting to MySQL workbench and not the JDBC?

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 can I connect to MongoDB server using JAVA from OpenShift?

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