首先,检查数据库CHARSET:
MariaDB [outdoors]> show create database outdoors;
| outdoors | CREATE DATABASE `outdoors` /*!40100 DEFAULT CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci */ |
然后检查表CHARSET:
MariaDB [outdoors]> show create table backend_comment;
| backend_comment | CREATE TABLE `backend_comment` (
`id` bigint(20) NOT NULL AUTO_INCREMENT,
`geo` varchar(10) DEFAULT NULL,
`content` varchar(250) NOT NULL,
`img` varchar(100) DEFAULT NULL,
`comment_id` int(11) DEFAULT NULL,
`create_time` datetime(6) NOT NULL,
`notify_id` bigint(20) NOT NULL,
`to_id` int(11) DEFAULT NULL,
`user_id` int(11) NOT NULL,
PRIMARY KEY (`id`),
KEY `backend_comment_notify_id_61aef760_fk_backend_notify_id` (`notify_id`),
KEY `backend_comment_to_id_4dd23479_fk_auth_user_id` (`to_id`),
KEY `backend_comment_user_id_1ab394ea_fk_auth_user_id` (`user_id`),
CONSTRAINT `backend_comment_notify_id_61aef760_fk_backend_notify_id` FOREIGN KEY (`notify_id`) REFERENCES `backend_notify` (`id`),
CONSTRAINT `backend_comment_to_id_4dd23479_fk_auth_user_id` FOREIGN KEY (`to_id`) REFERENCES `auth_user` (`id`),
CONSTRAINT `backend_comment_user_id_1ab394ea_fk_auth_user_id` FOREIGN KEY (`user_id`) REFERENCES `auth_user` (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci |
测试插入表情符号内容:
MariaDB [outdoors]> insert into backend_comment
(id, content, notify_id, user_id, create_time)
values (1, 'insert emoji test 😊', 1, 1, NOW());
错误 1366 (22007):字符串值不正确:第 1 行
列的“\xF0\x9F\x98\x8A”。outdoors
。backend_comment
content
**So what's the problem? help me please 😂**
----------
More information:
Server version: 10.11.8-MariaDB-0ubuntu0.24.04.1 Ubuntu 24.04
默认情况下,
mariadb
与
utf8mb3
一起运行,并且它不支持所有表情符号,因此当服务器将
utf8mb3
代码解释为
utf8mb4
时,会导致无效的代码。你可以:
设置每个连接的字符集,例如在 CLI 中:
mariadb -u{user} --default-character-set=utf8mb4 -p
或在服务器配置中设置字符集(请参阅