【转】MySQL导入数据乱码的分析与解决
mysql> select * from table into outfile 'c:\table.txt' where 条件
(或selet * into outfile from table where 条件 )
导出来的数据会出现中文乱码情况。同时使用
mysql>load data local infile 'c:\\table.txt' into table `table`
插入的数据也会出现乱码情况。
究其原因,这是mysql服务器系统变量中的character_set_database中默认的字符集问题
。
常见 mysql5.1中文手册 13.2.5. LOAD DATA INFILE语法
……
由character_set_database系统变量指示的字符集被用于解释文件中的信息。SET NAMES
和character_set_client的设置不会影响对输入的解释。
……
我的解决方法:
首先在windows平台下mysql用作源导出数据库
mysql> show variables like '%char%';
+--------------------------+----------------------------+
| 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 | gbk |
| character_set_system | utf8 |
| character_sets_dir | D:\mysql\share\charsets\ |
+--------------------------+----------------------------+
查看character_set_database,这里是latin1,latin1是装不了大字符集的
mysql>set character_set_database=utf8; ##设置默认的字符集为utf8
mysql> select * from table into outfile 'c:\table.txt' where 条件
在freebsd平台下
mysql> show variables like '%char%';
+--------------------------+----------------------------+
| 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 | gbk |
| character_set_system | utf8 |
|
+--------------------------+----------------------------+
同样显示latin1
mysql>set character_set_database=utf8; ##设置默认的字符集为utf8
mysql>load data local infile '/home/table.txt' into table `table`;
显示查看即可!
这里最好在两台服务器的character_set_database参数相同,当然我们还可以使用gbk等字符集来进行调整,看情况
定义默认编码字符集,格式如下:
mysqldump.exe -u xxx -ppass --default-character-set=utf8 freshbug_database > fb_dat_utf8.sql