mysql5.0中文插入乱码问题解决

  从google上找了不少方法,大部分建议转码什么的,不对路。
  我的工程是flex+blazeDS+spring+ibatis,brower的request是utf8的,java也是utf8,通过blazeDS能正常取得中文。但是insert成功后,数据库查看,中文乱码。
  把ibatis xml里的insert值替换成中文,如"中文问题"。插入后照样乱码,初步确认,问题不在工程端,重点排查mysql。
  mysql的字符集是utf8的,光看这点害人不浅。登录linux,mysql -uroot -p,进入mysql。
  命令:SHOW VARIABLES LIKE 'character_set_%';
  +--------------------------+----------------------------+
| Variable_name            | Value                      |
+--------------------------+----------------------------+
| character_set_client     | latin1                     |
| character_set_connection | latin1                     |
| character_set_database   | latin1                     |
| character_set_filesystem | binary                     |
| character_set_results    | latin1                     |
| character_set_server     | latin1                     |
| character_set_system     | utf8                       |
| character_sets_dir       | /usr/share/mysql/charsets/ |
+--------------------------+----------------------------+

只有character_set_system是utf8。
vi /etc/my.cnf
mysql的默认配置文件以及目录
编辑如下:
[mysqld]
default-character-set=utf8

 


之后:service mysql stop
service mysql start
重启后,再次查看SHOW VARIABLES LIKE 'character_set_%';

+--------------------------+----------------------------+
| Variable_name            | Value                      |
+--------------------------+----------------------------+
| character_set_client     | latin1                     |
| character_set_connection | latin1                     |
| character_set_database   | utf8                       |
| character_set_filesystem | binary                     |
| character_set_results    | latin1                     |
| character_set_server     | utf8                       |
| character_set_system     | utf8                       |
| character_sets_dir       | /usr/share/mysql/charsets/ |
+--------------------------+----------------------------+
此时,character_set_database已经是utf8,工程已经不再乱码。

 

可以再添加如下一句:

[client]

default-character-set=utf8

也可登录的时候, 要用以下命令:mysql --default-character-set=utf8 -u root -p
则:
+--------------------------+----------------------------+
| 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/ |
+--------------------------+----------------------------+

到此,问题解决。

总的来说,需要保证如下几点
1.工程交互界面收集的字符串在工程server端字符集需保持一致
2.工程与数据库字符集保持一致

 

你可能感兴趣的:(mysql,ibatis,database,System,character,variables)