我正在尝试将utf8mb4字符串正确存储到MySQL 5.5.30中。我们正在使用ConnectorJ 5.1.18。
根据文档,ConnectorJ应根据character_set_server变量自动检测字符编码......
但是,尽管我可以告诉它总是默认为
SET NAMES latin1而不是SET NAMES utf8mb4
连接器-j探查器的日志输出:
Sat Jul 06 15:45:20 CDT 2013 INFO: Profiler Event: [QUERY] at java.sql.DriverManager.getConnection(DriverManager.java:579) duration: 1 ms, connection-id: 57, statement-id: 3, resultset-id: 4, message: /* mysql-connector-java-5.1.18 ( Revision: tonci.grgin@oracle.com-20110930151701-jfj14ddfq48ifkfq ) */SHOW VARIABLES WHERE Variable_name ='language' OR Variable_name = 'net_write_timeout' OR Variable_name = 'interactive_timeout' OR Variable_name = 'wait_timeout' OR Variable_name = 'character_set_client' OR Variable_name = 'character_set_connection' OR Variable_name = 'character_set' OR Variable_name = 'character_set_server' OR Variable_name = 'tx_isolation' OR Variable_name = 'transaction_isolation' OR Variable_name = 'character_set_results' OR Variable_name = 'timezone' OR Variable_name = 'time_zone' OR Variable_name = 'system_time_zone' OR Variable_name = 'lower_case_table_names' OR Variable_name = 'max_allowed_packet' OR Variable_name = 'net_buffer_length' OR Variable_name = 'sql_mode' OR Variable_name = 'query_cache_type' OR Variable_name = 'query_cache_size' OR Variable_name = 'init_connect' Sat Jul 06 15:45:20 CDT 2013 INFO: Profiler Event: [FETCH] at java.sql.DriverManager.getConnection(DriverManager.java:579) duration: 0 ms, connection-id: 57, statement-id: 3, resultset-id: 4 Sat Jul 06 15:45:20 CDT 2013 INFO: Profiler Event: [QUERY] at java.sql.DriverManager.getConnection(DriverManager.java:579) duration: 0 ms, connection-id: 57, statement-id: 3, resultset-id: 5, message: /* mysql-connector-java-5.1.18 ( Revision: tonci.grgin@oracle.com-20110930151701-jfj14ddfq48ifkfq ) */SELECT @@session.auto_increment_increment Sat Jul 06 15:45:20 CDT 2013 INFO: Profiler Event: [FETCH] at java.sql.DriverManager.getConnection(DriverManager.java:579) duration: 0 ms, connection-id: 57, statement-id: 3, resultset-id: 5 Sat Jul 06 15:45:20 CDT 2013 INFO: Profiler Event: [QUERY] at java.sql.DriverManager.getConnection(DriverManager.java:579) duration: 0 ms, connection-id: 57, statement-id: 4, resultset-id: 6, message: SHOW COLLATION Sat Jul 06 15:45:20 CDT 2013 INFO: Profiler Event: [FETCH] at java.sql.DriverManager.getConnection(DriverManager.java:579) duration: 3 ms, connection-id: 57, statement-id: 4, resultset-id: 6 Sat Jul 06 15:45:20 CDT 2013 INFO: Profiler Event: [QUERY] at java.sql.DriverManager.getConnection(DriverManager.java:579) duration: 1 ms, connection-id: 57, statement-id: 999, resultset-id: 0, message: SET NAMES latin1
SHOW VARIABLES调用的输出如下:
character_set_client utf8 character_set_connection utf8 character_set_results utf8 character_set_server utf8mb4
服务器当前正在运行,因此要更新character_set_server值,我只需运行
SET GLOBAL和
SET语句。
更新: 当我更改my.cnf中的character_set_server值并重新启动我的服务器时,ConnectorJ完全按照预期检测到utf8mb4。
当我使用SET GLOBAL手动设置值时,ConnectorJ继续使用Latin1。
有人知道为什么会这样吗?有没有办法以ConnectorJ检测的方式更新字符集而不必使mysql脱机?
答案 0 :(得分:1)
我有类似的问题。过去,ConnectorJ的客户端字符集握手过去常常使用常规/不完整utf8
字符集而不是utf8mb4
https://dev.mysql.com/doc/relnotes/connector-j/en/news-5-1-13.html
修复自5.1.12版以来发现的错误。 Connector / J现在自动检测使用character_set_server = utf8mb4配置的服务器,或者在SET NAMES =在建立连接时进行的调用中使用characterEncoding = ...作为utf8mb4处理传递的Java编码utf-8。 (Bug#54175)
但是,升级ConnectorJ并没有解决任何问题。顺便说一句,要知道更改配置文件修复了问题,而在运行时更改值则没有。这为我节省了很多时间。
对于任何因任何原因无法重启服务器的问题。我建议(当你想使用utf8mb4时,与连接字符串中省略characterEncoding
的MySQL文档相反)将characterEncoding=UTF-8
添加到连接字符串并在其上执行手册SET NAMES utf8mb4
连接你遇到了麻烦。