0
votes

I have just started with google appengine. I am developing an app in php. If I wanted to see the result, I used

dev_appserver.py ./ --php_executable_path /usr/bin/php5-cgi

(It doesn't work without the --php_executable_path parameter for me)
It works fine, except I cannot connect to the cloud sql instance. I read this https://cloud.google.com/appengine/docs/php/cloud-sql and tried to connect to the cloud sql by:

$sql = new mysqli(null,
  'root', // username
  '',     // password
  ,
  null,
  '/cloudsql/:'
  );

If I deploy something to appengine, this works, but what should I do, if I need to debug the app localy and it depends on the database?

2

2 Answers

4
votes

@DTing's answer is correct that Google's docs encourage you to use a local MySQL from the local development server and recommend that pattern.

However, if you disagree and want to run the development server against your "production" SQL in the cloud, that's supported, too (just not encouraged because a bug during development could destroy your production data!).

Specifically, you follow the general instructions at, and pointed to by, https://cloud.google.com/sql/docs/getting-started#work (ignoring the appengine-specific part): make sure your Cloud SQL instance has an IP address, enable the outside-visible IP address of your workstation, make sure the SQL instance has a root password -- then check everything is working with a command line MySQL client, e.g

[[Note: to verify your workstation's outside-visible IP address, use e.g a browser to visit a site such as http://checkmyip.com/ ]]

$ mysql --host=INSTANCE_IP --user=root --password

and once everything is set up properly you just follow the instructions at https://cloud.google.com/appengine/docs/php/cloud-sql/#PHP_Using_a_local_MySQL_instance_during_development :

To connect to a Cloud SQL instance from your development environment, substitute "127.0.0.1" with the instance IP address. You do not use the "/cloudsql/"-based connection string to connect to a Cloud SQL instance if your App Engine app is running locally in the Development Server.

If you want to use the same code locally and deployed, you can use a Special $_SERVER keys variable (SERVER_SOFTWARE) to determine where your code is running. This approach is shown below.

So for example if your Cloud SQL's IP address is 12.34.56.78, you'd use

$sql = new mysqli('12.34.56.78:3306',
  '<username>',
  '<password>',
  <database-name>
  );

when $_SERVER['SERVER_SOFTWARE'] is not set or does not contain Google App Engine (which means you're running on the local development server).

0
votes

https://cloud.google.com/appengine/docs/php/cloud-sql/#PHP_Using_a_local_MySQL_instance_during_development

The Guestbook example above shows how your application can connect to a Cloud SQL instance when the code runs in App Engine and connect to a local MySQL server when the code runs in the Development Server. We encourage this pattern to minimize confusion and maximize flexibility.