Use mysqlbinlog to remotely download binary log files from a PolarDB for MySQL cluster and parse them locally for data auditing, point-in-time recovery, or replication troubleshooting.
Prerequisites
Before you begin, ensure that you have:
Enabled binary logging for the PolarDB for MySQL cluster. Binary logging is disabled by default. Without it,
SHOW BINARY LOGS;returnsYou are not using binary logging. Enabling or disabling binary logging triggers an automatic cluster restart — perform this change during off-peak hours and make sure your applications can reconnect automatically.A MySQL client installed on your local server.
The cluster endpoint (private or public). See Manage the endpoints of a cluster.
A database account for the PolarDB for MySQL cluster. See Create and manage a database account.
How it works
The node that serves binary logs depends on which endpoint you connect to:
| Connection method | Binary logs served from |
|---|---|
| Primary endpoint, default cluster endpoint, or custom cluster endpoint associated with the primary node | Primary node |
| Custom cluster endpoint associated only with read-only nodes | One of the read-only nodes |
Downloading binary logs from a read-only node requires:
PolarDB for MySQL 8.0, revision version 8.0.1.1.12 or later
PolarDB for MySQL 5.7, revision version 5.7.1.0.12 or later
If the version requirement is not met, mysqlbinlog returns Only allow to dump binary log file on primary instance. See Minor version update to upgrade.
Download binary log files
Connect to the PolarDB cluster from the MySQL client. See Database connection.

List the available binary log files:
SHOW BINARY LOGS;The output lists each log file name and size:
+------------------+-----------+ | Log_name | File_size | +------------------+-----------+ | mysql-bin.000005 | 2639 | +------------------+-----------+ 1 row in set (0.00 sec)Note the
Log_namevalue — use it in the next step.Exit the MySQL client:
exitRun mysqlbinlog to download the binary log file to your local server: Command format:
Parameters:
Parameter Description --read-from-remote-serverRequired. Connects to the cluster as a replication client to stream binary logs remotely. --host/-hThe private or public endpoint of the cluster. Use a private endpoint if the cluster and your server are in the same region and virtual private cloud (VPC); otherwise, use a public endpoint. If the endpoint uses a non-default port, append the port number after the endpoint. --user/-uThe database account name of the PolarDB for MySQL cluster. --password/-pThe account password. Omit the value to enter it interactively after running the command. --rawSaves the binary log in its original binary format instead of converting it to text. mysql-bin.<log-number>The name of the binary log file from the Log_namefield in step 2.mysqlbinlog -u<Username> -p<Password> -h<Endpoint> --read-from-remote-server --raw mysql-bin.******Example:
mysqlbinlog -utest_api -p -htest-polardb.rwlb.rds.aliyuncs.com --read-from-remote-server --raw mysql-bin.000005
View and parse binary log files
Run mysqlbinlog on the downloaded file to decode and view its contents:
mysqlbinlog -vv --base64-output=decode-rows mysql-bin.<log-number> | more-vv: shows detailed SQL statements and row-level changes--base64-output=decode-rows: decodes base64-encoded row events into readable SQL
The output shows the decoded contents of each event in the binary log file:

Use the same MySQL version as the PolarDB cluster's database engine version to avoid parsing errors.
For full mysqlbinlog syntax and options, see the MySQL documentation.
FAQ
Why does SHOW BINARY LOGS return "You are not using binary logging"?
Binary logging is disabled by default. Enable binary logging for the cluster and try again.
Enabling or disabling binary logging triggers an automatic cluster restart.
Why does mysqlbinlog fail with a sanity check or checksum error?
ERROR: Error in Log_event::read_log_event(): 'Sanity check failed', data_len: 151, event_type: 35
ERROR: Could not read entry at offset 120: Error in log format or read error.
ERROR: Got error reading packet from server: 'Slave can not handle replication events with the checksum that master is configured to log;Your mysqlbinlog version is too old. Upgrade to a newer version — for example, if you are on V3.3, upgrade to V3.4.
Why does mysqlbinlog fail with "Lost connection to MySQL server"?
ERROR: Failed on connect: Lost connection to MySQL server at 'reading initial communication packet', system error: 104Check the following:
Network connectivity: Verify you can reach the cluster endpoint from your server.
IP whitelist: Make sure your server's IP address is added to the cluster's whitelist. See Configure an IP whitelist.
Database settings: Check whether your local MySQL server can be connected to databases and ensure you can successfully access the databases.
Why are the binary log file contents not decoded?
If you run mysqlbinlog without --base64-output=decode-rows, row events are displayed as raw base64 strings:

Always include --base64-output=decode-rows when viewing binary log files.
Why does "Only allow to dump binary log file on primary instance" appear when connecting to a read-only node?
Your cluster version does not meet the minimum requirement for read-only node binary log access. Upgrade to PolarDB for MySQL 8.0 revision 8.0.1.1.12 or later, or PolarDB for MySQL 5.7 revision 5.7.1.0.12 or later. See Minor version update.
Why are binary logs from a read-only node sometimes behind the primary node?
The primary node replicates binary log files to read-only nodes asynchronously. If a sync latency occurs, read-only nodes may not yet have the most recently written log files. This is typically brief when the data volume is small.
Why is an expired binary log file not deleted after the binlog_expire_logs_seconds retention period?
If a read-only node thread is actively reading the file when it expires, the system holds it until the read completes. After the primary node writes a new binary log file, the system checks for expired files that are no longer being read and deletes them.