Exporting Data from Cloud SQL

This page describes how to export data from Cloud SQL to Cloud Storage. You can then download your data from Cloud Storage to your local environment if you want to access it locally.

For best practices for exporting data, see Best Practices for Importing and Exporting Data.

Before you begin

Decide what file type you are creating: a SQL dump file or a CSV file. To export your data to an external MySQL database, such as an external replica for Cloud SQL, create a SQL dump file. To export your data for use with other databases or tools, export to a CSV file.

Exporting data to a SQL dump file

Exporting from Cloud SQL uses the mysqldump utility with the --skip-triggers option, and excludes the mysql database. This means that the export does not include any triggers, stored procedures, functions, or views. If your database requires any of these elements, you must recreate them manually after you import your data.

If this export does not meet your requirements, you can also run the mysqldump utility directly against your Cloud SQL database, using whatever options you require.

Console

  1. Go to the Cloud SQL Instances page in the Google Cloud Platform Console.

    Go to the Cloud SQL Instances page

  2. Click the instance you want to export data from to open its Instance details page.
  3. Click Export in the button bar.
  4. Under Cloud Storage file, select a Cloud Storage bucket or folder for your export.
  5. In the Name field, provide a name for your export file and click Select.
  6. Set the Format to SQL.
  7. Click Show advanced options and enter a comma-separated list of all of the databases that you have created.
  8. Click Export to start the export.

gcloud (2nd Gen)

To export a dump file from an instance to a Cloud Storage bucket:

  1. Create a bucket for the export:
    gsutil mb -p [PROJECT_NAME] -l [LOCATION_NAME] gs://[BUCKET_NAME]
    

    This step is not required, but strongly recommended, so you do not open up access to any other data.

  2. Describe the instance you are exporting from:
    gcloud sql instances describe [INSTANCE_NAME]
    
  3. Copy the serviceAccountEmailAddress field.
  4. Add the service account to the bucket ACL as a writer:
    gsutil acl ch -u [SERVICE_ACCOUNT_ADDRESS]:W gs://[BUCKET_NAME]
    
  5. Export the database:
    gcloud sql export sql [INSTANCE_NAME] gs://[BUCKET_NAME]/sqldumpfile.gz \
                                --database=[DATABASE_NAME]
    

    For information about using the export command, see the sql export sql command reference page.

  6. If you do not need to retain the permissions provided by the ACL you set previously, remove the ACL:
    gsutil acl ch -d [SERVICE_ACCOUNT_ADDRESS] gs://[BUCKET_NAME]
    

gcloud (1st Gen)

gcloud sql export sql [INSTANCE_NAME] gs://[BUCKET_NAME]/sqldumpfile.gz \
                            --database [DATABASE_NAMES]

If you get an error like ERROR_RDBMS, you might not have the required permissions to export to that bucket. Confirm that you have at least WRITER permissions on the bucket. For more information on configuring access control in Cloud Storage, see Create and Manage Access Control Lists.

For information about using the export command, see the sql export sql command reference page.

cURL

  1. Create a bucket for the export:
    gsutil mb -p [PROJECT_NAME] -l [LOCATION_NAME] gs://[BUCKET_NAME]
    

    This step is not required, but strongly recommended, so you do not open up access to any other data.

  2. Provide your instance with write permissions to your bucket.

    For help with setting ACLs, see Setting ACLs.

  3. Export your database:
    ACCESS_TOKEN="$(gcloud auth application-default print-access-token)"
    curl --header "Authorization: Bearer ${ACCESS_TOKEN}" \
         --header 'Content-Type: application/json' \
         --data '{"exportContext":
                    {"fileType": "SQL",
                     "uri": "gs://<BUCKET_NAME>/<PATH_TO_DUMP_FILE>",
                     "databases": ["<DATABASE_NAME1>", "<DATABASE_NAME2>"] }}' \
       -X POST \
       https://www.googleapis.com/sql/v1beta4/projects/[PROJECT-ID]/instances/[INSTANCE_NAME]/export
    
  4. If you do not need to retain the permissions provided by the ACL you set previously, remove the ACL.
For the complete list of parameters for this request, see the instances:export page.

Exporting data to a CSV file

You can export your data in CSV format if you need to access it with a tool other than MySQL.

To export data to a CSV file:

  1. Go to the Cloud SQL instances page in the Google Cloud Platform Console.
    Go to the Cloud SQL instances page
  2. Click the instance to open its Instance details page.
  3. Click Export.
  4. Under Cloud Storage file, select a Cloud Storage bucket or folder for your export.
  5. In the Name field, provide a name for your export file and click Select.

    For Second Generation instances, you can use a file extension of .gz to compress your export file.

  6. Set the Format to CSV.
  7. Enter a SQL query to specify the data to export.

    For example, to export the entire contents of the entries table in the guestbook database, you would enter SELECT * FROM guestbook.entries;. You must specify a table; you cannot export an entire database in CSV format.

  8. Click Export to start the export.

What's next

Was this page helpful? Let us know how we did:

Send feedback about...

Cloud SQL for MySQL