用于MySQL配置的JDBC url使用utf8字符编码

时间:2013-08-05 12:35:22

标签: mysql jdbc encoding utf-8 collation

MySQL 5.6配置

我已手动配置/etc/mysql/my.cnf以使用utf8。见下文:

[mysqld]
character-set-server=utf8
character-sets-dir=/usr/share/mysql/charsets

[mysql]
character-sets-dir=/usr/share/mysql/charsets default-character-set=utf8

[mysqladmin]
character-sets-dir=/usr/share/mysql/charsets default-character-set=utf8

[mysqlcheck]
character-sets-dir=/usr/share/mysql/charsets default-character-set=utf8

[mysqldump]
character-sets-dir=/usr/share/mysql/charsets default-character-set=utf8

[mysqlimport]
character-sets-dir=/usr/share/mysql/charsets default-character-set=utf8

[mysqlshow]
character-sets-dir=/usr/share/mysql/charsets default-character-set=utf8

[client]
character-sets-dir=/usr/share/mysql/charsets default-character-set=utf8

来自控制台:

mysql> show variables like 'char%';
+--------------------------+----------------------------+
| Variable_name            | Value                      |
+--------------------------+----------------------------+
| character_set_client     | utf8                       |
| character_set_connection | utf8                       |
| character_set_database   | utf8                       |
| character_set_filesystem | binary                     |
| character_set_results    | utf8                       |
| character_set_server     | utf8                       |
| character_set_system     | utf8                       |
| character_sets_dir       | /usr/share/mysql/charsets/ |
+--------------------------+----------------------------+
8 rows in set (0.01 sec)

应用程序配置

现在对于web-app我在Tomcat的context.xml

中使用了这个连接url
url="jdbc:mysql://localhost:3306/test?autoReconnect=true&useUnicode=true&characterEncoding=UTF-8"

url="jdbc:mysql://localhost:3306/test?autoReconnect=true&useUnicode=true&characterEncoding=UTF-8&characterSetResults=utf8&connectionCollation=utf8_general_ci"

上面不起作用,让我关注申请。

show variables like 'char%';
+--------------------------+----------------------------+
| Variable_name            | Value                      |
+--------------------------+----------------------------+
| character_set_client     | utf8mb4                    |
| character_set_connection | utf8                       |
| 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/ |
+--------------------------+----------------------------+

这里可能有什么问题?为什么显示utf8mb4代替utf8

2 个答案:

答案 0 :(得分:16)

您可能需要执行以下操作:

Changes in MySQL Connector/J 5.1.13 (2010-06-24)

  • Connector / J不支持服务器5.5.2及更新版本的utf8mb4。

    Connector / J现在自动检测使用character_set_server = utf8mb4配置的服务器,或者在SET NAMES =在建立连接时进行的调用中使用characterEncoding = ...作为utf8mb4处理传递的Java编码utf-8。 (Bug#54175)

答案 1 :(得分:0)

对于您的网络应用,请尝试使用characterEncoding = utf8而不是UTF-8。那解决了我的SonarQube容器问题。