日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問(wèn) 生活随笔!

生活随笔

當(dāng)前位置: 首頁(yè) > 运维知识 > 数据库 >内容正文

数据库

mysql characterencoding utf8mb4_Mysql UTF-8mb4字符集的问题

發(fā)布時(shí)間:2023/12/8 数据库 29 豆豆
生活随笔 收集整理的這篇文章主要介紹了 mysql characterencoding utf8mb4_Mysql UTF-8mb4字符集的问题 小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

官方Mysql手冊(cè)鏈接

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.

文檔說(shuō)的很清楚

提示

mysql-connector-java 版本在8.0.12之前的,包括8.0.12,服務(wù)端必須設(shè)置character_set_server=utf8mb4;如果不是的話,就算設(shè)置了characterEncoding=UTF-8,照樣會(huì)被設(shè)置為MYSQL的 utf8字符集,也就是utf8mb3。

對(duì)于8.0.13和以后的版本,如果設(shè)置了characterEncoding=UTF-8,他會(huì)映射到MYSQL的utf8mb4字符集。

如果connectionCollation 也和characterEncoding一起設(shè)置了,但是不兼容,characterEncoding會(huì)被connectionCollation的設(shè)置覆蓋掉。

由于沒(méi)有Java-Style的utfmb3對(duì)應(yīng)的字符集名稱(chēng)可以用在connection選項(xiàng)charaterEncoding上,唯一的設(shè)置utf8mb3的方式就是在連接選項(xiàng)設(shè)置utf8mb3 collation(例如utf8_general_ci),這會(huì)強(qiáng)制使用utf8mb3字符集,正如上文所述。

警告

不要通過(guò)Connector發(fā)起SET NAMES指令,因?yàn)閐river不會(huì)檢測(cè)字符集是不是被查詢語(yǔ)句改動(dòng),并且當(dāng)連接第一次建立之后,會(huì)繼續(xù)使用當(dāng)時(shí)的字符集設(shè)置。

結(jié)論

對(duì)于網(wǎng)上的設(shè)置:

純屬扯淡。。

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

也是扯淡,

characterEncoding 要設(shè)置 為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

正確解決方法

改服務(wù)器配置吧,或者升級(jí)mysql-connector-java 到 8.0.13以后吧

測(cè)試情況

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

這樣寫(xiě)不報(bào)錯(cuò),但是無(wú)法正常存儲(chǔ)。

另外,版本在5.1.13以后的支持自動(dòng)檢測(cè)服務(wù)器設(shè)置,或者指定characterEncoding=utf-8。

但是我自己測(cè)試的結(jié)果就是 5.1.38 不寫(xiě)connectionCollation的情況下,指定utf-8也報(bào)錯(cuò)。

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)

即便寫(xiě)了connectionCollation,Mysql也不能正確存儲(chǔ)。

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//這個(gè)是emoji的hex值,通過(guò)navicat插入的。就可以。

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

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

| hex(content) |

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

| 3C703E3F3F3F3F3C2F703E |

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

1 row in set (0.04 sec)

#3F3F3F3F//這個(gè)是通過(guò)jdbc插入的,看樣子是無(wú)法正確存儲(chǔ)了。

總結(jié)

以上是生活随笔為你收集整理的mysql characterencoding utf8mb4_Mysql UTF-8mb4字符集的问题的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。

如果覺(jué)得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。