The mysqldump export tool

Updated at:

This topic describes how to use the mysqldump tool to export data from PolarDB-X. It covers common scenarios and provides detailed procedures.

Tool introduction

PolarDB-X supports mysqldump, the official MySQL data export tool. The mysqldump tool exports table schemas and data as SQL statements that you can import directly.

Note

The mysqldump tool is suitable for offline exports of small data volumes of less than 10 million rows. For larger data volumes or real-time data migration tasks, use DTS.

Syntax

To export data using the mysqldump tool, run the following command:

mysqldump -h ip -P port -u user -pPassword --default-character-set=char-set --net_buffer_length=10240 --no-create-db --no-create-info --skip-add-locks --skip-lock-tables --skip-tz-utc --set-charset  [--hex-blob] [--no-data] database [table1 table2 table3...] > dump.sql

For a description of mysqldump parameters, run the mysqldump --help command or see the official MySQL documentation. The following table describes common parameters:

Parameter Name

Description

ip

The IP address of the PolarDB-X instance.

port

The port of the PolarDB-X instance.

user

The username for PolarDB-X.

password

The password for PolarDB-X. Note that there is no space between -p and the password.

char-set

The specified character set.

--hex-blob

Exports binary string fields using hexadecimal format. You must use this option if you have binary data. This affects field types such as BINARY, VARBINARY, and BLOB.

--no-data

Does not export data.

table

Specifies a table to export. By default, all tables in the database are exported.

--no-create-info

Does not export table creation information.

--net_buffer_length

The size of the transfer buffer. This affects the length of the INSERT statement. The default value is 1046528.

Examples

The following sections provide examples of how to use the mysqldump tool in different scenarios.

When you transfer data between PolarDB-X and MySQL, do not export the table schema. This is because PolarDB-X supports sharding, and keywords in the CREATE TABLE (DRDS mode) statement, such as sharding functions, are not compatible with MySQL. Incompatible keywords include the following:

  • DBPARTITION BY hash(partition_key)

  • TBPARTITION BY hash(partition_key)

  • TBPARTITIONS N

  • BROADCAST

If you export the table schema, you must modify the CREATE TABLE statement in the exported SQL file to ensure a successful import. Therefore, you should export only the table data. Then, you can log on to the database, create the tables manually, and import the data.

Scenario 1: Import from MySQL to PolarDB-X

To import data from MySQL to PolarDB-X, follow these steps.

  1. Export data from MySQL to a file.

    Run the following command to export table data from MySQL. Do not export the table schema. In this example, the export file is named dump.sql.

    mysqldump -h ip -P port -u user -pPassword --default-character-set=char-set --net_buffer_length=204800 --no-create-db --no-create-info --skip-add-locks --skip-lock-tables --skip-tz-utc --set-charset --hex-blob database [table1 table2 table3...] > dump.sql
  2. Log on to PolarDB-X and manually create the destination table. For more information about the syntax of the CREATE TABLE statement in PolarDB-X, see CREATE TABLE (DRDS mode). If you did not add the --no-create-info parameter, the dump.sql file contains the CREATE TABLE statement from the source MySQL database. You must then modify this statement in the file to be compatible with PolarDB-X.

  3. Import the data file into PolarDB-X. You can import the data file into PolarDB-X using one of the following two methods:

    • Run the mysql -h ip -P port -u user -pPassword --default-character-set=char-set command to log on to the destination PolarDB-X instance. Then, run the source /yourpath/dump.sql command to import the data into the destination PolarDB-X instance.

    • Run the mysql -h ip -P port -u user -pPassword --default-character-set=char-set< /yourpath/dump.sql command to directly import the data into the destination PolarDB-X instance.

    Note
    • In both commands, set default-character-set to the character set of the data. If you use the Windows platform, you must escape the path separators in the file path specified for the source command.

    • The first method echoes all steps to the screen. It is slightly slower but lets you monitor the import process.

    • During the import, you may receive an error similar to ERROR 1231 (HY000): [a29ef6461c00000][10.117.207.130:3306][****]Variable @saved_cs_client can't be set to the value of @@character_set_client because of implementation differences between PolarDB-X and MySQL. This type of error message does not affect the correctness of the imported data.

Scenario 2: Import from one PolarDB-X to another PolarDB-X

If you have a PolarDB-X instance in a staging environment and want to import its table schemas and data to a production PolarDB-X instance after testing, follow these steps.

  1. Export data from the source PolarDB-X instance to a text file. For more information, see Step 1 in Scenario 1.

  2. Import the data file to the PolarDB-X instance. For more information, see Step 3 in Scenario 1.

  3. Manually create Sequence objects.

    The `mysqldump` command does not export Sequence objects from PolarDB-X. If you use Sequence objects in the source PolarDB-X instance and want to use them in the destination PolarDB-X instance, you must manually create them with the same names in the destination PolarDB-X instance. To do this, follow these steps:

    1. On the source PolarDB-X instance, run the SHOW SEQUENCES command to get the status of the Sequence objects in the current PolarDB-X instance.

    2. On the destination PolarDB-X database, run the CREATE SEQUENCE command to create new Sequence objects.

    For more information about Sequence commands, see Sequence.

Scenario 3: Export data from PolarDB-X to MySQL

To export data from PolarDB-X to MySQL, you can follow a process similar to importing data between PolarDB-X instances. The steps are as follows:

  1. Export data from the source PolarDB-X to a text file. For more information, see Step 1 in Scenario 1.

  2. Log on to MySQL and manually create the destination table. If the exported data file includes `CREATE TABLE` statements, you must modify them to remove keywords and other information that are specific to PolarDB-X and incompatible with MySQL.

    For example, consider a sharded table in PolarDB-X:

    CREATE TABLE `table_name` (
        `id` int(11) NOT NULL,
        `k` int(11) NOT NULL DEFAULT '0',
        ...
    ) ENGINE = InnoDB DEFAULT CHARSET = utf8mb4 dbpartition by hash(`id`);

    Remove the sharding function statement, which is incompatible with MySQL. The modified statement is as follows:

    CREATE TABLE `table_name` (
        `id` int(11) NOT NULL,
        `k` int(11) NOT NULL DEFAULT '0',
        ...
    ) ENGINE = InnoDB DEFAULT CHARSET = utf8mb4;
  3. Import the data file into PolarDB-X. For more information, see Step 3 in scenario 1.

FAQ

  • Problem: The mysqldump command returns the error when trying to dump tablespace.

    Cause: PolarDB-X does not support tablespaces.

    Solution: Add the --no-tablespaces parameter to the mysqldump command.

  • Problem: The mysqldump command returns the error SELECT COLUMN_NAME, JSON_EXTRACT(HISTOGRAM, '$."number-of-bucket-specified"') FROM information_schema.COLUMN_STATISTICS ....

    Cause: This error occurs because of an incompatible JSON format used when querying column statistics.

    Solution: Add the --column-statistics=0 parameter to the mysqldump command to skip exporting statistics information.

  • Problem: The mysqldump command returns the error Couldn't execute 'SHOW TRIGGERS ...'.

    Cause: PolarDB-X does not support triggers.

    Solution: Add the --skip-triggers parameter to the mysqldump command.

  • Problem: When you import a table without a primary key, the error Duplicate entry '0' for key 'PRIMARY' occurs.

    Cause: The dump file sets the sql_mode to NO_AUTO_VALUE_ON_ZERO, which causes a primary key conflict.

    Solution: Then, remove the NO_AUTO_VALUE_ON_ZERO field from the sql_mode setting in the dump file.

  • Problem: Garbled characters appear when exporting data using mysqldump.

    Solution: If your instance is version 5.4.19 or later, you can use a privileged account to set COMPATIBLE_CHARSET_VARIABLES to `true`. Then, export the data again using mysqldump.

    SET GLOBAL COMPATIBLE_CHARSET_VARIABLES=true;