当查询包含 4 字节 UTF-8 字符时,我在使用记录对表执行的所有查询的数据库触发器时遇到问题。
My mysql.cnf https://pastebin.com/3cix3GzZ
Ubuntu 16.04 上的 mysql 版本为 5.7.19
示例脚本:
show variables where Variable_name like 'character\_set\_%' or Variable_name like 'collation%';
drop database if exists my_test_db;
create database my_test_db;
use my_test_db;
create table my_test_table(id int not null primary key auto_increment, jdoc json not null);
create table my_test_table_log(id int not null primary key auto_increment, query varchar(1024) NOT NULL);
SELECT "insert works when trigger is not active" as "";
insert into my_test_table(jdoc) VALUES(JSON_OBJECT("Dubai was", "????"));
DELIMITER |
CREATE TRIGGER log_my_test_table_queries_insert
BEFORE INSERT ON `my_test_table`
FOR EACH ROW
BEGIN
DECLARE original_query VARCHAR(1024);
SET original_query = (SELECT info
FROM INFORMATION_SCHEMA.PROCESSLIST
WHERE id = CONNECTION_ID());
INSERT INTO `my_test_table_log` (`query`) VALUES (original_query);
END;
|
DELIMITER ;
SELECT "insert doesn't work when trigger is active" as "";
insert into my_test_table(jdoc) VALUES(JSON_OBJECT("Dubai was", "????"));
我的输出:
+--------------------------+--------------------+
| Variable_name | Value |
+--------------------------+--------------------+
| character_set_client | utf8mb4 |
| character_set_connection | utf8mb4 |
| character_set_database | utf8mb4 |
| character_set_filesystem | binary |
| character_set_results | utf8mb4 |
| character_set_server | utf8mb4 |
| character_set_system | utf8 |
| collation_connection | utf8mb4_unicode_ci |
| collation_database | utf8mb4_unicode_ci |
| collation_server | utf8mb4_unicode_ci |
+--------------------------+--------------------+
10 rows in set (0.00 sec)
Query OK, 2 rows affected (0.03 sec)
Query OK, 1 row affected (0.00 sec)
Database changed
Query OK, 0 rows affected (0.03 sec)
Query OK, 0 rows affected (0.02 sec)
+-----------------------------------------+
| |
+-----------------------------------------+
| insert works when trigger is not active |
+-----------------------------------------+
1 row in set (0.00 sec)
Query OK, 1 row affected (0.00 sec)
Query OK, 0 rows affected (0.01 sec)
+--------------------------------------------+
| |
+--------------------------------------------+
| insert doesn't work when trigger is active |
+--------------------------------------------+
1 row in set (0.01 sec)
ERROR 1366 (HY000): Incorrect string value: '\xF0\x9F\x94\xA5")...' for column 'INFO' at row 1
数据库似乎是使用正确的字符集创建的
SHOW CREATE TRIGGER log_my_test_table_queries_insert;
SHOW CREATE TABLE my_test_table;
SHOW CREATE TABLE my_test_table_log;
outputs https://pastebin.com/yRP6RDSq
详细信息 详细信息 详细信息