READ COMMITTED ERROR 1665 无法写入二进制日志

READ COMMITTED ERROR 1665 impossible to write to binary log

这个例子有效:

MariaDB [test]> SET TRANSACTION  ISOLATION LEVEL REPEATABLE READ;
Query OK, 0 rows affected (0.00 sec)

MariaDB [test]> INSERT INTO test(id, value) VALUES (1, 'test'), (2, 'test 2');
Query OK, 2 rows affected (0.37 sec)
Records: 2  Duplicates: 0  Warnings: 0

MariaDB [test]> SELECT * FROM test;
+------+--------+
| id   | value  |
+------+--------+
|    1 | test   |
|    2 | test 2 |
+------+--------+
2 rows in set (0.00 sec)

MariaDB [test]> COMMIT;

但是当我更改交易级别时:

MariaDB [test]> SET TRANSACTION  ISOLATION LEVEL READ COMMITTED;
Query OK, 0 rows affected (0.00 sec)

MariaDB [test]> INSERT INTO test(id, value) VALUES (1, 'test'), (2, 'test 2');
ERROR 1665 (HY000): Cannot execute statement: impossible to write to binary log
since BINLOG_FORMAT = STATEMENT and at least one table uses a storage engine lim
ited to row-based logging. InnoDB is limited to row-logging when transaction iso
lation level is READ COMMITTED or READ UNCOMMITTED.
MariaDB [test]> SELECT * FROM test;
Empty set (0.00 sec)

MariaDB [test]> COMMIT;
Query OK, 0 rows affected (0.00 sec)

如何解决这个问题?

在这种情况下,错误消息非常详细。根据 mysql 关于 binlog_format 的文档:

If you are using InnoDB tables and the transaction isolation level is READ COMMITTED or READ UNCOMMITTED, only row-based logging can be used. It is possible to change the logging format to STATEMENT, but doing so at runtime leads very rapidly to errors because InnoDB can no longer perform inserts.

因此,您必须将 binlog_format 设置为 ROW:

SET SESSION binlog_format = 'ROW';