官方Mysql手册链接

Notes

For Connector/J 8.0.12 and earlier: In order to use the utf8mb4 character set for the connection, the server MUST be configured with character_set_server=utf8mb4; if that is not the case, when UTF-8 is used for characterEncoding in the connection string, it will map to the MySQL character set name utf8, which is an alias for utf8mb3.

For Connector/J 8.0.13 and later:

When UTF-8 is used for characterEncoding in the connection string, it maps to the MySQL character set name utf8mb4.

If the connection option connectionCollation is also set alongside characterEncoding and is incompatible with it, characterEncoding will be overridden with the encoding corresponding to connectionCollation.

Because there is no Java-style character set name for utfmb3 that you can use with the connection option charaterEncoding, the only way to use utf8mb3 as your connection character set is to use a utf8mb3 collation (for example, utf8_general_ci) for the connection option connectionCollation, which forces a utf8mb3 character set to be used, as explained in the last bullet.

Warning

Do not issue the query SET NAMES with Connector/J, as the driver will not detect that the character set has been changed by the query, and will continue to use the character set configured when the connection was first set up.

文档说的很清楚

提示

mysql-connector-java 版本在8.0.12之前的,包括8.0.12,服务端必须设置character_set_server=utf8mb4;如果不是的话,就算设置了characterEncoding=UTF-8,照样会被设置为MYSQL的 utf8字符集,也就是utf8mb3。

对于8.0.13和以后的版本,如果设置了characterEncoding=UTF-8,他会映射到MYSQL的utf8mb4字符集。

如果connectionCollation 也和characterEncoding一起设置了,但是不兼容,characterEncoding会被connectionCollation的设置覆盖掉。

由于没有Java-Style的utfmb3对应的字符集名称可以用在connection选项charaterEncoding上,唯一的设置utf8mb3的方式就是在连接选项设置utf8mb3 collation(例如utf8_general_ci),这会强制使用utf8mb3字符集,正如上文所述。

警告

不要通过Connector发起SET NAMES指令,因为driver不会检测字符集是不是被查询语句改动,并且当连接第一次建立之后,会继续使用当时的字符集设置。

结论

对于网上的设置:

纯属扯淡。。

jdbc:mysql://localhost:3306/dbnameuseUnicode=true&characterEncoding=utf8

也是扯淡,

characterEncoding 要设置 为UTF-8。

MySQL Character Set Name

Java-Style Character Encoding Name

For 8.0.12 and earlier: utf8

UTF-8

For 8.0.13 and later: utf8mb4

UTF-8

Java-Style的字符集是UTF-8,而不是utf8

正确解决方法

改服务器配置吧,或者升级mysql-connector-java 到 8.0.13以后吧

测试情况

jdbc:mysql://localhost:3306/dbnameuseUnicode=true&characterEncoding=utf-8&connectionCollation=utf8mb4_general_ci

这样写不报错,但是无法正常存储。

另外,版本在5.1.13以后的支持自动检测服务器设置,或者指定characterEncoding=utf-8。

但是我自己测试的结果就是 5.1.38 不写connectionCollation的情况下,指定utf-8也报错。

Connector/J now auto-detects servers configured with character_set_server=utf8mb4 or treats the Java encoding utf-8 passed using characterEncoding=... as utf8mb4 in the SET NAMES= calls it makes when establishing the connection. (Bug #54175)

即便写了connectionCollation,Mysql也不能正确存储。

mysql> SHOW VARIABLES WHERE Variable_name LIKE 'character_set_%' OR Variable_name LIKE 'collation%';

+--------------------------+----------------------------+

| Variable_name | Value |

+--------------------------+----------------------------+

| character_set_client | utf8mb4 |

| character_set_connection | utf8mb4 |

| character_set_database | utf8 |

| character_set_filesystem | binary |

| character_set_results | utf8mb4 |

| character_set_server | utf8 |

| character_set_system | utf8 |

| character_sets_dir | /usr/share/mysql/charsets/ |

| collation_connection | utf8mb4_general_ci |

| collation_database | utf8_general_ci |

| collation_server | utf8_bin |

+--------------------------+----------------------------+

11 rows in set (0.04 sec)

mysql> select hex(content) from send_message where id = 348;

+------------------------+

| hex(content) |

+------------------------+

| 3C703EF09F988A3C2F703E |

+------------------------+

1 row in set (0.04 sec)

#F09F988A//这个是emoji的hex值,通过navicat插入的。就可以。

mysql> select hex(content) from send_message where id = 349;

+------------------------+

| hex(content) |

+------------------------+

| 3C703E3F3F3F3F3C2F703E |

+------------------------+

1 row in set (0.04 sec)

#3F3F3F3F//这个是通过jdbc插入的,看样子是无法正确存储了。

mysql characterencoding utf8mb4_Mysql UTF-8mb4字符集的问题相关推荐

  1. mysql提示符详解_MySQL字符集使用详解

    查看字符集相关变量 mysql> show variables like 'character%'; +--------–+-----------+ | Variable_name | Valu ...

  2. unicode表_Python数据库操作 Mysql数据库表引擎与字符集#学习猿地

    # Mysql数据库表引擎与字符集 ![](./imgs/752951346A5F4E7EBDE362FA97107707.png) ### 1.服务器处理客户端请求 其实不论客户端进程和服务器进程是 ...

  3. 7.Mysql数据库表引擎与字符集

    7.Mysql数据库表引擎与字符集 1.服务器处理客户端请求 其实不论客户端进程和服务器进程是采用哪种方式进行通信,最后实现的效果都是:客户端进程向服务器进程发送 一段文本(MySQL语句),服务器进 ...

  4. mysql c 中文字符串_MySQL字符集中文乱码终极解决方案和mysql查询中文问题解决方法...

    开源数据库MySQL从来都是中小企业构建web应用的首选,特别是和PHP配合简直就是一 对黄金搭档,深受web开发人员的喜爱.但自从4.1以来MySQL加入了多字符集的支持,很多MySQL使用者发现中 ...

  5. 如何查看mysql默认字符集_如何找出MySQL中的默认服务器字符集?

    您可以使用系统变量character_set_server来了解MySQL中的默认服务器字符集.以下是语法-SHOW VARIABLES LIKE 'character_set_server'; 另外 ...

  6. Mysql utf8mb3 utf8mb4 与UTF8 字符集参数(character_set_system)的说明

    Mysql UTF8之utf8mb3 utf8mb4 字符集概述 字符集即是为了兼容各国的文字而做的编码,比如GB2312.GBK等,通用的编码我们用utf8. mysql(5.7及8.0)里utf8 ...

  7. MySQL数据库表引擎与字符集

    Mysql数据库表引擎与字符集 1.服务器处理客户端请求 其实不论客户端进程和服务器进程是采用哪种方式进行通信,最后实现的效果都是:客户端进程向服务器进程发送 一段文本(MySQL语句),服务器进程处 ...

  8. SQL Server 与 MySQL 中排序规则与字符集相关知识的一点总结

    字符集&&排序规则 字符集是针对不同语言的字符编码的集合,比如UTF-8字符集,GBK字符集,GB2312字符集等等,不同的字符集使用不同的规则给字符进行编码.排序规则则是在特定字符集 ...

  9. mysql 修改字符集为utf8mb4_MySQL数据库修改字符集为utf8mb4

    需要修改my.ini( my.cnf) [client] default-character-set=utf8mb4 [mysqld] character-set-client-handshake = ...

最新文章

  1. Google common_schema 2.2 documentation
  2. iPhone中的heic格式怎么查看,heic文件怎么打开
  3. android重新加载程序,从被杀死的Android应用程序中恢复
  4. 获取日期对应的财务期间
  5. 平面方程(Plane Equation)
  6. java父类调用子类方法_Java 中父类怎么调用子类的方法?
  7. 【C语言】输入一个字符串,并对字符串中的偶数位置的字符按从小到大的顺序排序,奇数位置的字符不动,输出排序后的结果...
  8. 【poj3358】消因子+BSGS 或 消因子+欧拉定理 两种方法
  9. 启动和停止数据库——停止例程
  10. python交互式shell之jupyter notebook初步安装使用
  11. PC版免费京东全民营业自动化做任务脚本(多号版)
  12. 如何用mysql命令 复制表_mysql中用命令行复制表结构的方法
  13. 什么是互质数(或互素数) ?
  14. 纯纯的爬虫知识,python scrapy 下载中间件知多少
  15. IMX6ULL 串口5修改
  16. vs2017u5 exists, deleting.
  17. 物联网控制APP入门专题(五)---使用android studio直接编写物联网控制APP
  18. 基于PMOS的过压保护(OVP)电路仿真
  19. fedora nginx php,在Fedora 24服务器和工作站上使用MariaDB和PHP / PHP-FPM设置Nginx
  20. 剑指 Offer 46. 把数字翻译成字符串

热门文章

  1. SpringCloud Stream MQ生产和消费同应用造成的同步问题
  2. 10个精选一线互联网架构思考与实践
  3. 看了这个,让你彻底明白什么网关、路由、DNS解析等
  4. python web编程之django post请求
  5. “一52岁程序员老将”,看完这“208道精品面试题”;最终成功跳槽阿里P8,从此平步青云,飞黄腾达
  6. 计算机设备的快捷命令,快速打开设备管理器的快捷键教程
  7. biti_rainy的博客
  8. python 用tkinter 做一个简易番茄钟
  9. C语言 扑克牌小游戏
  10. WCP人员管理之添加人员