This page offers an overview of how to migrate your Online Transactional
Processing (OLTP) database from MySQL to [Spanner](https://docs.cloud.google.com/spanner).
The process to migrate to Spanner might vary depending on factors
like data size, downtime requirements, application code complexity, sharding
schema, custom functions, and failover and replication strategies.

The Spanner migration is broken down into the following steps:

1. Assess your migration.
2. Migrate your schema, and translate any SQL queries.
3. Migrate your application to use Spanner in addition to MySQL.
4. Load sample data and optimize your performance.
5. Migrate your data.
6. Validate the migration.

## Assess your migration

Assessing a migration from your source MySQL database to Spanner
requires evaluating your business, technical, operational, and financial needs.
For more information, see
[Assess your migration](https://docs.cloud.google.com/spanner/docs/assess-migration.md.txt).

## Migrate your schema

You convert your existing schema to a Spanner
[schema](https://docs.cloud.google.com/spanner/docs/schema-and-data-model.md.txt) using Spanner
migration tool.

For more information, see
[Migrate a schema from MySQL](https://docs.cloud.google.com/spanner/docs/migrate-mysql-schema.md.txt).

## Migrate your application to use Spanner

Spanner provides a set of
[Client libraries](https://docs.cloud.google.com/spanner/docs/reference/libraries.md.txt)
for various languages, and the ability to read and write data using
Spanner-specific API calls, as well as by using
[SQL queries](https://docs.cloud.google.com/spanner/docs/query-syntax)
and
[Data Modification Language (DML)](https://docs.cloud.google.com/spanner/docs/dml-syntax)
statements. Using API calls might be faster for some queries, such as direct row
reads by key, because the SQL statement doesn't have to be translated.

Spanner provides a
[JDBC driver](https://docs.cloud.google.com/spanner/docs/use-oss-jdbc.md.txt)
for Java applications.

As part of the migration process, features not available in
Spanner as mentioned previously must be implemented in the
application. For example, a trigger to verify data values and update a related
table would need to be implemented in the application using a read or write
transaction to read the existing row, verify the constraint, then write the
updated rows to both tables.

The default isolation level of transactions in Spanner is
[serializable isolation](https://docs.cloud.google.com/spanner/docs/isolation-levels.md.txt#serializable), which
ensures external consistency of your data. Spanner also offers
[repeatable read isolation](https://docs.cloud.google.com/spanner/docs/isolation-levels.md.txt#repeatable-read). We
recommended that you set the isolation level to repeatable read, and the
concurrency control to
[pessimistic concurrency](https://docs.cloud.google.com/spanner/docs/concurrency-control.md.txt#pessimistic_concurrency_in_repeatable_read_isolation)
as part of the application migration process so that the transaction semantics
of Spanner matches closely with the default transaction semantics
of MySQL. For instructions on how to set the isolation level and concurrency
control in your application, see
[Use repeatable read isolation level](https://docs.cloud.google.com/spanner/docs/use-repeatable-read-isolation.md.txt)
and
[Configure concurrency control](https://docs.cloud.google.com/spanner/docs/concurrency-control.md.txt#configure_concurrency_control).

Spanner offers
[read or write and read-only transactions](https://docs.cloud.google.com/spanner/docs/transactions.md.txt).
Additionally, read transactions can have
[Timestamp bounds](https://docs.cloud.google.com/spanner/docs/timestamp-bounds.md.txt)
applied, where you are reading a consistent version of the data either:

- at an exact time in the past (up to 1 hour ago).
- in the future (where the read will block until that time arrives).
- with an acceptable amount of bounded staleness, which will return a consistent view up to some time in the past without needing to check that later data is available on another replica. This can give performance benefits at the expense of possibly stale data.

## Load sample data to Spanner

You can load sample data in to Spanner
before performing a complete data migration to test schemas, queries, and your
application.

You can use the
[BigQuery reverse ETL](https://docs.cloud.google.com/bigquery/docs/export-to-spanner.md.txt)
workflow and the [Google Cloud CLI](https://docs.cloud.google.com/sdk) to load a small amount of data in the
CSV file format in to Spanner.

For more information, see [Load sample data](https://docs.cloud.google.com/spanner/docs/load-sample-data.md.txt).

To transfer your data from MySQL to Spanner, you can also
export your MySQL database to a portable file format---for example,
XML---and then import that data into Spanner using
Dataflow.

## Migrate data to Spanner

After optimizing your Spanner schema and loading sample data, you
can move your data into an empty production-sized Spanner database.

For more information, see [Migrate your MySQL data](https://docs.cloud.google.com/spanner/docs/mysql-live-data-migration.md.txt).

## Validate your data migration

As data streams into your Spanner database, you can periodically
run a comparison between your Spanner data and your MySQL data
to make sure that the data is consistent. You can validate consistency by
querying both data sources and comparing the results.

You can use Dataflow to perform a detailed comparison over large
data sets by using
[join transform](https://beam.apache.org/documentation/pipelines/design-your-pipeline/#multiple-sources).
This transform takes 2 keyed data sets, and matches the values by key. The
matched values can then be compared for equality. You can regularly run this
verification until the level of consistency matches your business requirements.

For more information, see
[Validate your data migration](https://docs.cloud.google.com/spanner/docs/data-validation.md.txt).

## Configure fallback and cutover

You can set up fallback and cutover for MySQL using reverse replication.
Configuring fallback helps ensure that you have a contingency plan to
revert to your source MySQL database if issues arise after cutting over to
Spanner.

Reverse replication is useful when you encounter unexpected issues with
Spanner and need to fall back to the original MySQL database
with minimal service disruption. Reverse replication enables fallback by
replicating data written to Spanner back to your source MySQL
database.

The reverse replication flow involves the following steps, performed by the
[`spanner-to-sourcedb` Dataflow template](https://github.com/GoogleCloudPlatform/DataflowTemplates/tree/main/v2/spanner-to-sourcedb):

1. Read changes from Spanner using
   [Spanner change streams](https://docs.cloud.google.com/spanner/docs/change-streams.md.txt).

2. Filter forward-migrated changes.

3. Transform Spanner data to be compatible with your source
   database schema.

4. Verify whether the source database already contains more recent data for the
   specified primary key.

5. Write data to your source database.

## What's next

- [Schema design best practices](https://docs.cloud.google.com/spanner/docs/schema-design.md.txt).
- [Optimize your Spanner schema](https://docs.cloud.google.com/spanner/docs/whitepapers/optimizing-schema-design.md.txt).
- [Configure fallback to MySQL](https://docs.cloud.google.com/spanner/docs/mysql-configure-fallback.md.txt).
- [Perform cutover from MySQL](https://docs.cloud.google.com/spanner/docs/mysql-perform-cutover.md.txt).
- [Perform fallback to MySQL](https://docs.cloud.google.com/spanner/docs/mysql-fallback.md.txt).
- Learn how to use [Dataflow](https://docs.cloud.google.com/dataflow/docs/how-to) for more complex situations.