Accessing a local MySQL instance in your GAE devel

2020-07-27 19:07发布

问题:

I am new to Google App Engine environment. We are starting a project where we are using Google Cloud SQL. For testing purpose we need to setup a local MySQL instance. I have tried searching for the answer, but I didn't find any that helped me.

If I was to summarize my question, I am trying access a local MySQL instance in my GAE development environment using JAVA in Eclipse.

回答1:

You have to add the MySQL connector in you App Engine SDK folder.

You can find the connector there: http://dev.mysql.com/downloads/connector/j/. Then, you have to place it in this folder: appengine-java-sdk\lib\impl

Then you have to run a local version of MySQL (for example using EasyPHP).

Here is a sample of the code that you could use to connect to your database (singleton) :

public static Connection getInstance() throws Exception {
  if (connection != null && !connection.isClosed()) {
    return connection;
  }
  if (isLocalTesting) {
    //MySQL
    String url = "jdbc:mysql://127.0.0.1:3306/YOUR_DB_NAME";
    connection = DriverManager.getConnection(url, "root", "");
  } else {
    // Google Cloud SQL
    DriverManager.registerDriver(new AppEngineDriver());
    connection = DriverManager.getConnection("jdbc:google:rdbms://" + instanceName + "/NAME_DB");
  }
  return connection;
}

And lastly: You have to include the MySQL library in your build path as well: http://prntscr.com/124jwm



回答2:

I use Eclipse Juno and install Google App Engine SDK and plugin. There is configuration for development mysql and google cloud SQL instance. its automaticaly go to your local when development and go to cloud SQL when you deploy it.