Connecting Application to OpenShift MongoDB - java

I had a MongoDB on MongoLab, which I was using with my Android Application and everything was just fine.
But well, they decided to upgrade the MongoDB version to 3.0.x and now I’m not able to connect again because the java driver 3.0 don’t seem to work very well on Android. We can see some discussion about the situation here: mongodb 3.x driver Android compatibility
Then I decided to try OpenShift, which has version 2.4 of MongoDB.
But I’m not being able to connect to the database like I use to do with MongoLab. I feel like I might be misunderstanding the purpose of OpenShift.
Caused by: java.net.ConnectException: Connection refused: connect
[...]
Exception in thread "main" 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=127.XX.XXX.X:27017, type=UNKNOWN, state=CONNECTING, exception={com.mongodb.MongoSocketOpenException: Exception opening socket}, caused by {java.net.ConnectException: Connection refused: connect}}]
Does anyone have any experience with OpenShift that could give me some advice?
If OpenShift is not an option, could anyone give some suggestion about a free MongoDB cloud service that is not 3.0.X+?
Just would like to remind that this is for college study that’s why we are not investing money on it.
Thanks in advance.

Related

Connecting Azure app service to MongoDB Atlas across vnet peering connection

So I have an Azure App Service and I want it to be able to connect to mongo db atlas. For our Atlas setup there is a peering connection between our managed environment on azure (and the vnet it all sits in) and our mongo db cluster.
The mongo cluster is setup with "Connect via peering only" which we can't change. This means we can't connect to our db with any old public ip address -It has to come from the vnet it's peered with. The vms (from within the peered vnet) are able to access the db no problem.
I tried with the app service to connect to the db with the same connection string that worked for the vm. It returned a the error:
java.net.ConnectException: Connection refused (Connection refused)}}, {address=MYMONGOADDRESS.mongodb.net:27017, type=UNKNOWN, state=CONNECTING, exception={com.mongodb.MongoSocketOpenException: Exception opening socket}, caused by {java.net.ConnectException: Connection refused (Connection refused)}}]
This is expected due to the reasons above. However I setup the vnet integration for the app service
. I created a dedicated subnet as described for the app service and whitelisted the ip range in mongo. However I still couldn't connect to the db after using this vnet integration. I also got a very similar error but slightly different.
{java.net.SocketTimeoutException: connect timed out}}, {address=MYMONGOADDRESS.mongodb.net:27017, type=UNKNOWN, state=CONNECTING, exception={com.mongodb.MongoSocketOpenException: Exception opening socket}, caused by {java.net.SocketTimeoutException: connect timed out}}]
Where instead of a connection refused I'm not getting a connection time out. I've repeated this a few times and get the same result.
Anyone know why these errors are different and any help in being able to connect to the db from my azure app service?
Is there any firewall or network security group? What the network peering status is? If it shows « initiated » then this means it has not been completed. Network peering has to be done on each ends. If it shows « connected », then I would look for firewall or network security groups.
See this
https://docs.atlas.mongodb.com/security-vpc-peering/#vpc-peering
No idea if this can apply to your case but this question was the first and only that came up when I was searching for something similar.
In my case I have a MongoDB Atlas deployed on Azure and an app running in AKS. I set up peering as documented but still could not connect from the app container in AKS to the MongoDB. Turns out the problem was an incorrect connection string.
Atlas has a different URL when connecting directly and when connecting through a peer network:
# Direct connection
mongo "mongodb+srv://<cluster>.<something>.mongodb.net/<dbname>"
# Peered connection (note the -pri)
mongo "mongodb+srv://<cluster>-pri.<something>.mongodb.net/<dbname>"
As an additional note, I configured the Atlas Network Acces IP address list with the CIDR of the Azure VN Subnet used in AKS. (Under Virtual Network > Subnets > IPv4)

Trouble connecting to Mongo Atlas Free tier using the latest mongo client [duplicate]

Trying to connect Atlas cluster via Java driver using MongoDB version 3.6.
So, I'm writting like:
MongoClientURI uri = new MongoClientURI("mongodb+srv://admin:mypassword#cluster0-ox90k.mongodb.net/test?retryWrites=true");
MongoClient mongoClient = new MongoClient(uri);
In this case the error is:
java.lang.IllegalArgumentException: The connection string is invalid. Connection strings must start with 'mongodb://'
at com.mongodb.ConnectionString.<init>(ConnectionString.java:203)
at com.mongodb.MongoClientURI.<init>(MongoClientURI.java:176)
at com.mongodb.MongoClientURI.<init>(MongoClientURI.java:158)
at project.Bot.check(Bot.java:30)
at project.Bot.onUpdateReceived(Bot.java:104)
at java.util.ArrayList.forEach(ArrayList.java:1249)
at org.telegram.telegrambots.generics.LongPollingBot.onUpdatesReceived(LongPollingBot.java:27)
at org.telegram.telegrambots.updatesreceivers.DefaultBotSession$HandlerThread.run(DefaultBotSession.java:309)
When the program starts with snippet using MongoDB version 3.6 or later without +srv:
MongoClientURI uri = new MongoClientURI("mongodb://admin1:mypassword#cluster0-ox90k.mongodb.net/test?retryWrites=true");
MongoClient mongoClient = new MongoClient(uri);
I'm getting an 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=UNKNOWN, servers=[{address=cluster0.mongodb.net:27017, type=UNKNOWN, state=CONNECTING, exception={com.mongodb.MongoSocketException: cluster0.mongodb.net}, caused by {java.net.UnknownHostException: cluster0.mongodb.net}}]
at com.mongodb.connection.BaseCluster.createTimeoutException(BaseCluster.java:369)
at com.mongodb.connection.BaseCluster.selectServer(BaseCluster.java:101)
at com.mongodb.binding.ClusterBinding$ClusterBindingConnectionSource.<init>(ClusterBinding.java:75)
at com.mongodb.binding.ClusterBinding$ClusterBindingConnectionSource.<init>(ClusterBinding.java:71)
at com.mongodb.binding.ClusterBinding.getReadConnectionSource(ClusterBinding.java:63)
at com.mongodb.operation.OperationHelper.withConnection(OperationHelper.java:201)
at com.mongodb.operation.CountOperation.execute(CountOperation.java:206)
at com.mongodb.operation.CountOperation.execute(CountOperation.java:53)
at com.mongodb.Mongo.execute(Mongo.java:772)
at com.mongodb.Mongo$2.execute(Mongo.java:759)
at com.mongodb.MongoCollectionImpl.count(MongoCollectionImpl.java:185)
at com.mongodb.MongoCollectionImpl.count(MongoCollectionImpl.java:170)
at project.Bot.check(Bot.java:36)
at project.Bot.onUpdateReceived(Bot.java:103)
at java.util.ArrayList.forEach(ArrayList.java:1249)
at org.telegram.telegrambots.generics.LongPollingBot.onUpdatesReceived(LongPollingBot.java:27)
at org.telegram.telegrambots.updatesreceivers.DefaultBotSession$HandlerThread.run(DefaultBotSession.java:309)
In POM file I have dependency:
<dependency>
<groupId>org.mongodb</groupId>
<artifactId>mongo-java-driver</artifactId>
<version>3.6.0</version>
</dependency>
Also, when I'm starting mongo my database is added to this address mongodb://127.0.0.1:27017, but I added path to the cluster not for this. Maybe I need to write path to concrete cluster or?
Ofc, I have admin-user. In addition, I can connect via Compass to my cluster and from shell. mongod process is started. This error appears only, when I'm running in IDE. Same issue probably here.
Does anyone know how to solve this error? I appreciate any help.
Solved it!
So, what I've done:
I tried only to connect to tier cluster via driver3.6 and wrote
mongodb+srv://user:#cluster0-ox90k.mongodb.net/test?retryWrites=true
I always get an error: Connection strings must start with 'mongodb://'.
Okay, I deleted the snippet +srv and wrote the same way
mongodb://user:#cluster0-ox90k.mongodb.net/test?retryWrites=true
and get again the 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=UNKNOWN, servers=[{address=cluster0-ox90k.mongodb.net:27017, type=UNKNOWN, state=CONNECTING, exception={com.mongodb.MongoSocketException: cluster0-ox90k.mongodb.net}, caused by {java.net.UnknownHostException: cluster0-ox90k.mongodb.net}}]
So, I wrote via driver3.4 or earlier like
mongodb://user:<PASSWORD>#cluster0-shard-00-00-ox90k.mongodb.net:27017,cluster0-shard-00-01-ox90k.mongodb.net:27017,cluster0-shard-00-02-ox90k.mongodb.net:27017/test?ssl=true&replicaSet=Cluster0-shard-0&authSource=admin&retryWrites=true
and finally it solved.
Note: You can get this connection string from the Atlas management console by setting your Java driver to '3.4 or later'. This will help you avoid having to come up with the connection string yourself.
Updated: if you want to use drivers 3.7+, you need to write instead of format connection (and to avoid my issues above)
MongoClientURI uri = new MongoClientURI("mongodb+srv://admin:mypassword#cluster0-ox90k.mongodb.net/test?retryWrites=true");
MongoClient mongoClient = new MongoClient(uri);
another variant using MongoClients.create() (as of the 3.7 release), and as mentioned here:
MongoClient mongoClient = MongoClients.create("mongodb+srv://admin:mypassword#cluster0-ox90k.mongodb.net/test?retryWrites=true");
Note: the password need to write not like mongodb://user:<mypassword>#...,
just in format mongodb://user:mypassword#...
without braces <>.
There seem to be a few issues here
First
3.6.0 is not the Mongo driver library that was actually loaded into your application classpath; I suspect that you were previously testing with an old version, and recently updated the POM? You were previously using version 3.2.0.
How do I know this?
I started digging through the code, and at version 3.6.0, the error message you provided is nowhere near line 203. And also, you can see that the above linked code has support for the +srv.
Browing back through previousl releases, I finally found that error massge on line 203, back at release 3.2.0.
Long story short, trying doing a Maven clean, and rebuild.
Relaunch Eclipse to pick up new dependencies if a project refresh does not help.
Second
MongoTimeoutException: Timed out after 30000 ms while waiting for a server
This one is highly likely a firewall / access control group configuration issue, in that the firewall is blocking the packets from reaching your Atlas cluster.
See adding addresses to the whitelist.
One more important note:
in this string:
MongoClientURI uri = new MongoClientURI("mongodb+srv://admin:mypassword#cluster0-ox90k.mongodb.net/test?retryWrites=true");
test ==> is a Db name, before making this connection, DB should exist.
The above-mentioned solution is not working in the current assignment, because I'm working on the Spring-boot framework assignment.
I have got error as Caused by: java.net.UnknownHostException: khweshacluster0.brzta.mongodb.net
To Solve this I have to take configurations from below
Take Highlighted URL
& make below format URL as
spring.data.mongodb.uri=mongodb://:#<Atlas_cluster_URL>
& same update in application.properties
as follows,
spring.data.mongodb.uri=mongodb://username:password#khweshacluster0-shard-00-00.brzta.mongodb.net:27017,khweshacluster0-shard-00-01.brzta.mongodb.net:27017,khweshacluster0-shard-00-02.brzta.mongodb.net:27017/myFirstDatabase?ssl=true&replicaSet=atlas-3b1nqz-shard-0&authSource=admin&retryWrites=true&w=majority
I faced same issue when trying to connect from my tomcat application to MongoDB.
I had tomcat 9.0 and MongoDB 4.2 installed with application using Mongo Driver 3.12.3
Error: org.springframework.dao.DataAccessResourceFailureException: Timed out after
30000 ms while waiting to connect. Client view of cluster state is {type=UNKNOWN,
servers=[{address=#127.0.0.1:27017, type=UNKNOWN, state=CONNECTING, exception=
{com.mongodb.MongoSocketException: #127.0.0.1}, caused by
{java.net.UnknownHostException: #127.0.0.1}}]; nested exception is
com.mongodb.MongoTimeoutException: Timed out after 30000 ms while waiting to connect.
Client view of cluster state is {type=UNKNOWN, servers=[{address=#127.0.0.1:27017,
type=UNKNOWN, state=CONNECTING, exception={com.mongodb.MongoSocketException:
#127.0.0.1}, caused by {java.net.UnknownHostException: #127.0.0.1}}]
I tried creating sample Java application to connect DB and it worked, however it was not able to connect from web application.
So created user for DB and assigned role userAdmin, which worked for me.
Not working Conn String - mongodb://#127.0.0.1:27017/docs <br>
Working Conn string - mongodb://docs_local:docs#127.0.0.1:27017/docs
i had this issue and i checked the documentation
i created a configuration class like this
import com.mongodb.client.MongoClient;
import com.mongodb.client.MongoClients;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
#Configuration
public class AppConfig {
public #Bean
MongoClient mongoClient() {
return MongoClients.create("mongodb://localhost:27017");
}
}
you can put your uri where "mongodb://localhost:27017" is
https://docs.spring.io/spring-data/mongodb/docs/current/reference/html/#reference
I had the similar issue and solved it by selecting Node driver version 2.2.12 or later and it will give me a connection string starting with 'mongodb://'

Cassandra Refusing Connection

I'm using VPS with CentOS 6.6.
For two days I'm trying to start Cassandra that I've already installed.
Its CLI is just not working and giving me error. I have tried all online solutions and none worked.
root#maw [/etc/cassandra/conf]# cassandra-cli -h localhost
org.apache.thrift.transport.TTransportException: java.net.ConnectException: Connection refused
at org.apache.thrift.transport.TSocket.open(TSocket.java:185)
at org.apache.thrift.transport.TFramedTransport.open(TFramedTransport.java:81)
at org.apache.cassandra.thrift.TFramedTransportFactory.openTransport(TFramedTransportFactory.java:41)
at org.apache.cassandra.cli.CliMain.connect(CliMain.java:65)
at org.apache.cassandra.cli.CliMain.main(CliMain.java:237)
Caused by: java.net.ConnectException: Connection refused
at java.net.PlainSocketImpl.socketConnect(Native Method)
at java.net.AbstractPlainSocketImpl.doConnect(AbstractPlainSocketImpl.java:339)
at java.net.AbstractPlainSocketImpl.connectToAddress(AbstractPlainSocketImpl.java:200)
at java.net.AbstractPlainSocketImpl.connect(AbstractPlainSocketImpl.java:182)
at java.net.SocksSocketImpl.connect(SocksSocketImpl.java:392)
at java.net.Socket.connect(Socket.java:579)
at org.apache.thrift.transport.TSocket.open(TSocket.java:180)
... 4 more
Exception connecting to localhost/9160. Reason: Connection refused.
Welcome to Cassandra CLI version 2.0.14
The CLI is deprecated and will be removed in Cassandra 3.0. Consider migrating to cqlsh.
CQL is fully backwards compatible with Thrift data; see http://www.datastax.com/dev/blog/thrift-to-cql3
Type 'help;' or '?' for help.
Type 'quit;' or 'exit;' to quit.
[default#unknown]
I have also tried cqlsh and it gives similar error:
root#maw [/etc/cassandra/conf]# cqlsh
Connection error: Could not connect to localhost:9160
Please help.
The problem was Java.
It needs Oracle Java. I was using Open Java.
After changing Java to Oracle Java, it worked.
There can be many problems with cassandra not allowing the connection. Just mentioning the check-points which you can follow to solve the issue
1) Basic check if cassandra is running - service cassandra status
2) If you are using cassandra 2.0 or later then make sure it is running on the oracle java7 not java6
3) Check you rpc_address and listen_address settings in cassandra.yaml
4) Make sure firewall is not blocking any of the ports required by cassandra.

java.sql.SQLException: Invalid state, the Connection object is closed

I have a java web application that is running on jetty that connects to SQL server local database (I'm using hibernate). Both the application and the database are in the SAME PC. Everything runs smoothly until I disable the network adapter or unplug the Ethernet wire, at this moment I cannot access my web app and getting this (among other nested exceptions) exception:
Caused by: java.sql.SQLException: Invalid state, the Connection object is closed.
at net.sourceforge.jtds.jdbc.ConnectionJDBC2.checkOpen(ConnectionJDBC2.java:1713)
at net.sourceforge.jtds.jdbc.ConnectionJDBC2.setAutoCommit(ConnectionJDBC2.java:2223)
at org.jboss.resource.adapter.jdbc.BaseWrapperManagedConnection.checkTransaction (BaseWrapperManagedConnection.java:429)
at org.jboss.resource.adapter.jdbc.WrappedConnection.checkTransaction (WrappedConnection.java:525)
at org.jboss.resource.adapter.jdbc.WrappedConnection.commit(WrappedConnection.java:333)
at org.hibernate.transaction.JDBCTransaction.commitAndResetAutoCommit (JDBCTransaction.java:170)
at org.hibernate.transaction.JDBCTransaction.commit(JDBCTransaction.java:146)
... 51 more
Any thoughts?
P.S: If you need further info please tell me.

Mysql communication error in tomcat even with out using any mysql in application

I am Getting following error when ever i am starting my tomcat
com.mysql.jdbc.CommunicationsException: Communications link failure due to underlying exception:
** BEGIN NESTED EXCEPTION **
java.net.ConnectException
MESSAGE: Connection refused
I am getting this error even with out using any application in my tomcat.
Could you please help me why tomcat is looking for mysql connect at the time of start
Regards
May be you have configured a Tomcat Datasource Resource in your server.xml or context.xml in your ${CATALINA_BASE}/conf directory. So, whenever tomcat starts it tries to create the connection pool for your datasource and it is not able to communicate to the db server host. This is highly possible if you don't use mysql in your application

Categories