从A主机备份到B主机
- mysqldump -uroot -p vw>vw.sql
- 现备份数据库文件,需要恢复到目标机B,B的数据库版本为5.5.23,A机器的mysql版本为5.0.22
- mysql>source /root/vw.sql;
- ……………………………………………………
- Query OK, 6748 rows affected (0.13 sec)
- Records: 6748 Duplicates: 0 Warnings: 0
- Query OK, 6807 rows affected (0.12 sec)
- Records: 6807 Duplicates: 0 Warnings: 0
- Query OK, 6752 rows affected (0.13 sec)
- Records: 6752 Duplicates: 0 Warnings: 0
- Query OK, 6659 rows affected (0.13 sec)
- Records: 6659 Duplicates: 0 Warnings: 0
- Query OK, 6676 rows affected (0.13 sec)
- Records: 6676 Duplicates: 0 Warnings: 0
- Query OK, 6583 rows affected (0.12 sec)
- Records: 6583 Duplicates: 0 Warnings: 0
- Query OK, 6598 rows affected (0.13 sec)
- Records: 6598 Duplicates: 0 Warnings: 0
- ERROR 1064 (42000): You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '' at line 1
- mysql>
在B机器上面导入的时候出现以上错误,排除数据库本身问题,后来通过navicat工具备份A的数据,再次导入机器B却成功了。
经过测试发现是字符集问题导致。解决办法如下
在A机器上面备份,使用如下命令,恢复到B机器,测试OK。
- mysqldump -uroot --default-character-set=utf8 -p vw>vw.sql
- 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 | latin1 |
- | character_set_system | utf8 |
- | character_sets_dir | /usr/share/mysql/charsets/ |
- +--------------------------+----------------------------+
二、查看 MySQL 数据表(table) 的字符集。
- mysql> show full columns from s_type;
- +---------+------------------------+-----------------+------+-----+---------+----------------+---------------------------------+---------+
- | Field | Type | Collation | Null | Key | Default | Extra | Privileges | Comment |
- +---------+------------------------+-----------------+------+-----+---------+----------------+---------------------------------+---------+
- | tid | mediumint(10) unsigned | NULL | NO | PRI | NULL | auto_increment | select,insert,update,references | |
- | tname | varchar(125) | utf8_general_ci | NO | MUL | NULL | | select,insert,update,references | |
- | en-name | varchar(125) | utf8_general_ci | NO | | NULL | | select,insert,update,references | |
- | type | tinyint(3) | NULL | NO | | NULL | | select,insert,update,references | |
- +---------+------------------------+-----------------+------+-----+---------+----------------+---------------------------------+---------+
- mysql> show full columns from s_type;
- +---------+------------------------+-----------------+------+-----+---------+----------------+---------------------------------+---------+
- | Field | Type | Collation | Null | Key | Default | Extra | Privileges | Comment |
- +---------+------------------------+-----------------+------+-----+---------+----------------+---------------------------------+---------+
- | tid | mediumint(10) unsigned | NULL | NO | PRI | NULL | auto_increment | select,insert,update,references | |
- | tname | varchar(125) | utf8_general_ci | NO | MUL | NULL | | select,insert,update,references | |
- | en-name | varchar(125) | utf8_general_ci | NO | | NULL | | select,insert,update,references | |
- | type | tinyint(3) | NULL | NO | | NULL | | select,insert,update,references | |
- +---------+------------------------+-----------------+------+-----+---------+----------------+---------------------------------+---------+
- mysql>show char set;
- mysql>show charset;
- +----------+-----------------------------+---------------------+--------+
- | Charset | Description | Default collation | Maxlen |
- +----------+-----------------------------+---------------------+--------+
- | big5 | Big5 Traditional Chinese | big5_chinese_ci | 2 |
- | dec8 | DEC West European | dec8_swedish_ci | 1 |
- | cp850 | DOS West European | cp850_general_ci | 1 |
- | hp8 | HP West European | hp8_english_ci | 1 |
- | koi8r | KOI8-R Relcom Russian | koi8r_general_ci | 1 |
- | latin1 | cp1252 West European | latin1_swedish_ci | 1 |
- | latin2 | ISO 8859-2 Central European | latin2_general_ci | 1 |
- | swe7 | 7bit Swedish | swe7_swedish_ci | 1 |
- | ascii | US ASCII | ascii_general_ci | 1 |
- | ujis | EUC-JP Japanese | ujis_japanese_ci | 3 |
- | sjis | Shift-JIS Japanese | sjis_japanese_ci | 2 |
- | hebrew | ISO 8859-8 Hebrew | hebrew_general_ci | 1 |
- | tis620 | TIS620 Thai | tis620_thai_ci | 1 |
- | euckr | EUC-KR Korean | euckr_korean_ci | 2 |
- | koi8u | KOI8-U Ukrainian | koi8u_general_ci | 1 |
- | gb2312 | GB2312 Simplified Chinese | gb2312_chinese_ci | 2 |
- | greek | ISO 8859-7 Greek | greek_general_ci | 1 |
- | cp1250 | Windows Central European | cp1250_general_ci | 1 |
- | gbk | GBK Simplified Chinese | gbk_chinese_ci | 2 |
- | latin5 | ISO 8859-9 Turkish | latin5_turkish_ci | 1 |
- | armscii8 | ARMSCII-8 Armenian | armscii8_general_ci | 1 |
- | utf8 | UTF-8 Unicode | utf8_general_ci | 3 |
- | ucs2 | UCS-2 Unicode | ucs2_general_ci | 2 |
- | cp866 | DOS Russian | cp866_general_ci | 1 |
- | keybcs2 | DOS Kamenicky Czech-Slovak | keybcs2_general_ci | 1 |
- | macce | Mac Central European | macce_general_ci | 1 |
- | macroman | Mac West European | macroman_general_ci | 1 |
- | cp852 | DOS Central European | cp852_general_ci | 1 |
- | latin7 | ISO 8859-13 Baltic | latin7_general_ci | 1 |
- | cp1251 | Windows Cyrillic | cp1251_general_ci | 1 |
- | cp1256 | Windows Arabic | cp1256_general_ci | 1 |
- | cp1257 | Windows Baltic | cp1257_general_ci | 1 |
- | binary | Binary pseudo charset | binary | 1 |
- | geostd8 | GEOSTD8 Georgian | geostd8_general_ci | 1 |
- | cp932 | SJIS for Windows Japanese | cp932_japanese_ci | 2 |
- | eucjpms | UJIS for Windows Japanese | eucjpms_japanese_ci | 3 |
- +----------+-----------------------------+---------------------+--------+
五,修改mysql默认字符设置
- vi /etc/my.cnf
- 在[client]下添加
- default-character-set=utf8
- 在[mysqld]下添加
- default-character-set=utf8
- 然后重启mysql
- service mysqld restart
修改数据库的字符集
- mysql>use mydb
- mysql>alter database mydb character set utf-8;
- mysql>create database mydb character set utf-8;
修改/var/lib/mysql/mydb/db.opt
- default-character-set=latin1
- default-collation=latin1_swedish_ci
- default-character-set=utf8
- default-collation=utf8_general_ci
- # /etc/rc.d/init.d/mysql restart
- mysql> set character_set_client=utf8;
- mysql> set character_set_connection=utf8;
- mysql> set character_set_database=utf8;
- mysql> set character_set_results=utf8;
- mysql> set character_set_server=utf8;
- mysql> set character_set_system=utf8;
- mysql> set collation_connection=utf8;)
- mysql> set collation_database=utf8;
- mysql> set collation_server=utf8;
- mysql> show variables like 'character_set_%';
- +---------------------------+---------------------------+
- | 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/|
- +---------------------------+----------------------------+
- mysql> show variables like 'collation_%';
- +----------------------+-----------------+
- | Variable_name | Value |
- +----------------------+-----------------+
- | collation_connection | utf8_general_ci |
- | collation_database | utf8_general_ci |
- | collation_server | utf8_general_ci |
- +----------------------+-----------------+
MySQL的字符集支持(Character Set Support)有两个方面:
字符集(Character set)和排序方式(Collation)。
对于字符集的支持细化到四个层次:
服务器(server),数据库(database),数据表(table)和连接(connection)。
1.MySQL默认字符集
MySQL对于字符集的指定可以细化到一个数据库,一张表,一列,应该用什么字符集。但是,传统的程序在创建数据库和数据表时并没有使用那么复杂的配置,它们用的是默认的配置,那么,默认的配置从何而来呢? (1)编译MySQL 时,指定了一个默认的字符集,这个字符集是 latin1;
(2)安装MySQL 时,可以在配置文件 (my.ini) 中指定一个默认的的字符集,如果没指定,这个值继承自编译时指定的;
(3)启动mysqld 时,可以在命令行参数中指定一个默认的的字符集,如果没指定,这个值继承自配置文件中的配置,此时 character_set_server 被设定为这个默认的字符集;
(4)当创建一个新的数据库时,除非明确指定,这个数据库的字符集被缺省设定为character_set_server;
(5)当选定了一个数据库时,character_set_database 被设定为这个数据库默认的字符集;
(6)在这个数据库里创建一张表时,表默认的字符集被设定为 character_set_database,也就是这个数据库默认的字符集;
(7)当在表内设置一栏时,除非明确指定,否则此栏缺省的字符集就是表默认的字符集;
简单的总结一下,如果什么地方都不修改,那么所有的数据库的所有表的所有栏位的都用
latin1 存储,不过我们如果安装 MySQL,一般都会选择多语言支持,也就是说,安装程序会自动在配置文件中把
default_character_set 设置为 UTF-8,这保证了缺省情况下,所有的数据库的所有表的所有栏位的都用 UTF-8 存储。 参考文章: http://blog.chinaunix.net/uid-20180960-id-1972668.html
本文转自it你好 51CTO博客,原文链接:http://blog.51cto.com/itnihao/850509,如需转载请自行联系原作者