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.
Related
I am trying to connect to my GCP projects PostgreSQL CloudSQL instance from my local machine. The PostgreSQL doesn't have a public IP, only private.
Properties connProps = new Properties();
connProps.setProperty("user", "XXX-compute#developer.gserviceaccount.com");
connProps.setProperty("password", "password");
connProps.setProperty("sslmode", "disable");
connProps.setProperty("socketFactory", "com.google.cloud.sql.postgres.SocketFactory");
connProps.setProperty("cloudSqlInstance", "coral-XXX-XXXX:us-central1:mdm");
connProps.setProperty("enableIamAuth", "true");
HikariConfig config = new HikariConfig();
config.setJdbcUrl(jdbcURL);
config.setDataSourceProperties(connProps);
config.setConnectionTimeout(10000); // 10s
HikariDataSource connectionPool = new HikariDataSource(config);
I get the below error
Failed to get driver instance for jdbcUrl=jdbc:postgresql:///mdm
java.sql.SQLException: No suitable driver
I have verified that my username, instancename, IAM connectivity is all working fine. The IAM service account I am using is my compute engine's default service account.
Should I be able to connect to this PostgreSQL instance from my local machine?
First, make sure you're configuring your JDBC URL correctly.
The URL should look like this:
jdbc:postgresql:///<DATABASE_NAME>?cloudSqlInstance=<INSTANCE_CONNECTION_NAME>&socketFactory=com.google.cloud.sql.postgres.SocketFactory&user=<POSTGRESQL_USER_NAME>&password=<POSTGRESQL_USER_PASSWORD>
See the docs for details.
Second, if your Cloud SQL instance is Private IP only, your local machine won't have a network path to it, unless you've explicitly configured one (see this answer for options).
Generally, the simplest way to connect to a private IP instance is to run a VM in the same VPC as the instance, and connect from that VM.
While it is a good practice from the security point to have only the private IP enabled and remove the public IP from the Cloud SQL instance, there are some considerations to be kept in mind when thinking about the connectivity.
With the Cloud SQL instance having only the private IP enabled there is no direct way in which you can connect to it from the local machine, neither by using private IP nor by using Cloud SQL proxy.
Now, in your case, as you mentioned you have only private IP enabled in the Cloud SQL instance, it seems to be the reason you are getting the error.
To mitigate the error -
If possible, I would suggest you provision a public IP address
for the Cloud SQL instance and then try to connect it by correctly
specifying the jdbc URL as mentioned here, which looks something like this -
jdbc:postgresql:///<DATABASE_NAME>?cloudSqlInstance=<INSTANCE_CONNECTION_NAME>&socketFactory=com.google.cloud.sql.postgres.SocketFactory&user=<POSTGRESQL_USER_NAME>&password=<POSTGRESQL_USER_PASSWORD>
Else, if you don’t want to provision a public IP address, to
establish the connection from an external resource you can make use
of a Cloud VPN Tunnel or a VLAN attachment if you have a Dedicated
Interconnect or a Partner Interconnect as mentioned here.
If you don’t have a Dedicated Interconnect or a Partner Interconnect and you want to use the private IP only then, to connect to the Cloud SQL you can enable port forwarding via a Compute Engine VM instance. This is done in two steps -
Connect the Compute Engine to the Cloud SQL instance via the private IP.
Forward the local machine database connection request to the Compute Engine to reach the Cloud SQL instance through Cloud SQL Proxy tunnel. This youtube video describes how to do this.
To get a detailed description of the above you can go through this article.
I don't know why this is happening error
I'm trying to simply connect my database to jdbc
In the url jdbc:mysel://localhost:test you set the host as localhost and the port as test
There is no such port called test, you need to provide a port number.
Most likely you are using the default port for mysql, so you might need to provide one at all.
I have a working Spring Boot app (1.2) that uses Postgres. Today I am trying to switch it to Oracle, but when I try to connect I get an exception that says:
java.sql.SQLRecoverableException: IO Error: The Network Adapter could not establish the connection
And below that,
Caused by: java.net.ConnectException: Connection refused
So of course that looks like bad credentials, but I know they are good, and they are working in Oracle SQL Developer just fine. I'm baffled. Here are my properties file entries:
# Properties for Hibernate and Oracle
spring.datasource.driverClassName=oracle.jdbc.driver.OracleDriver
spring.datasource.url=jdbc:oracle:thin:#//earth-db-11:5121/stardev
spring.datasource.username=ops$abcdefg
spring.datasource.password=mypassword
spring.jpa.database-platform=org.hibernate.dialect.Oracle10gDialect
The only idea I have is that there is a $ in the user name, and I have tried escaping it and putting double quotes around it.
Any ideas?
Thanks...
UPDATE:
Many thanks to BonanzaOne, I did have the port number wrong. Correcting that results in a new error:
java.sql.SQLRecoverableException: Listener refused the connection with the following error:
ORA-12514, TNS:listener does not currently know of service requested in connect descriptor
I looked it up of course, but I don't follow what its telling me:
ORA-12514: TNS:listener does not currently know of service requested in connect descriptor
Cause: The listener received a request to establish a connection to a database
or other service. The connect descriptor received by the listener specified a
service name for a service (usually a database service) that either has not yet
dynamically registered with the listener or has not been statically configured
for the listener. This may be a temporary condition such as after the listener
has started, but before the database instance has registered with the listener.
Still, SQL Explorer connects fine.
That exception means that the Oracle listener is not up, or you are trying to connect to a listener that don't exist/not accessible.
My guess is that you trying the wrong port "5121". Oracle default port is 1521.
Can you try with that and see what happens?
From the FAQ there are basically two ways of composing your JDBC string URL:
Old syntax
jdbc:oracle:thin:#[HOST][:PORT]:SID
New syntax
jdbc:oracle:thin:#//[HOST][:PORT]/SERVICE
My guess is that you are using the wrong syntax-SID/Service name combination, in other words, you are using the new syntax that requires the SERVICE name, but you are using the SID name to do it.
Try this: jdbc:oracle:thin:#earth-db-11:1521:stardev
Or maybe find out the Service name and apply it to the new syntax that you are using, instead of the SID name.
When I print the I.P. address of the system using InetAddress.getLocalHost(), I get user-VAIO/192.168.1.3 . Now, when I connect to derby using jdbc:derby://localhost:1527/mydatabase;create=true, it connects without any errors but when I connect the same using jdbc:derby://192.168.1.3:1527/mydatabase;create=true, it fails giving me the following exception:-
java.net.ConnectException : Error connecting to server 192.168.1.3 on port 1527 with message Connection refused: connect.
Any help will be appreciated.
When you start your Derby Network Server, you provide a value for the '-h' argument. You might not realize you are doing this, if you are using the packaged StartNetworkServer.bat file, but look inside the batch file, and you will see the -h argument there.
The batch file comes provided with the syntax '-h default' when you download Derby from the Apache website.
But you can change that, to say, for example, '-h 192.168.1.3', and then your Derby Network Server will accept connections that specify 'jdbc:derby://192.168.1.3/my/database'.
Note that if you want to accept such connections from other computers on the network, you will also have to adjust your Windows Firewall rules, as by default it will prevent such connections.
I am trying to connect to a db2 database in Java. Below the driver and the connection string and the driver details i am giving
Class.forName("COM.ibm.db2.jdbc.net.DB2Driver");
String url="jdbc:db2://hostname:portnumber/databasename";
sourceConnection=DriverManager.getConnection(url,"username","password");
But I am getting the below exception
"COM.ibm.db2.jdbc.DB2Exception: [IBM][JDBC Driver] CLI0615E Error receiving from socket, server is not responding. SQLSTATE=08S01"
I also tried changing the connection string to
String url="jdbc:db2:hostname:portnumber/databasename";
Still it is resulting the same exception above while trying to get the Connection.
And i have tried the below option also using JDBC app driver
Class.forName("COM.ibm.db2.jdbc.app.DB2Driver");
DB2DataSource db2ds = new DB2DataSource();
db2ds.setServerName("hostname");
db2ds.setPortNumber(portnumber);
db2ds.setDatabaseName("databasename");
db2ds.setUser("username");
db2ds.setPassword("password");
sourceConnection=db2ds.getConnection();
For the above two connection I used the jar "db2java.jar"
And i have tried using the JCC driver:
Class.forName("com.ibm.db2.jcc.DB2Driver");
String url="jdbc:db2://hostname:portnumber/databasename";
sourceConnection=DriverManager.getConnection(url,"username","password");
For this connection i have added the below jars
1)db2jcc.jar
2)db2jcc_license_cu.jar
This time around I am getting the below error,
"com.ibm.db2.jcc.am.go: [jcc][t4][201][11237][3.57.82] Connection authorization failure occurred.
Reason: Security mechanism not supported. ERRORCODE=-4214, SQLSTATE=28000"
I tried to connect to the same db2 source using "Quest for DB2" tool and the connection was successful.
Am i missing something in the code and is it a problem with DB2 drivers or connection string?
Can someone please guide me.
Thanks in advance.
Cause:
If the DB2® instance where InfoSphere Optim Performance Manager is running has the authentication configuration parameter set to DATA_ENCRYPT, you cannot log in to the web console.
Resolving the problem:
Do the following steps:
On the DB2 instance where Optim Performance Manager is running, set the authentication configuration parameter to SERVER by issuing the following command:
db2 update dbm cfg using authentication server
Restart the DB2 instance and InfoSphere Optim Performance Manager.
For more details visit here.
Your first two attempts were not supposed to work. You're using the JCC driver URL format, so it wouldn't be valid for either "net" or "app" drivers, which are deprecated anyway.
Use the JCC driver (com.ibm.db2.jcc.DB2Driver) and the URL format of "jdbc:db2://hostname:portnumber/databasename" and see this technote for the solution to the "Security mechanism not supported" problem. In short, you need to use a supported JDK.