10bet网址
MySQL 8.0 Reference Manual
Related Documentation Download this Manual
PDF (US Ltr)- 41.9Mb
PDF (A4)- 42.0Mb
Man Pages (TGZ)- 266.3Kb
Man Pages (Zip)- 376.1Kb
Info (Gzip)- 4.0Mb
Info (Zip)- 4.0Mb
Excerpts from this Manual

17.1.2.5 Choosing a Method for Data Snapshots

If the source database contains existing data it is necessary to copy this data to each replica. There are different ways to dump the data from the source database. The following sections describe possible options.

To select the appropriate method of dumping the database, choose between these options:

  • Use themysqldumptool to create a dump of all the databases you want to replicate. This is the recommended method, especially when usingInnoDB.

  • If your database is stored in binary portable files, you can copy the raw data files to a replica. This can be more efficient than usingmysqldumpand importing the file on each replica, because it skips the overhead of updating indexes as theINSERTstatements are replayed. With storage engines such asInnoDBthis is not recommended.

  • Use MySQL Server's clone plugin to transfer all the data from an existing replica to a clone. For instructions to use this method, seeSection 5.6.7.7, “Cloning for Replication”.

Tip

To deploy multiple instances of MySQL, you can useInnoDB Clusterwhich enables you to easily administer a group of MySQL server instances inMySQL Shell. InnoDB Cluster wraps MySQL Group Replication in a programmatic environment that enables you easily deploy a cluster of MySQL instances to achieve high availability. In addition, InnoDB Cluster interfaces seamlessly withMySQL Router, which enables your applications to connect to the cluster without writing your own failover process. For similar use cases that do not require high availability, however, you can useInnoDB ReplicaSet. Installation instructions for MySQL Shell can be foundhere.

17.1.2.5.1 Creating a Data Snapshot Using mysqldump

To create a snapshot of the data in an existing source database, use themysqldumptool. Once the data dump has been completed, import this data into the replica before starting the replication process.

The following example dumps all databases to a file nameddbdump.db, and includes the--master-dataoption which automatically appends theCHANGE REPLICATION SOURCE TO|CHANGE MASTER TOstatement required on the replica to start the replication process:

$> mysqldump --all-databases --master-data > dbdump.db
Note

If you do not use--master-data, then it is necessary to lock all tables in a separate session manually. See部分17.1.2.4”,获得复制源头e Binary Log Coordinates”.

It is possible to exclude certain databases from the dump using themysqldumptool. If you want to choose which databases to include in the dump, do not use--all-databases. Choose one of these options:

  • Exclude all the tables in the database using--ignore-tableoption.

  • Name only those databases which you want dumped using the--databasesoption.

Note

By default, if GTIDs are in use on the source (gtid_mode=ON),mysqldumpincludes the GTIDs from thegtid_executedset on the source in the dump output to add them to thegtid_purgedset on the replica. If you are dumping only specific databases or tables, it is important to note that the value that is included bymysqldumpincludes the GTIDs of all transactions in thegtid_executedset on the source, even those that changed suppressed parts of the database, or other databases on the server that were not included in the partial dump. Check the description for mysqldump's--set-gtid-purgedoption to find the outcome of the default behavior for the MySQL Server versions you are using, and how to change the behavior if this outcome is not suitable for your situation.

For more information, seeSection 4.5.4, “mysqldump — A Database Backup Program”.

To import the data, either copy the dump file to the replica, or access the file from the source when connecting remotely to the replica.

17.1.2.5.2 Creating a Data Snapshot Using Raw Data Files

This section describes how to create a data snapshot using the raw files which make up the database. Employing this method with a table using a storage engine that has complex caching or logging algorithms requires extra steps to produce a perfectpoint in timesnapshot: the initial copy command could leave out cache information and logging updates, even if you have acquired a global read lock. How the storage engine responds to this depends on its crash recovery abilities.

If you useInnoDBtables, you can use themysqlbackupcommand from the MySQL Enterprise Backup component to produce a consistent snapshot. This command records the log name and offset corresponding to the snapshot to be used on the replica. MySQL Enterprise Backup is a commercial product that is included as part of a MySQL Enterprise subscription. SeeSection 30.2, “MySQL Enterprise Backup Overview”for detailed information.

This method also does not work reliably if the source and replica have different values forft_stopword_file,ft_min_word_len, orft_max_word_lenand you are copying tables having full-text indexes.

Assuming the above exceptions do not apply to your database, use thecold backuptechnique to obtain a reliable binary snapshot ofInnoDBtables: do aslow shutdownof the MySQL Server, then copy the data files manually.

To create a raw data snapshot ofMyISAMtables when your MySQL data files exist on a single file system, you can use standard file copy tools such ascporcopy, a remote copy tool such asscporrsync, an archiving tool such asziportar, or a file system snapshot tool such asdump. If you are replicating only certain databases, copy only those files that relate to those tables. ForInnoDB, all tables in all databases are stored in thesystem tablespacefiles, unless you have theinnodb_file_per_tableoption enabled.

The following files are not required for replication:

  • Files relating to themysqldatabase.

  • The replica's connection metadata repository filemaster.info, if used; the use of this file is now deprecated (seeSection 17.2.4, “Relay Log and Replication Metadata Repositories”).

  • The source's binary log files, with the exception of the binary log index file if you are going to use this to locate the source binary log coordinates for the replica.

  • Any relay log files.

Depending on whether you are usingInnoDBtables or not, choose one of the following:

If you are usingInnoDBtables, and also to get the most consistent results with a raw data snapshot, shut down the source server during the process, as follows:

  1. Acquire a read lock and get the source's status. See部分17.1.2.4”,获得复制源头e Binary Log Coordinates”.

  2. In a separate session, shut down the source server:

    $> mysqladmin shutdown
  3. Make a copy of the MySQL data files. The following examples show common ways to do this. You need to choose only one of them:

    $> tar cf/tmp/db.tar./data$> zip -r/tmp/db.zip./data$> rsync --recursive./data/tmp/dbdata
  4. Restart the source server.

If you are not usingInnoDBtables, you can get a snapshot of the system from a source without shutting down the server as described in the following steps:

  1. Acquire a read lock and get the source's status. See部分17.1.2.4”,获得复制源头e Binary Log Coordinates”.

  2. Make a copy of the MySQL data files. The following examples show common ways to do this. You need to choose only one of them:

    $> tar cf/tmp/db.tar./data$> zip -r/tmp/db.zip./data$> rsync --recursive./data/tmp/dbdata
  3. In the client where you acquired the read lock, release the lock:

    mysql> UNLOCK TABLES;

Once you have created the archive or copy of the database, copy the files to each replica before starting the replication process.