在Linux上将MySQL字符集更改为UTF8,使其可与JDBC一起使用 [英] Changing MySQL charset to UTF8 on linux so it will work with JDBC
问题描述
我的应用程序可以部署在Win \ Linux上,并使用MySQL 5.5,并且可以用不同的语言存储数据.
My application can be deployed on Win\Linux, and uses MySQL 5.5, and it may store data in different languages.
我将MySQL服务器更改为将以下字符集变量设置为UTF8:
I changed my MySQL server to have the following charset variables set to UTF8:
- character_set_client
- 字符集连接
- 字符集连接
- character_set_database
- character_set_results
- character_set_server
- character_set_system
为此,我在my.ini中添加了以下内容:
To do so, I added the following to my.ini:
[mysql]
default-character-set=utf8
[mysqld]
init_connect='SET collation_connection = utf8_unicode_ci'
init_connect='SET NAMES utf8'
character-set-server=utf8
collation-server=utf8_unicode_ci
skip-character-set-client-handshake
它在Windows(MySQL 5.5.21 64位)上运行良好,但是当我的应用程序在Linux(MySQL 5.5.2 64位)上运行时,出现以下错误:
It worked well on Windows (MySQL 5.5.21 64 bit), but when my application run on Linux (MySQL 5.5.2 64 bit) I'm getting the following error:
com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: Unknown character set: 'utf8mb4'
任何人都知道'utf8mb4'是从哪里来的,我该如何克服呢?
Anyone knows where does 'utf8mb4' came from and how can I overcome this?
JDBC驱动程序:"mysql-connector-java-5.1.18-bin"
JDBC driver: 'mysql-connector-java-5.1.18-bin'
推荐答案
您可以将其添加到connection-url
参数中,例如:
You can just to add to your connection-url
params like:
jdbc:mysql://主机名:3306/dbname?useUnicode = true& characterEncoding = utf8& characterResultSets = utf8
jdbc:mysql://hostname:3306/dbname?useUnicode=true&characterEncoding=utf8&characterResultSets=utf8
主机名->可以是localhost
或任何其他主机(您可以使用IP地址000.000.0.0
指定)
hostname -> may be localhost
or any other host(you can specify with ip address 000.000.0.0
)
这篇关于在Linux上将MySQL字符集更改为UTF8,使其可与JDBC一起使用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!