[[["易于理解","easyToUnderstand","thumb-up"],["解决了我的问题","solvedMyProblem","thumb-up"],["其他","otherUp","thumb-up"]],[["很难理解","hardToUnderstand","thumb-down"],["信息或示例代码不正确","incorrectInformationOrSampleCode","thumb-down"],["没有我需要的信息/示例","missingTheInformationSamplesINeed","thumb-down"],["翻译问题","translationIssue","thumb-down"],["其他","otherDown","thumb-down"]],["最后更新时间 (UTC):2025-09-05。"],[],[],null,["# Migrate from MySQL to Spanner\n\nThis page offers an overview of how to migrate your Online Transactional\nProcessing (OLTP) database from MySQL to [Spanner](/spanner).\nThe process to migrate to Spanner might vary depending on factors\nlike data size, downtime requirements, application code complexity, sharding\nschema, custom functions, and failover and replication strategies.\n\nThe Spanner migration is broken down into the following steps:\n\n1. Assess your migration.\n2. Migrate your schema, and translate any SQL queries.\n3. Migrate your application to use Spanner in addition to MySQL.\n4. Load sample data and optimize your performance.\n5. Migrate your data.\n6. Validate the migration.\n7. Configure cutover and fallback mechanisms.\n\nAssess your migration\n---------------------\n\nAssessing a migration from your source MySQL database to Spanner\nrequires evaluating your business, technical, operational, and financial needs.\nFor more information, see\n[Assess your migration](/spanner/docs/assess-migration).\n\nMigrate your schema\n-------------------\n\nYou convert your existing schema to a Spanner\n[schema](/spanner/docs/schema-and-data-model) using Spanner\nmigration tool.\n\nFor more information, see\n[Migrate schema from MySQL overview](/spanner/docs/migrate-mysql-schema).\n\nMigrate your application to use Spanner\n---------------------------------------\n\nSpanner provides a set of\n[Client libraries](/spanner/docs/reference/libraries)\nfor various languages, and the ability to read and write data using\nSpanner-specific API calls, as well as by using\n[SQL queries](/spanner/docs/query-syntax)\nand\n[Data Modification Language (DML)](/spanner/docs/dml-syntax)\nstatements. Using API calls might be faster for some queries, such as direct row\nreads by key, because the SQL statement doesn't have to be translated.\n\nSpanner provides a\n[JDBC driver](/spanner/docs/use-oss-jdbc)\nfor Java applications.\n\nAs part of the migration process, features not available in\nSpanner as mentioned previously must be implemented in the\napplication. For example, a trigger to verify data values and update a related\ntable would need to be implemented in the application using a read or write\ntransaction to read the existing row, verify the constraint, then write the\nupdated rows to both tables.\n\nSpanner offers\n[read or write and read-only transactions](/spanner/docs/transactions)\nwhich ensure external consistency of your data. Additionally, read transactions\ncan have\n[Timestamp bounds](/spanner/docs/timestamp-bounds)\napplied, where you are reading a consistent version of the data either:\n\n- at an exact time in the past (up to 1 hour ago).\n- in the future (where the read will block until that time arrives).\n- 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.\n\nLoad sample data to Spanner\n---------------------------\n\nYou can load sample data in to Spanner\nbefore performing a complete data migration to test schemas, queries, and your\napplication.\n\nYou can use the\n[BigQuery reverse ETL](/bigquery/docs/export-to-spanner)\nworkflow and the [Google Cloud CLI](/sdk) to load a small amount of data in the\nCSV file format in to Spanner.\n\nFor more information, see [Load sample data](/spanner/docs/load-sample-data).\n\nTo transfer your data from MySQL to Spanner, you can also\nexport your MySQL database to a portable file format---for example,\nXML---and then import that data into Spanner using\nDataflow.\n\nMigrate data to Spanner\n-----------------------\n\nAfter optimizing your Spanner schema and loading sample data, you\ncan move your data into an empty production-sized Spanner database.\n\nFor more information, see [Live data migration from MySQL](/spanner/docs/mysql-live-data-migration).\n\nValidate your data migration\n----------------------------\n\nAs data streams into your Spanner database, you can periodically\nrun a comparison between your Spanner data and your MySQL data\nto make sure that the data is consistent. You can validate consistency by\nquerying both data sources and comparing the results.\n\nYou can use Dataflow to perform a detailed comparison over large\ndata sets by using\n[join transform](https://beam.apache.org/documentation/pipelines/design-your-pipeline/#multiple-sources).\nThis transform takes 2 keyed data sets, and matches the values by key. The\nmatched values can then be compared for equality. You can regularly run this\nverification until the level of consistency matches your business requirements.\n\nFor more information, see\n[Validate your data migration](/spanner/docs/data-validation).\n\nConfigure cutover and fallback mechanisms\n-----------------------------------------\n\nYou can set up cutover and fallback for MySQL using reverse replication. Cutover\nand fallback means you have a contingency plan of reverting to your source\nMySQL database if you encounter issues with Spanner.\n\nReverse replication is useful when you encounter unforeseen issues with\nSpanner and need to fall back to the original MySQL database with\nminimum disruption to the service. Reverse replication enables fall back by\nreplicating data written on Spanner back on your source MySQL\ndatabase.\n\nThe reverse replication flow involves the following steps, performed by the\n[`Spanner to SourceDB` Dataflow template](https://github.com/GoogleCloudPlatform/DataflowTemplates/tree/main/v2/spanner-to-sourcedb):\n\n1. Read changes from Spanner using\n [Spanner change streams](/spanner/docs/change-streams).\n\n2. Filter the forward-migrated changes.\n\n3. Transform Spanner data to be compatible with your source database\n schema.\n\n4. Verify whether the source database already contains more recent data for the\n specified primary key.\n\n5. Write the data to your source database.\n\nWhat's next\n-----------\n\n- [Best practices in schema design](/spanner/docs/schema-design).\n- [Optimize your Spanner schema](/spanner/docs/whitepapers/optimizing-schema-design).\n- Learn how to use [Dataflow](/dataflow/docs/how-to) for more complex situations."]]