I am trying to connect to cloud sql from app engine using cloud back-end java servlet in android studio. But I got error in getting connection from DriverManager.
Connection conn = DriverManager.getConnection("jdbc:google:mysql://<id:<sql-id>/database",
"myuser","password");
and driver is
Class.forName("com.mysql.jdbc.GoogleDriver");
Please assist me if fixing this error...
The com.mysql.jdbc.GoogleDriver driver and jdbc:google:mysql connection string only work from Google App Engine. When connecting from your development workstation you should use the plain MySQL java connector (ie com.mysql.jdbc.Driver and jdbc:mysql). You will need to authorize you workstation's IP address and add an IP address to your Cloud SQL instance to connect to.
For more information follow the guide at https://cloud.google.com/sql/docs/external.
Actually i got it,problem is with some_user database permission for using database in appengine(Localhost for cloud sql database).
FIX:
Type code in cloud sql database and execute
grant all privileges on databasename.* to 'user'#'localhost' identified by 'password';
This should help.........
Related
I am trying to connect from my java app running on a Google Cloud Run instance, to a Google Cloud Sql instance that is part of the same Google Cloud project.
When my app tries to open a connection to the db, it throws the following error...
org.jdbi.v3.core.ConnectionException: java.sql.SQLException: Cannot create JDBC driver of class '' for connect URL 'jdbc:google:mysql://my-gc-app:my-cloud-sql-instance/mydb'
I have enabled all the correct roles for the cloud run service to connect to the cloud sql instance.
The java code that is establishing the DataSource is as follows...
BasicDataSource dataSource = new BasicDataSource();
dataSource.setUrl("jdbc:google:mysql://my-gc-app:my-cloud-sql-instance/mydb");
dataSource.setUsername(mySqlUser);
dataSource.setPassword(mySqlusersPassword);
dataSource.setMinIdle(5);
dataSource.setMaxIdle(10);
dataSource.setMaxOpenPreparedStatements(100);
Database.instantiate(dataSource);
You're missing a line that specifies which DB type should be used.
For example if you're going to connect to Mysql server.
dataSource.setDriverClassName("com.mysql.jdbc.Driver");
If it leads to a driver not found error or sth like that, try adding the library with the keyword mysql:mysql-connector-java:8.0.12
We have a java project in an Azure virtual machine (VM), and need connect to Azure SQL db by JDBC connection, so we use the JDBC connection string provided by Azure SQL db as follows:
"jdbc:sqlserver://ZZZdbserver.database.windows.net:1433;database=ZZZ;user=*****;password=*****;encrypt=true;trustServerCertificate=false;hostNameInCertificate=*.database.windows.net;loginTimeout=30;"
but we got an exception:
"java.security.cert.CertificateException: Failed to validate the server name in a certificate during Secure Sockets Layer (SSL) initialization.
The server name is *.database.windows.net, the name in certificate is cr2.eastus1-a.control.database.windows.net."
then we updated the JDBC connection string to:
"jdbc:sqlserver://ZZZdbserver.database.windows.net:1433;database=ZZZ;user=*****;password=*****;encrypt=true;trustServerCertificate=false;hostNameInCertificate=cr2.eastus1-a.control.database.windows.net;loginTimeout=30;"
but we got another exception:
org.apache.commons.dbcp.SQLNestedException: Cannot create PoolableConnectionFactory (Cannot open server "ZZZ1" requested by the login. The login failed.)
where "ZZZ1" is our username for the Azure VM.
Any idea to solve this issue?
By default, Azure VM has not blocked the outbound connection to the Internet. You could first verify the network connectivity from your VM to the Azure SQL database via running the command telnet ZZZdbserver.database.windows.net 1433 in CMD or Test-NetConnection -computer ZZZdbserver.database.windows.net -port 1433 in Powershell. If it fails, you may check if there is any firewall blocking this database connection or port or your application is listening on that port or your SQL database is online.
Additionally, if the network connectivity has succeeded, you could verify if the username or password is correct when you are using to connect to your database. Make sure there is not any typo. Also, the username should have enough privileges to access this database. You can try to access this database via Server admin login in the properties of the SQL database. Moreover, you could refer to this sample code to use Java to connect to access the Azure SQL database.
When using the Microsoft JDBC Driver for SQL Server to connect to an Azure SQL Database. You should note this:
Appending the server name to the userId in the connection string
Prior to the 4.0 version of the Microsoft JDBC Driver for SQL Server,
when connecting to an Azure SQL Database, you were required to append
the server name to the UserId in the connection string. For example,
user#servername. Beginning in version 4.0 of the Microsoft JDBC Driver
for SQL Server, it's no longer necessary to append #servername to the
UserId in the connection string.
Using encryption requires setting hostNameInCertificate
Prior to the 7.2 version of the Microsoft JDBC Driver for SQL Server,
when connecting to an Azure SQL Database, you should specify
hostNameInCertificate if you specify encrypt=true (If the server name
in the connection string is shortName.domainName, set the
hostNameInCertificate property to *.domainName.). This property is
optional as of version 7.2 of the driver.
Hope this helps.
If I need set encrypt=true, and hostNameInCertificate=cr2.eastus1-a.control.database.windows.net. Where do I need get the certificate for cr2.eastus1-a.control.database.windows.net from Azure SQL DB's service?
I have deployed on appengine a basic java web app that is supposed to connect to a postgresql instance on Cloud SQL (works locally).
After deployment It (wrongly?) raise this exception when I browse the page that tries to connect to the DB:
org.postgresql.Driver connect: Unexpected connection error: (Driver.java:271)
java.lang.RuntimeException: The Google Cloud SQL API is not enabled for project [ad*****manager-XXXXXXX]. Please use the Google Developers Console to enable it: https://console.cloud.google.com/apis/api/sqladmin/overview?project=ad*****manager-XXXXXXX
I have checked that the Google Cloud SQL API is enabled as you can see on
that screenshot
I have granted the cloud sql editor rights to my app on the project that hosts the instance (Note that the postgresql instance and the web app live in two separate projects.)
It is a flex environment app
Here is the part of the code that fails:
Class.forName("org.postgresql.Driver");
connection = DriverManager.getConnection(dburl, dbuser, dbpwd);
And the connection url:
jdbc:postgresql://google/name_of_my_database?useSSL=false&socketFactoryArg=ad*****manager-XXXXXXX:europe-west1:XXXX-pgdb-preprod&socketFactory=com.google.cloud.sql.postgres.SocketFactory
Actually I had to enable the SQL API not in the project that hosts the DB as said in the error message but in the project that hosts the application
To do so, go in the API section, choose library and search for SQL API (see screenshot below)
screenshot
I now have an application work with oracle. After my oracle get upgraded to 12c then the weird behavior happen.
I can connect to oracle database when using SID=PRD1. But it's not work when I change it to SID=DEV
The error message is ORA-28040: No matching authentication protocol.
Both SID PRD1,DEV can be connected successfully in Oracle SQL Developer.
But from java, Only SID=PRD1 is work and the SID=DEV is failed.
How can I solve this ?
Regards,
OBJ
PS . Both SID DEV , PRD1 are in the same machine
I have a google app engine connected project with which I am trying to connect to a google cloud sql database.I upload my project to google and via it I try to connect to the db.
my connection URL is as follows->
Class.forName("com.mysql.jdbc.GoogleDriver");
String password="";
url = "jdbc:google:mysql://my-project-id:my-instance-name/dbname?user=root&password="+password+"useUnicode=true&characterEncoding=UTF-8";
Connection conn = DriverManager.getConnection(url);
it is at the point where I attempt to get a connection with the url that I get an exception
com.mysql.jdbc.exceptions.jdbc4.CommunicationsException: Communications link failure The last packet sent successfully to the server was 0 milliseconds ago. The driver has not received any packets from the server.
I then added the following string to my url->
&autoReconnect=true&failOverReadOnly=false&maxReconnects=10
then received a different exception-->
com.mysql.jdbc.exceptions.jdbc4.MySQLNonTransientConnectionException: Could not create connection to database server. Attempted reconnect 10 times. Giving up.
I have tried pinging the ip address of the db instance and it works fine.
Do I need to give any explicit permissions in my cloud sql instance for it to be able to connect?
Any suggestions are welcome.
The strange thing is that I am able to connect to the db using eclipse by going to project->google->app engine->usegoogle cloud sql instance->configure.I use the same user name (root) and password ("") here as in the url in the code but for some reason it refuses to connect through the code.
I have also enabled the
<use-google-connector-j>true</use-google-connector-j>
tag in my
appengine-web.xml
I changed the root password from "" to some value and I am able to connect via mysql client
and I can access the database, but on giving the same password in code it refuses to connect.
Any suggestions will be appreciated.
Thanks,
Laura
Instance Id by default contains the project Id. So don't explicitly add project Id again.
Project ID: sampleapp
Instance Id: sampleapp:instance1 (This is how google cloud sql creates by default)
Connections string:
Correct:
jdbc:google:mysql://sampleapp:instance1/dbname?user=root&password="+password+"useUnicode=true&characterEncoding=UTF-8";
In correct
jdbc:google:mysql://sampleapp:sampleapp:instance1/dbname?user=root&password="+password+"useUnicode=true&characterEncoding=UTF-8";