Google Cloud SQL provides a relational database that you can use with your App Engine application. Cloud SQL is a MySQL database that lives in Google's cloud. To learn more about Google Cloud SQL, see the Google Cloud SQL documentation.
You can connect to Cloud SQL First Generation or Second Generation instances.
For information on pricing and restrictions imposed by both Cloud SQL and App Engine, see Pricing and Access Limits.
Before you begin
- Select or create a Cloud Platform Console project.
- To test in your development environment, you need to connect to a local MySQL
server. Install the MySQL Community Server.
If you use Linux on a distribution with
apt-get, you can run:
sudo apt-get install mysql-server
- Install Composer, a tool for dependency management for PHP.
In the same project as your App Engine application, create a Cloud SQL instance and configure
the root user.
You can use a Cloud SQL First Generation or a Second Generation instance. For more information about Cloud SQL Second Generation, see Second Generation Capabilities. Note that Cloud SQL Second Generation is in beta.
- For the Cloud SQL instance, create a database.
The default user for App Engine applications is
root@localhost. If you don't want to use the default user, create a user.
If you are using a Cloud SQL First Generation instance, in the
Cloud Platform Console, grant
your App Engine application access to the Google Cloud SQL instance.
If you are using a Cloud SQL Second Generation instance and your application is in the same project as the SQL instance, then your application should already have access.
- Get the Cloud SQL instance connection name to use as a connection string
in your application code. To find the instance connection name, go to the
Cloud SQL Instances page in the Cloud Platform Console.
Go to the Cloud SQL Instances page
- Click on the Instance ID for the Cloud SQL instance you are using.
- Under Properties, find the Instance connection name.
- Add the Cloud SQL instance connection name, database name, user, and
password to the environment variables
For example, replace the values in
MYSQL_PASSWORD. Note that this sample uses App Engine's native UNIX socket with the prefix
/cloudsql/to connect with Cloud SQL:
To install dependencies using Composer, run:
Code sample overviewThe following sample reads and displays guestbook entries from Cloud SQL. When you click to sign the guestbook, the application adds the entry and writes the results back to Cloud SQL: If you want to use this sample, follow the steps below to test and deploy the application. After the application is running, go to
/create_tablesto create the tables needed.
Testing and deployingThe development server in the Google App Engine SDK can use a locally installed MySQL server instance to closely mirror the Google Cloud SQL environment during development. To test in your development environment:
- Update the connection string in your application code to connect to a local
instance of MySQL server.
For example, you can set the MySQL connection parameters with the following commands:
cd php-docs-samples/appengine/standard/cloudsql export MYSQL_DSN="mysql:host=127.0.0.1;port=3306;dbname=guestbook" export MYSQL_USERNAME=root export MYSQL_PASSWORD= php -S localhost:8080
- Start the MySQL server in your development environment.
- Start the development server, including a path to the application directory. For example, if your application's
main.phpfile is in a directory named
- The web server is now running and listening for requests on port 8080. To view, visit the following URL:
- When you are ready, deploy your project to App Engine:
appcfg.py update cloudsql/