Enable binary logging

更新时间:
复制 MD 格式

This topic describes how to enable the binary logging feature for a PolarDB cluster.

Background information

PolarDB is a cloud-native database that is fully compatible with MySQL. By default, PolarDB uses advanced physical logs instead of binary logs. For better integration with the MySQL ecosystem, PolarDB lets you enable binary logging manually. After you enable binary logging, you can connect to data products such as Elasticsearch and AnalyticDB. You can also set up real-time data synchronization from PolarDB to RDS, from RDS to PolarDB, or between PolarDB clusters.

Limits

  • If your PolarDB cluster was created on or after April 5, 2019, you can enable binary logging directly. If your PolarDB cluster was created before April 5, 2019, you must upgrade the cluster's minor version to the latest version before you can enable binary logging. For more information about how to upgrade the minor version, see Version management.

Billing

Storage for binary logs is part of the cluster's storage space and is subject to storage fees. You can manage the storage space used by binary log files by setting the binary log retention period.

Billing examples

Subscription

If your cluster uses the subscription storage type and has enough storage space, no extra fees are charged.

image

Pay-as-you-go

If your cluster uses the pay-as-you-go storage type, fees are charged based on the actual storage space that the binary logs occupy.

image

Usage notes

  • Binary logging is disabled by default. After you enable binary logging, the cluster automatically restarts. The restart task usually completes within 5 minutes. During the restart, a transient disconnection of about 40 seconds occurs. The exact duration depends on the data volume and the number of tables. Perform this operation during off-peak hours and ensure your application has a reconnection mechanism.

  • Enabling binary logging reduces write performance but does not affect read performance. Typically, the performance impact is less than 10%. Under extreme stress, the performance impact can reach 40%. For more information, see FAQ.

  • When you pull, subscribe to, or synchronize binary logs, for example, using a tool such as DTS, use the Primary Endpoint of the PolarDB cluster. The primary endpoint points directly to the primary node that generates binary logs, which provides better compatibility and stability. For more information about how to view the Primary Endpoint, see View connection addresses and ports.

  • After you enable binary logging, committing a large transaction can block other transactions from committing. This may also affect the duration of cluster restarts and specification changes.

  • The loose_polar_log_bin parameter described in this topic is a global-level parameter. To use the session-level binary logging feature, you must enable the sql_log_bin parameter.

    Note

    The sql_log_bin parameter controls whether to enable session-level binary logging. By default, this parameter is disabled. To enable it, go to the Quota Center page. Find the polardb sql_log_bin parameter permissions quota and click Apply in the Actions column.

  • If you use DTS to migrate data from RDS to PolarDB, binary logging is automatically enabled.

Enable binary logging

Enable binary logging when you create a cluster

When you create a cluster, on the Cluster Configuration page, select the Enable Binlog check box. For more information, see Create a pay-as-you-go cluster and Create a subscription cluster.

Enable binary logging for an existing cluster

Important

After you enable binary logging, the cluster automatically restarts. The restart task usually completes within 5 minutes. During the restart, a transient disconnection of about 40 seconds occurs. The exact duration depends on the data volume and the number of tables. Perform this operation during off-peak hours and ensure your application has a reconnection mechanism.

  1. Log on to the PolarDB console, click Clusters in the navigation pane on the left, select the Region where the cluster is deployed, and then click the cluster ID to open the cluster details page.

  2. You can enable binary logging in one of the following ways:

    • Method 1:

      1. Go to the Binlog Management page of the cluster.

        image

      2. Click the Enable Now button.

      3. In the Enable Binlog dialog box, set the effective time to Effective Immediately or Scheduled Time.

        If you select Scheduled Time, specify the effective time.

        image

      4. Click OK.

    • Method 2: Enable binary logging by setting the value of the loose_polar_log_bin parameter.

      1. In the navigation pane on the left, choose Settings and Management > Parameters.

      2. Find the loose_polar_log_bin parameter and change its value. For more information, see Modify parameters.

        Note
        • If your PolarDB for MySQL cluster runs MySQL 5.6, change the current value to ON_WITH_GTID.

        • If your PolarDB for MySQL cluster runs MySQL 5.7 or MySQL 8.0, change the current value to ON.

Disable binary logging

  1. Log on to the PolarDB console, click Clusters in the navigation pane on the left, select the Region where the cluster is deployed, and then click the cluster ID to open the cluster details page.

  2. You can disable binary logging in one of the following ways:

    • Method 1:

      1. Go to the Binlog Management page of the cluster.

        image

      2. Click the Disable Binlog button.

      3. In the Disable Binlog dialog box, set the effective time to Effective Immediately or Scheduled Time.

        If you select Scheduled Time, specify the effective time.

        If needed, select the Immediately purge local binlogs check box.

        image

      4. Click OK.

    • Method 2: Disable binary logging by setting the value of the loose_polar_log_bin parameter.

      1. In the navigation pane on the left, choose Settings and Management > Parameters.

      2. Find the loose_polar_log_bin parameter and change its value. For more information, see Modify parameters.

        Note
        • If your PolarDB for MySQL cluster runs MySQL 5.6, change the current value to OFF_WITH_GTID.

        • If your PolarDB for MySQL cluster runs MySQL 5.7 or MySQL 8.0, change the current value to OFF.

        • After you disable binary logging, existing binary log files are retained permanently. If you want to delete them, shorten the retention period and wait for the files to be automatically deleted before you disable binary logging.

Binary log file retention period

Retention policies

The retention policies for binary log files are as follows:

  • After you enable binary logging, files are retained for 3 days by default. Binary log files older than 3 days are automatically deleted.

    Note
    • For PolarDB for MySQL clusters purchased before November 23, 2023, binary log files are retained for two weeks (14 days) by default.

    • For PolarDB for MySQL clusters purchased before January 17, 2024, binary log files are retained for one week (7 days) by default.

  • After you disable binary logging, existing binary log files are retained permanently and are not automatically deleted.

Modify the retention period

Important
  • Modifying the binary log retention period does not cause transient disconnections or require a cluster restart.

  • However, if modifying the retention period results in a large volume of binary log files (for example, 10 TB) being purged, short-term database write exceptions may occur. Therefore, if a large volume of binary log files exists, perform this operation during off-peak hours. Shorten the retention period in multiple steps to purge a portion of the binary log data each time.

  • Purged binary log files cannot be recovered.

  • If binary logging is enabled for your cluster, you can modify the binary log file retention period in one of the following ways:

    • If the cluster is a PolarDB for MySQL 5.6 cluster, you can set the binary log retention period by modifying the value of the loose_expire_logs_hours parameter. The value is in hours, the value range is 0 to 2,376, and the default value is 72. A value of 0 indicates that binary log files are not automatically deleted. For more information about parameter settings, see Modify parameters.

    • If the cluster is a PolarDB for MySQL 5.7 or 8.0 cluster, you can set the binary log retention period by modifying the value of the binlog_expire_logs_seconds parameter. The value is in seconds, the value range is 0 to 4,294,967,295, and the default value is 259,200. A value of 0 indicates that binary log files are not automatically deleted. For more information about parameter settings, see Modify parameters.

      Important

      After you set the binary log retention period by modifying these two parameters, historical binary log files in the cluster are not immediately purged. To purge the historical binary log files at this time, you can use one of the following three methods:

      • When the last binary log file in the cluster reaches the size specified by max_binlog_size and the system switches to a new binary log file, the historical binary log files are automatically purged.

      • Use a privileged account to execute the flush binary logs command to immediately trigger a binary log file switch and purge expired binary log files.

      • You can also restart the cluster. After the cluster restarts, historical binary log files are automatically purged.

  • To delete binary log files from a cluster where binary logging is disabled, you can enable binary logging, set the binary log retention period parameter (loose_expire_logs_hours or binlog_expire_logs_seconds) to a small value, wait for the files to be automatically deleted upon expiration, and then disable binary logging.

Obtain and view binary logs

Use the mysqlbinlog tool to view and parse binary logs. For more information, see Remotely obtain and parse the binary logs of a PolarDB for MySQL cluster.

FAQ

  • Q: How long are binary log files retained?

    A: If binary logging is enabled for your cluster, binary log files are retained for 3 days by default. Binary log files older than 3 days are automatically deleted.

    Note
    • For PolarDB for MySQL clusters purchased before November 23, 2023, binary log files are retained for two weeks (14 days) by default.

    • For PolarDB for MySQL clusters purchased before January 17, 2024, binary log files are retained for one week (7 days) by default.

    If binary logging is disabled for your cluster, existing binary log files are retained permanently and are not automatically deleted.

    Note

    You can modify parameters to change the binary log file retention period for a cluster where binary logging is enabled. For more information, see Binary log file retention period.

  • Q: What does the error message Could not find first log file name in binary log index file mean?

    A: The error message Could not find first log file name in binary log index file indicates that the binary log has been purged. Purged binary logs cannot be recovered.

  • Q: Can I disable binary logging after it is enabled?

    A: Set the loose_polar_log_bin parameter to OFF and submit.

    Note

    After you disable binary logging, existing binary log files are retained permanently. If you want to delete them, shorten the retention period and wait for the files to be automatically deleted before you disable binary logging.

  • Q: How do I reduce the storage space occupied by binary logs?

    A: You can set the binary log retention period parameter (loose_expire_logs_hours or binlog_expire_logs_seconds) to a smaller value to reduce the storage space occupied by binary log files.

  • Q: How much does enabling binary logging affect performance?

    A: Enabling binary logging does not affect query (SELECT) performance. It only affects the performance of write and update operations, such as INSERT, UPDATE, and DELETE. Typically, the performance impact is less than 10%. Under extreme stress, the performance impact can reach 40%.

  • Q: After binary logging is enabled, the cluster restarts automatically. How long does the restart task take to complete?

    A: Generally, the restart task completes within 5 minutes. During the restart, a transient disconnection of about 40 seconds occurs. The exact duration depends on the data volume and the number of tables. Perform this operation during off-peak hours and ensure your application has a reconnection mechanism.

  • Q: How do I remotely obtain and view the content of binary logs?

    A: For more information, see Remotely obtain and parse the binary logs of a PolarDB for MySQL cluster.

  • Q: Why can't I use the DMS feature Perform lock-free schema changes using lock-free change tickets to perform table schema changes, such as adding an index, on a PolarDB for MySQL cluster?

    A: Binary logging is disabled by default for PolarDB for MySQL clusters. Therefore, to use the lock-free schema change feature of DMS, you must first enable binary logging for the PolarDB for MySQL cluster. If you do not want to enable binary logging, you can also use online DDL commands to change the table schema.

  • Q: After I enable binary logging, can I use Canal to collect changes made to MySQL?

    A: Yes. Canal can capture changes from MySQL.

  • Q: Does executing the SHOW BINARY LOGS statement to query the size of binary log files affect cluster performance?

    A: No, it does not. The SHOW BINARY LOGS statement is a management operation and has a negligible impact on cluster performance. It does not directly affect the read and write performance of the database, unlike data write operations such as INSERT, UPDATE, and DELETE. Therefore, you can use this command for queries without worrying about performance degradation.

  • Q: How do I query the last write time of a binary log in PolarDB for MySQL?

    A: Use the show full binary logs statement to view the name, size, and last write time of all binary log files.

    Note

    This feature is supported on PolarDB for MySQL clusters that meet the following requirements:

    • MySQL 8.0.2, and the minor version must be 8.0.2.2.0 or later.

    • MySQL 8.0.1, and the minor version must be 8.0.1.1.14 or later.

    • MySQL 5.7, and the minor version must be 5.7.1.0.27 or later.

    • MySQL 5.6, and the minor version must be 5.6.1.0.38 or later.

  • Q: How do I view the content of a binary log at a specific time in PolarDB for MySQL?

    A: You cannot directly view the content of a binary log at a specific time. Instead, you can perform a point-in-time recovery and then parse the binary log to view its content.