OK So I am trying to make a Java server that posts to a cloudSQL instance using cloudSQL Proxy and I am stuck.
I started both the proxy and the server successfully on my local machine:
./cloud_sql_proxy -instances=<INSTANCE_CONNECTION_NAME>=tcp:3306 \
-credential_file=<PATH_TO_KEY_FILE> &
This is the server:
public class PostMainServer {
public static void main(String[] args) throws Exception {
Server server = new Server(7070);
ServletContextHandler handler = new ServletContextHandler(server, "/post");
handler.addServlet(Servlet.class, "/");
server.start();
}
}
This is a Jetty servlet that the server uses:
@SuppressWarnings({ "deprecation", "resource" })
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// ...... logic goes here...... //
// Now it's time to connect to the CloudSQL instance.
String jdbcUrl = String.format(
"jdbc:mysql://google/%s?cloudSqlInstance=%s&"
+ "socketFactory=com.google.cloud.sql.mysql.SocketFactory",
databaseName,
instanceConnectionName);
Connection conn = DriverManager.
getConnection(jdbcUrl, username, password); //<----- The Exception is thrown here....
// the rest below.....
}
This is the error I get from the console when I use Postman to make a POST request:
Apr 09, 2017 6:30:21 PM com.google.cloud.sql.mysql.SocketFactory connect
INFO: Connecting to Cloud SQL instance [INSTANCE-NAME].
Apr 09, 2017 6:30:21 PM com.google.cloud.sql.mysql.SslSocketFactory getInstance
INFO: First Cloud SQL connection, generating RSA key pair.
java.sql.SQLNonTransientConnectionException: Could not create connection to database server.
When walking through the debugger the SQLException is thrown here in the getConnection() method of the DriverManager class:
SQLException reason = null;
for(DriverInfo aDriver : registeredDrivers) {
// If the caller does not have permission to load the driver then
// skip it.
if(isDriverAllowed(aDriver.driver, callerCL)) {
try {
println(" trying " + aDriver.driver.getClass().getName());
Connection con = aDriver.driver.connect(url, info);
if (con != null) {
// Success!
println("getConnection returning " + aDriver.driver.getClass().getName());
return (con);
}
} catch (SQLException ex) {
if (reason == null) {
reason = ex; // <--------- THIS LINE IS REACHED
}
}
} // The rest of the method below.....
Lastly the proxy does not register any connections it is stuck here:
2017/04/09 18:29:21 Listening on [INSTANCE-CONNECTION-NAME]
2017/04/09 18:29:21 Ready for new connections...
Assuming that the username and password credentials are right, what could possibly be the cause of this refusal to connect to the proxy? I am stuck for 2 days now.
答案 0 :(得分:1)
好的,事实证明我安装了云SDK但是我没有在我的本地服务器上通过首先在命令行上运行'gcloud auth login'来授权它。