Error
Error Code:
1666
MariaDB Error 1666: Binary Log Format Mismatch
Description
This error indicates a conflict where MariaDB cannot write a statement to the binary log because the statement inherently requires row-based logging, but the server's `BINLOG_FORMAT` is set to `STATEMENT`. It often arises with operations that are non-deterministic or involve complex data changes best captured at the row level.
Error Message
Cannot execute statement: impossible to write to binary log since statement is in row format and BINLOG_FORMAT = STATEMENT.
Known Causes
4 known causesComplex DML Statements
Executing Data Manipulation Language (DML) statements that involve complex logic, subqueries, or operations that are not easily represented as simple SQL statements in the binary log.
Non-Deterministic Functions
Using functions like `UUID()`, `RAND()`, or other non-deterministic functions within DML statements, which MariaDB often prefers to log in row format for consistency.
`INSERT ... SELECT` with Specific Conditions
Certain `INSERT ... SELECT` operations, particularly those involving `ORDER BY`, `LIMIT`, or complex expressions, can be deemed unsafe for `STATEMENT` logging.
Triggers or Stored Procedures
DML operations initiated within triggers or complex stored procedures might internally generate changes that are best represented in a row-based format.
Solutions
3 solutions available1. Temporarily Set BINLOG_FORMAT to ROW easy
Quickly resolve the error by changing the binary log format for the current session.
1
Connect to your MariaDB server using a client.
2
Execute the following SQL statement to change the binary log format for the current session. This will allow the statement to be written to the binary log.
SET SESSION binlog_format = 'ROW';
3
Now, re-execute the statement that caused the error. It should now be written to the binary log without issue.
4
After successfully executing your statement, you can optionally reset the session's binary log format back to its original value (if it was STATEMENT). However, this is often not necessary unless you have specific replication or auditing requirements that mandate STATEMENT format for all operations.
SET SESSION binlog_format = 'STATEMENT';
2. Permanently Change BINLOG_FORMAT to ROW medium
Configure MariaDB to use ROW format for binary logging globally.
1
Locate your MariaDB configuration file. This is typically named `my.cnf` or `my.ini` and is usually found in `/etc/mysql/`, `/etc/`, or within the MariaDB installation directory.
2
Edit the configuration file using a text editor (e.g., `nano`, `vim`). Add or modify the `binlog_format` setting under the `[mysqld]` section.
[mysqld]
binlog_format = ROW
3
Save the changes to the configuration file.
4
Restart the MariaDB service for the changes to take effect.
# For systems using systemd:
sudo systemctl restart mariadb
# For systems using init.d:
sudo service mysql restart or sudo /etc/init.d/mysql restart
5
Verify the change by connecting to MariaDB and checking the global variable.
SHOW GLOBAL VARIABLES LIKE 'binlog_format';
3. Rewrite the Statement for STATEMENT Format Compatibility advanced
Modify the problematic SQL statement to be compatible with STATEMENT-based binary logging.
1
Identify the specific SQL statement that is causing the error. This statement is likely using functions or constructs that are not deterministically safe for STATEMENT-based replication (e.g., `UUID()`, `NOW()`, functions that depend on the order of execution or system state).
2
Analyze the problematic statement and determine why it's incompatible with `BINLOG_FORMAT = STATEMENT`. This often involves looking for non-deterministic functions or operations.
3
Rewrite the statement to be deterministic. This might involve:
4
Replacing non-deterministic functions with deterministic alternatives. For example, instead of `NOW()`, you might pass the current timestamp as a parameter or use a pre-determined value.
5
Ensuring that any operations that rely on row order are made explicit and deterministic.
6
If the statement involves `INSERT ... SELECT` or similar constructs, consider if a `ROW` format would be more appropriate for that specific operation (though this often leads back to changing the global `binlog_format`).
7
Test the rewritten statement thoroughly in a non-production environment to ensure it behaves as expected and is compatible with your current `BINLOG_FORMAT`.