Why my java program keeps connecting to Cloud SQL via cloudsqlproxy

瀏覽次數:175 次
跳到第一則未讀訊息

Khanh Phan

未讀,
2020年3月4日 上午9:30:552020/3/4
收件者:Google Cloud SQL discuss

My Google Cloud mySQL is setup and running. In the user table, I have created id=test and allowed it to connect from % (any host)

I have a small Java program

Properties dbcpProperties = new Properties();

String CLOUD_SQL_CONNECTION_NAME = "google_project:us-central1:instance";

dbcpProperties.put("driverClassName","com.mysql.jdbc.Driver");

dbcpProperties.put("url",
        String.format(
            "jdbc:mysql://google/%s?socketFactory=com.google.cloud.sql.mysql.SocketFactory"
                + "&cloudSqlInstance=%s", DB_NAME, CLOUD_SQL_CONNECTION_NAME));

dbcpProperties.put("username", DB_USER);
dbcpProperties.put("password", DB_PASS);

try {
      BasicDataSource dataSource = (BasicDataSource) BasicDataSourceFactory.createDataSource(
          dbcpProperties);
      System.out.println("UPN Data source succesfully created.");
    } catch (Exception e) {
      System.out.println("FATAL ERROR: Couldn't create new Data source.");
      throw new RuntimeException(e);
    }

Everytime I run it, it complains Caused by: org.apache.commons.dbcp.SQLNestedException: Cannot create PoolableConnectionFactory (Access denied for user 'test'@'cloudsqlproxy~myipaddress' (using password: YES)) at org.apache.commons.dbcp.BasicDataSource.createPoolableConnectionFactory(BasicDataSource.java:1549) at org.apache.commons.dbcp.BasicDataSource.createDataSource(BasicDataSource.java:1388) at org.apache.commons.dbcp.BasicDataSource.getLogWriter(BasicDataSource.java:1098) at org.apache.commons.dbcp.BasicDataSourceFactory.createDataSource(BasicDataSourceFactory.java:350) at test.Test.connect2CloudMySQL2(Test.java:1345) ... 6 more

But if I allowed user test to connect from any cloudsqlproxy~%, then it works

Why does it insists on using cloudsqlproxy ? How can I disable that?

noverlyjoseph

未讀,
2020年3月4日 中午12:03:462020/3/4
收件者:Google Cloud SQL discuss

Kurtis Van Gent

未讀,
2020年3月4日 下午4:41:522020/3/4
收件者:Google Cloud SQL discuss
To add additional context, the library you are using (`com.google.cloud.sql.mysql.SocketFactory`) uses an in-process "Cloud SQL proxy", which means it connects through the same server side proxy that the binary Cloud SQL proxy connects through. 

TL:DR; Using the Cloud SQL JDBC socket factory counts as using the Cloud SQL proxy. 

Khanh

未讀,
2020年3月4日 下午4:58:152020/3/4
收件者:Google Cloud SQL discuss
but in the mysql user table, I have already allowed user test to have access from any host %(any host).
shouldn't that have cover all type of from-source?

Olu

未讀,
2020年3月6日 上午10:31:132020/3/6
收件者:Google Cloud SQL discuss
Hi, Khanh

Quite rightly, leaving a MySQL Username unrestricted as you have done implies the user account is configured to connect from any host, however as indicated in this documentation[1] for example, if you are connecting to your CloudSQL Instance using IP addresses, you are still required to specify the client IP address as an Authorized address. The CloudSQL proxy however helps to avoid this configuration by providing secure access to your CloudSQL instance without having to add Authorized networks or configure SSL[2].

As rightly indicated in the comment by Kurtis above, the Cloud SQL Socket Factory for JDBC drivers should provide the same functionality[3][4]. The JDBC socket factory provides an alternative to the client-side proxy software. Your code seems to show that you are using the CloudSQL Socket Factory for JDBC. The CloudSQL API needs to be enabled to use the JDBC socket factory. Can you confirm this is enabled?

Reviewing the details provided in your issue description, it is unclear what exactly may be wrong with your implementation, however, this issue still seems to relate to your implementation. I suggest you review this sample implementations[5][6] for better understanding. Better still, if you require further assistance, it is best to contact the GCP Support[7]. This way a GCP Support Engineer would be able to review the details with you. 

回覆所有人
回覆作者
轉寄
0 則新訊息