考虑一下:
- 您正在将数据存储为
latin1
- 您是由mysqld内部处理数据为
latin1
如果来自OS或来自连接的数据是utf8
,mysqld将如何处理它?
您可以更改传入的字符集行为,而不是猜测或期望最佳。除information_schema
和以外mysql
,请使用所有数据库并将默认字符集设置为utf8
:
ALTER DATABASE dbname CHARACTER SET utf8;
如果您要使用特定的排序规则,请执行以下操作:
ALTER DATABASE dbname COLLATE 'utf8_general_ci';
以下是排序规则可供选择:
mysql> select * from information_schema.collations where CHARACTER_SET_NAME = 'utf8';
+--------------------+--------------------+-----+------------+-------------+---------+
| COLLATION_NAME | CHARACTER_SET_NAME | ID | IS_DEFAULT | IS_COMPILED | SORTLEN |
+--------------------+--------------------+-----+------------+-------------+---------+
| utf8_general_ci | utf8 | 33 | Yes | Yes | 1 |
| utf8_bin | utf8 | 83 | | Yes | 1 |
| utf8_unicode_ci | utf8 | 192 | | Yes | 8 |
| utf8_icelandic_ci | utf8 | 193 | | Yes | 8 |
| utf8_latvian_ci | utf8 | 194 | | Yes | 8 |
| utf8_romanian_ci | utf8 | 195 | | Yes | 8 |
| utf8_slovenian_ci | utf8 | 196 | | Yes | 8 |
| utf8_polish_ci | utf8 | 197 | | Yes | 8 |
| utf8_estonian_ci | utf8 | 198 | | Yes | 8 |
| utf8_spanish_ci | utf8 | 199 | | Yes | 8 |
| utf8_swedish_ci | utf8 | 200 | | Yes | 8 |
| utf8_turkish_ci | utf8 | 201 | | Yes | 8 |
| utf8_czech_ci | utf8 | 202 | | Yes | 8 |
| utf8_danish_ci | utf8 | 203 | | Yes | 8 |
| utf8_lithuanian_ci | utf8 | 204 | | Yes | 8 |
| utf8_slovak_ci | utf8 | 205 | | Yes | 8 |
| utf8_spanish2_ci | utf8 | 206 | | Yes | 8 |
| utf8_roman_ci | utf8 | 207 | | Yes | 8 |
| utf8_persian_ci | utf8 | 208 | | Yes | 8 |
| utf8_esperanto_ci | utf8 | 209 | | Yes | 8 |
| utf8_hungarian_ci | utf8 | 210 | | Yes | 8 |
| utf8_sinhala_ci | utf8 | 211 | | Yes | 8 |
+--------------------+--------------------+-----+------------+-------------+---------+
22 rows in set (0.03 sec)
你也可以跑
mysql> show collation where charset='utf8';
+--------------------+---------+-----+---------+----------+---------+
| Collation | Charset | Id | Default | Compiled | Sortlen |
+--------------------+---------+-----+---------+----------+---------+
| utf8_general_ci | utf8 | 33 | Yes | Yes | 1 |
| utf8_bin | utf8 | 83 | | Yes | 1 |
| utf8_unicode_ci | utf8 | 192 | | Yes | 8 |
| utf8_icelandic_ci | utf8 | 193 | | Yes | 8 |
| utf8_latvian_ci | utf8 | 194 | | Yes | 8 |
| utf8_romanian_ci | utf8 | 195 | | Yes | 8 |
| utf8_slovenian_ci | utf8 | 196 | | Yes | 8 |
| utf8_polish_ci | utf8 | 197 | | Yes | 8 |
| utf8_estonian_ci | utf8 | 198 | | Yes | 8 |
| utf8_spanish_ci | utf8 | 199 | | Yes | 8 |
| utf8_swedish_ci | utf8 | 200 | | Yes | 8 |
| utf8_turkish_ci | utf8 | 201 | | Yes | 8 |
| utf8_czech_ci | utf8 | 202 | | Yes | 8 |
| utf8_danish_ci | utf8 | 203 | | Yes | 8 |
| utf8_lithuanian_ci | utf8 | 204 | | Yes | 8 |
| utf8_slovak_ci | utf8 | 205 | | Yes | 8 |
| utf8_spanish2_ci | utf8 | 206 | | Yes | 8 |
| utf8_roman_ci | utf8 | 207 | | Yes | 8 |
| utf8_persian_ci | utf8 | 208 | | Yes | 8 |
| utf8_esperanto_ci | utf8 | 209 | | Yes | 8 |
| utf8_hungarian_ci | utf8 | 210 | | Yes | 8 |
| utf8_sinhala_ci | utf8 | 211 | | Yes | 8 |
+--------------------+---------+-----+---------+----------+---------+
22 rows in set (0.00 sec)
mysql>
要查看数据库的单个字符集,请运行以下命令:
mysql> show create database sample;
+----------+-------------------------------------------------------------------+
| Database | Create Database |
+----------+-------------------------------------------------------------------+
| sample | CREATE DATABASE `sample` /*!40100 DEFAULT CHARACTER SET latin1 */ |
+----------+-------------------------------------------------------------------+
1 row in set (0.00 sec)
mysql>
至于设置,您可以尝试以下操作:
将行添加到 my.cnf
[mysqld]
character_set_database=utf8
character_set_server=utf8
然后重启mysql
我在2011年8月1日讨论了此问题:表中的字符集编码
CAVEAT(对于Windows中的MySQL数据库服务器)
这些命令
ALTER DATABASE dbname CHARACTER SET utf8;
ALTER DATABASE dbname COLLATE 'utf8_general_ci';
由于Windows锁定文件的方式,在Windows版本的MySQL中不起作用。所需文件被调用db.opt
,该文件位于中的数据库子文件夹中datadir
。
您可能需要执行以下操作:
- mysqldump那个数据库(没有数据库创建信息,只有表创建和INSERT)
- 删除该数据库
- 创建具有特定字符集和排序规则的数据库
- 重新装入转储中
结语
无论您做什么,都请在Dev / Staging Server上进行任何更改以查看是否获得所需的效果
更新2012-12-05 11:00 EDT
你的问题
我应该真的改变它吗?
为了保证对数据的正确处理,您可能需要确保每个苹果都对。准备作为一个字符集的数据,然后将其装入数据库的表中,并可能使数据对齐,就好像它看到另一个字符集一样,可能无法使用mysqld在检索并发送回数据库连接时看到的字符集来显示数据。尝试在Dev / Staging Server上加载数据库,并尝试设置默认字符集。
为什么有些默认设置使用utf8
某些默认设置latin1
?
这将取决于MySQL Binary的操作系统版本。Windows版本可能具有,latin1
而Linux版本可能会使用utf8
。