简体   繁体   中英

Trying to connect simple Java program to Google Cloud Platform MySQL Database

I am trying to connect this small Java program to my MySQL Database instance on GCP for educational purposes.

This code works well with the instance I have locally on localhost and port 3306. However, this is not very practical or useful.

Scenarios that work:

  1. Connecting Java client program to MySQL database served locally.
  2. Connecting MySQL Workbench client to MySQL database served locally.
  3. Connecting MySQL Workbench client to MySQL database served on Google Cloud Platform.

My objective:

  1. Connect very simple Java client program to MySQL database served on Google Cloud Platform.

Jar files used:

  1. mysql-connector-java-8.0.29.jar
  2. mysql-socket-factory-1.6.1-jar-with-dependencies.jar

IDE used:

  1. Eclipse

The code:


    package test;

import java.sql.*;
public class Test
{
    public static void main(String[] args)
    {
        String CREDENTIALS = "jdbc:mysql://google/emexdb?cloudSqlInstance=ethereal-shine-354205:us-central1:emexdb-mysql&socketFactory=google.cloud.sql.mysql.SocketFactory&useSSL=false&user=****&password=***********";
        try
        {
            //Get a connection to database
            Connection myConn = DriverManager.getConnection(CREDENTIALS);
            
            //Create a statement
            Statement myStmt = myConn.createStatement();
            //Execute SQL query
            ResultSet myRs = myStmt.executeQuery("select * from Test;");
            //Process the result set
            while(myRs.next())
            {
                System.out.println(myRs.getNString("id")+"  "+myRs.getString("_name"));
            }
            
            myConn.close();
        }
        catch(Exception eSQL)
        {
            eSQL.printStackTrace();
        }
    }
}

This is the error message Eclipse is giving me:

java.sql.SQLNonTransientConnectionException: Cannot connect to MySQL server on google:3,306.

Make sure that there is a MySQL server running on the machine/port you are trying to connect to and that the machine this software is running on is able to connect to this host/port (i.e. not firewalled). Also make sure that the server has not been started with the --skip-networking flag.


at com.mysql.cj.jdbc.exceptions.SQLError.createSQLException(SQLError.java:110)
at com.mysql.cj.jdbc.exceptions.SQLError.createSQLException(SQLError.java:97)
at com.mysql.cj.jdbc.exceptions.SQLError.createSQLException(SQLError.java:89)
at com.mysql.cj.jdbc.exceptions.SQLError.createSQLException(SQLError.java:63)
at com.mysql.cj.jdbc.exceptions.SQLError.createSQLException(SQLError.java:73)
at com.mysql.cj.jdbc.ConnectionImpl.<init>(ConnectionImpl.java:462)
at com.mysql.cj.jdbc.ConnectionImpl.getInstance(ConnectionImpl.java:241)
at com.mysql.cj.jdbc.NonRegisteringDriver.connect(NonRegisteringDriver.java:198)
at java.sql/java.sql.DriverManager.getConnection(DriverManager.java:683)
at java.sql/java.sql.DriverManager.getConnection(DriverManager.java:253)
at test.Test.main(Test.java:12)
Caused by: java.lang.NullPointerException: Cannot invoke "com.mysql.cj.protocol.a.NativeProtocol.getSocketConnection()" because the return value of "com.mysql.cj.NativeSession.getProtocol()" is null
    at com.mysql.cj.jdbc.ConnectionImpl.connectOneTryOnly(ConnectionImpl.java:972)
    at com.mysql.cj.jdbc.ConnectionImpl.createNewIO(ConnectionImpl.java:818)
    at com.mysql.cj.jdbc.ConnectionImpl.<init>(ConnectionImpl.java:448)
    ... 5 more

Any help would be much appreciated. Thank you!

Have you whitelisted your public IP in the settings of your GCP MySQL database?

You can do that here:
Your GCP Project ⇾ SQL (Press the three horizontal bars in the left corner) ⇾ Connections ⇾ Authorised networks ⇾ ADD NETWORK

You can use this Website to get your public IP.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM