General error: 1366 Incorrect string value: '\xF0\x9F\x98' for column 'content'
2022-06-15 加入收藏
比如类似这样报错:
SQLSTATE[HY000]: General error: 1366 Incorrect string value: 'xF0xA7x92xADxEFxBC...' for column 'content' at row 1
请参考一下教程:
问题分析:
在mysql 5.5 之前,UTF-8编码只支持1-3个字节,支持BMP这部分的Unicode编码区;从MySQL 5.5开始,可以支持4个字节UTF编码 utf8mb4 ,一个字符能够支持更多的字符集,也能够支持更多表情符号。
utf8mb4兼容utf8,且比utf8能表示更多的字符,是utf8字符集的超集。所以现在一些新的业务,比如ISO等,会将MySQL数据库的字符集设置为utf8mb4。
解决方案:
1、将mysql版本升级为5.7版本
2、到数据库,把数据库字符集修改为:utf8mb4_general_ci
3、将数据表的 content 字段类型设置为:LONGTEXT、排序规则设置为:utf8mb4_general_ci
4、继续在MySQL数据库的my.cnf中关于字符集的配置修改为下面的配置了:
[client]
default-character-set=utf8mb4
[mysqld]
character-set-server = utf8mb4
collation-server = utf8mb4_unicode_ci
init_connect='SET NAMES utf8mb4'
skip-character-set-client-handshake = true
[mysql]
default-character-set = utf8mb4
宝塔中的配置文件路径(宝塔下安装的MySQL,my.cnf路径:根目录>etc):
也可以直接在宝塔MySQL 的配置文件中修改:
修改后,其内容为:
[client]
#password = your_password
port = 3306
socket = /tmp/mysql.sock
default-character-set=utf8mb4 # 这是我们添加的
[mysqld]
port = 3306
socket = /tmp/mysql.sock
datadir = /www/server/data
default_storage_engine = InnoDB
performance_schema_max_table_instances = 400
table_definition_cache = 400
skip-external-locking
key_buffer_size = 32M
max_allowed_packet = 100G
table_open_cache = 128
sort_buffer_size = 768K
net_buffer_length = 4K
read_buffer_size = 768K
read_rnd_buffer_size = 256K
myisam_sort_buffer_size = 8M
thread_cache_size = 16
query_cache_size = 16M
tmp_table_size = 32M
sql-mode=NO_ENGINE_SUBSTITUTION,STRICT_TRANS_TABLES
explicit_defaults_for_timestamp = true
#skip-name-resolve
max_connections = 500
max_connect_errors = 100
open_files_limit = 65535
log-bin=mysql-bin
binlog_format=mixed
server-id = 1
expire_logs_days = 10
slow_query_log=1
slow-query-log-file=/www/server/data/mysql-slow.log
long_query_time=3
#log_queries_not_using_indexes=on
early-plugin-load = ""
innodb_data_home_dir = /www/server/data
innodb_data_file_path = ibdata1:10M:autoextend
innodb_log_group_home_dir = /www/server/data
innodb_buffer_pool_size = 128M
innodb_log_file_size = 64M
innodb_log_buffer_size = 16M
innodb_flush_log_at_trx_commit = 1
innodb_lock_wait_timeout = 50
innodb_max_dirty_pages_pct = 90
innodb_read_io_threads = 2
innodb_write_io_threads = 2
character-set-server = utf8mb4 # 这是我们添加的
collation-server = utf8mb4_unicode_ci # 这是我们添加的
init_connect='SET NAMES utf8mb4' # 这是我们添加的
skip-character-set-client-handshake = true # 这是我们添加的
[mysqldump]
quick
max_allowed_packet = 500M
[mysql]
no-auto-rehash
default-character-set = utf8mb4 # 这是我们添加的
[myisamchk]
key_buffer_size = 32M
sort_buffer_size = 768K
read_buffer = 2M
write_buffer = 2M
[mysqlhotcopy]
interactive-timeout
5、修改完之后,重载MySQL配置,并重启MySQL服务,即可存入输入法自带的表情了!