Change MySQL default character set to UTF-8 in my.cnf?
目前,我们在PHP中使用以下命令在应用程序中将字符集设置为utf-8。
由于这有点开销,我们希望在MySQL中将其设置为默认设置。我们可以在/etc/my.cnf或其他位置执行此操作吗?
我在/etc/my.cnf中寻找了一个默认的字符集,但是没有关于字符集的内容。
此时,我执行了以下操作,将mysql charset和collation变量设置为utf-8:
1 2 3 | skip-character-set-client-handshake character_set_client=utf8 character_set_server=utf8 |
号
这是正确的处理方法吗?
要将默认值设置为utf-8,请将以下内容添加到my.cnf中
1 2 3 4 5 6 7 8 9 10 11 |
如果要更改现有数据库的字符集,请告诉我…你的问题没有直接说明,所以我不确定你是否想这样做。
对于MySQL的最新版本,
导致问题。我想这是不赞成的。
正如JustinBall在"升级到MySQL5.5.12,而现在MySQL无法启动"中所说,您应该:
删除那个指令,你应该做的很好。
那么您的配置文件("/etc/my.cnf"例如)应该如下所示:
1 2 3 4 | [mysqld] collation-server = utf8_unicode_ci init-connect='SET NAMES utf8' character-set-server = utf8 |
。
重启mysql。
为了确保mysql是utf-8,请在mysql提示符中运行以下查询:
第一个查询:
输出应如下所示:
1
2
3
4
5
6
7
8
9
10
11
12+--------------------------+---------------------------------+
| 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/local/mysql/share/charsets/|
+--------------------------+---------------------------------+。
第二个查询:
号
查询输出为:
1
2
3
4
5
6
7+----------------------+-----------------+
| Variable_name | Value |
+----------------------+-----------------+
| collation_connection | utf8_general_ci |
| collation_database | utf8_unicode_ci |
| collation_server | utf8_unicode_ci |
+----------------------+-----------------+号
在mysql 5.5上,我的.cnf中有
1 2 3 4 5 6 | [mysqld] init_connect='SET collation_connection = utf8_unicode_ci' init_connect='SET NAMES utf8' character-set-server=utf8 collation-server=utf8_unicode_ci skip-character-set-client-handshake |
。
结果是
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 | 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/ | +--------------------------+----------------------------+ 8 rows in set (0.00 sec) +----------------------+-----------------+ | Variable_name | Value | +----------------------+-----------------+ | collation_connection | utf8_unicode_ci | | collation_database | utf8_unicode_ci | | collation_server | utf8_unicode_ci | +----------------------+-----------------+ 3 rows in set (0.00 sec) |
这个问题已经有很多答案了,但是Mathias Bynens提到为了更好地支持UTF-8,应该使用"utf8mb4"而不是"utf8"("utf8"不支持4字节字符,插入时字段被截断)。我认为这是一个重要的区别。所以这里还有另一个关于如何设置默认字符集和排序规则的答案。一个可以让你插入一堆粪便的。?)
这适用于MySQL5.5.35。
请注意,某些设置可能是可选的。因为我不完全确定我是否忘记了什么,所以我会把这个答案做成一个社区维基。
旧设置1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 | mysql> SHOW VARIABLES LIKE 'char%'; 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 | latin1 | | character_set_system | utf8 | | character_sets_dir | /usr/share/mysql/charsets/ | +--------------------------+----------------------------+ 8 rows in set (0.00 sec) +----------------------+-------------------+ | Variable_name | Value | +----------------------+-------------------+ | collation_connection | utf8_general_ci | | collation_database | latin1_swedish_ci | | collation_server | latin1_swedish_ci | +----------------------+-------------------+ 3 rows in set (0.00 sec) |
配置
1 2 3 4 5 6 7 8 9 10 11 12 13 | # ?? ?? # UTF-8 should be used instead of Latin1. Obviously. # NOTE"utf8" in MySQL is NOT full UTF-8: http://mathiasbynens.be/notes/mysql-utf8mb4 [client] default-character-set = utf8mb4 [mysqld] character-set-server = utf8mb4 collation-server = utf8mb4_unicode_ci [mysql] default-character-set = utf8mb4 |
号新设置
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 | mysql> SHOW VARIABLES LIKE 'char%'; SHOW VARIABLES LIKE 'collation%'; +--------------------------+----------------------------+ | Variable_name | Value | +--------------------------+----------------------------+ | character_set_client | utf8mb4 | | character_set_connection | utf8mb4 | | character_set_database | utf8mb4 | | character_set_filesystem | binary | | character_set_results | utf8mb4 | | character_set_server | utf8mb4 | | character_set_system | utf8 | | character_sets_dir | /usr/share/mysql/charsets/ | +--------------------------+----------------------------+ 8 rows in set (0.00 sec) +----------------------+--------------------+ | Variable_name | Value | +----------------------+--------------------+ | collation_connection | utf8mb4_general_ci | | collation_database | utf8mb4_unicode_ci | | collation_server | utf8mb4_unicode_ci | +----------------------+--------------------+ 3 rows in set (0.00 sec) |
字符集系统总是utf8。
这不会影响现有表,只是默认设置(用于新表)。可以使用以下alter代码转换现有表(不使用dump restore解决方案):
1 2 |
。
编辑:
在MySQL5.0服务器上:character_set_client、character_set_connection、character_set_results、collation_connection保持为latin1。发行
警告:如果有一个索引列类型为varchar(255)的utf8表,则在某些情况下无法转换它,因为超过了最大键长度(
注:my.cnf文件位于
添加这些行后:
1 2 3 4 5 6 7 8 9 10 11 |
。
不要忘记重新启动服务器:
1 | sudo service mysql restart |
Nijacat很接近,但指定了过度杀戮:
要将默认值设置为utf-8,请将以下内容添加到my.cnf中
号
然后,要验证:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 | 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/ | +--------------------------+----------------------------+ 8 rows in set (0.00 sec) +----------------------+-----------------+ | Variable_name | Value | +----------------------+-----------------+ | collation_connection | utf8_general_ci | | collation_database | utf8_general_ci | | collation_server | utf8_general_ci | +----------------------+-----------------+ 3 rows in set (0.00 sec) |
我还发现,在
mysql v5.5.3及更高版本:
只需在[mysqld]部分添加三行:
1 2 3 4 | [mysqld] character-set-server = utf8 collation-server = utf8_unicode_ci skip-character-set-client-handshake |
号
注:其中包括
这里列出的所有设置都是正确的,但这里是最理想和最充分的解决方案:
1 2 3 4 5 6 7 |
号
把这些加到
请注意,由于性能问题,我选择了utf8_unicode_ci排序规则类型。
结果是:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 | mysql> SHOW VARIABLES LIKE 'character%'; +--------------------------+----------------------------+ | 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_unicode_ci | | collation_database | utf8_unicode_ci | | collation_server | utf8_unicode_ci | +----------------------+-----------------+ |
号
这是当你作为非超级用户连接的时候!
例如,超级用户和非超级用户连接之间的区别(当然是在utf8_unicode_ci排序的情况下):
具有超级特权的用户:
1 2 3 4 5 6 7 8 | mysql> SHOW VARIABLES LIKE 'collation%'; +----------------------+-----------------+ | Variable_name | Value | +----------------------+-----------------+ | collation_connection | utf8_general_ci | <--- | collation_database | utf8_unicode_ci | | collation_server | utf8_unicode_ci | +----------------------+-----------------+ |
号
非超级特权用户:
1 2 3 4 5 6 7 8 | mysql> SHOW VARIABLES LIKE 'collation%'; +----------------------+-----------------+ | Variable_name | Value | +----------------------+-----------------+ | collation_connection | utf8_unicode_ci | | collation_database | utf8_unicode_ci | | collation_server | utf8_unicode_ci | +----------------------+-----------------+ |
号
我写了一篇全面的文章(rus),详细解释了为什么你应该使用一个或另一个选项。考虑所有类型的字符集和排序规则:对于服务器、数据库、连接、表甚至列。
我希望这篇文章能帮助澄清不清楚的时刻。
在Xubuntu 12.04下,我简单地补充了
1 2 | [mysqld] character_set_server = utf8 |
。
收件人/etc/mysql/my.cnf
结果是
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 | 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/ | +--------------------------+----------------------------+ 8 rows in set (0.00 sec) +----------------------+-----------------+ | Variable_name | Value | +----------------------+-----------------+ | collation_connection | utf8_general_ci | | collation_database | utf8_general_ci | | collation_server | utf8_general_ci | +----------------------+-----------------+ 3 rows in set (0.00 sec) |
。
还可以查看http://dev.mysql.com/doc/refman/5.6/en/charset-server.html
指令已改为
http://dev.mysql.com/doc/refman/5.6/en/charset-configuration.html
在Fedora 21上
1 | $ vi /etc/my.cnf |
添加以下内容:
1 2 3 4 5 6 7 8 9 10 11 12 |
。
保存并退出。
最后请记住,使用
在进行配置时,MySQL版本和Linux发行版可能很重要。
但是,鼓励根据
我想简单解释一下托马森德的回答:
1 2 3 4 5 6 | [mysqld] init_connect='SET collation_connection = utf8_unicode_ci' init_connect='SET NAMES utf8' character-set-server=utf8 collation-server=utf8_unicode_ci skip-character-set-client-handshake |
。
[我的QLD]
这会将排序规则连接更改为utf8u unicodeu ci
1 | init_connect='SET collation_connection = utf8_unicode_ci' |
号
使用
1 | init_connect='SET NAMES utf8' |
集合名称将影响三个字符,即:
1 2 3 | character_set_client character_set_results character_set_connection |
这将设置
1 | character-set-server=utf8 |
这只会影响排序规则数据库和排序规则服务器
1 | collation-server=utf8_unicode_ci |
对不起,我不太确定这是干什么用的。但我不使用它:
1 | skip-character-set-client-handshake |
号
MySQL5.5,您只需要:
1 2 3 4 | [mysqld] character_set_client=utf8 character_set_server=utf8 collation_server=utf8_unicode_ci |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 | mysql> show variables like 'char%'; +--------------------------+----------------------------+ | 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/ | +--------------------------+----------------------------+ 8 rows in set (0.00 sec) |
。
如果您在使用MySQL Workbench确认客户机的字符集支持时遇到问题,请记住以下注意事项:
重要mysql workbench打开的所有连接都会自动设置客户端字符设置为utf8。手动更改客户端字符集,例如使用集名称…,可能导致MySQL工作台无法正确显示字符。为了有关客户端字符集的其他信息,请参见连接字符集和排序规则。
因此,我无法用my.cnf更改覆盖mysql工作台的字符集。例如,"设置名称utf8mb4"
如果您对客户机的设置感到困惑,那么在重新启动MySQL服务后会重新设置conn。尝试以下步骤(对我有效):
[client]
character-sets-dir=/usr/local/mysql/share/mysql/charsets
查看参考资料了解更多信息。
你可以这样做,如果它不起作用,你需要重新启动MySQL。
更改mysql字符:
顾客号mySQL
1 | character_set_server=utf8 |
号
我们不应该在mysqld中写入
start: Job failed to start
号
最后:
1 2 3 4 5 6 7 8 9 10 11 12 | +--------------------------+----------------------------+ | 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/ | +--------------------------+----------------------------+ |
号