昨天第一次导出Mysql数据库,发现dump中有一些很奇怪的字符,比如:
INSERT INTO `piwik_archive_blob_2013_01` VALUES (15,'Actions_actions_url_6',1,'2013-01-17','2013-01-17',1,'2013-01-20 07:36:53','xuNM0ý/œ#&ÝÕ³\ZõNYpÊÀì#!üw7Hж}°ÀAáZoN*šgµ\'GWª[Yûðe¯57 ÃÁÆ7|Ÿ\'Ü%µDh©-EÛ^ËL±ÕÞtªk@(,b±ßZ.ÒÃ6b²aiÓÍ)87[ïÎœ,æya¥uÒ<|+íª7MNuïÝ¿8ñ%1Ʊ>ÚX');
我的服务器 MySQL 的版本是:5.1.66-0+squeeze1 (Debian)。该数据库由Piwik安装脚本自动创建。
这是我试图解决此问题的方法:
#1 首先我检查了数据库字符集。
> show table status;
26 个表的排序规则 utf8_general_ci 听起来很正常。我猜 mysqldump 以不同的字符集(latin1?)导出,所以我尝试了:
mysqldump -u user -p**** --all-databases --default-character-set=utf8 | gzip -9 > dump.sql.gz
结果= 我仍然有相同的奇怪字符。
注意)后来我了解到mysqldump的默认字符集是utf8,无论服务器默认字符集如何。所以--default-character-set=utf8
没用。
#2 然后我想我可以通过更新 mysql conf 来解决问题。原来的conf是:
mysql> show variables like "%character%";show variables like "%collation%";
+--------------------------+----------------------------+
| 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/ |
+--------------------------+----------------------------+
+----------------------+-------------------+
| Variable_name | Value |
+----------------------+-------------------+
| collation_connection | latin1_swedish_ci |
| collation_database | latin1_swedish_ci |
| collation_server | latin1_swedish_ci |
+----------------------+-------------------+
所以我更新/var/lib/mysql/my.cnf
并添加了:
[mysqld]
init_connect='SET collation_connection = utf8_general_ci'
init_connect='SET NAMES utf8'
character-set-server=utf8
collation-server=utf8_general_ci
default-character-set=utf8
default-collation=utf8_general_ci
[mysqldump]
default-character-set=utf8
然后
/etc/init.d/mysql restart
mysql> show variables like "%character%";show variables like "%collation%";
+--------------------------+----------------------------+
| Variable_name | Value |
+--------------------------+----------------------------+
| character_set_client | utf8 |
| character_set_connection | utf8 |
| character_set_database | latin1 |
| character_set_filesystem | binary |
| character_set_results | utf8 |
| character_set_server | utf8 |
| character_set_system | utf8 |
| character_sets_dir | /usr/share/mysql/charsets/ |
+--------------------------+----------------------------+
+----------------------+-------------------+
| Variable_name | Value |
+----------------------+-------------------+
| collation_connection | utf8_general_ci |
| collation_database | latin1_swedish_ci |
| collation_server | utf8_general_ci |
+----------------------+-------------------+
结果= 相同的奇怪字符。
#3
我改变character_set_database
了collation_database
:
mysql> ALTER DATABASE piwik default character SET utf8 collate utf8_general_ci;
mysql> show variables like "%character%";show variables like "%collation%";
+--------------------------+----------------------------+
| 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/ |
+--------------------------+----------------------------+
+----------------------+-----------------+
| Variable_name | Value |
+----------------------+-----------------+
| collation_connection | utf8_general_ci |
| collation_database | utf8_general_ci |
| collation_server | utf8_general_ci |
+----------------------+-----------------+
结果= 相同的奇怪字符。
#4 我现在明白了,我应该在创建数据库之前将 MySQL (latin1) 中的默认字符集更改为 utf8。
排序规则utf8_general_ci
(#1) 表示数据存储在 utf8 中。但是,mysqldump 是否有可能认为数据存储在 latin1 中并以 utf8 编码数据?这意味着最后数据是双 utf8 编码的(叹气)。在这种情况下,我该如何解决问题?
谢谢你的帮助。
ps) 我想知道为什么 Piwik 不需要将数据库默认字符集更改为 utf8。