在my.cnf中将MySQL默认字符集更改为UTF-8?

时间:2010-08-18 15:29:41

标签: mysql character-encoding

目前我们在PHP中使用以下命令在我们的应用程序中将字符集设置为UTF-8

由于这有点开销,我们想将其设置为MySQL中的默认设置。我们可以在/etc/my.cnf或其他地方这样做吗?

SET NAMES 'utf8'
SET CHARACTER SET utf8

我在/etc/my.cnf中找了一个默认字符集,但是没有关于字符集的内容。

此时,我做了以下操作将MySQL字符集和校对变量设置为UTF-8:

skip-character-set-client-handshake
character_set_client=utf8
character_set_server=utf8

这是处理此问题的正确方法吗?

18 个答案:

答案 0 :(得分:403)

要将默认值设置为UTF-8,您需要将以下内容添加到my.cnf

[client]
default-character-set=utf8

[mysql]
default-character-set=utf8


[mysqld]
collation-server = utf8_unicode_ci
init-connect='SET NAMES utf8'
character-set-server = utf8

如果您想更改现有数据库的字符集,请告诉我...您的问题没有直接指定,所以我不确定这是否是您想要做的。

答案 1 :(得分:244)

对于最新版本的MySQL,

default-character-set = utf8

导致问题。我认为它已被弃用了。

正如Justin Ball在“Upgrade to MySQL 5.5.12 and now MySQL won’t start中所说,你应该:

  1. 删除该指令,你应该很好。

  2. 然后您的配置文件(例如'/etc/my.cnf')应如下所示:

    [mysqld]
    collation-server = utf8_unicode_ci
    init-connect='SET NAMES utf8'
    character-set-server = utf8
    
  3. 重启MySQL。

  4. 为确保您的MySQL是UTF-8,请在MySQL提示符下运行以下查询:

    • 首先查询:

       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/local/mysql/share/charsets/|
       +--------------------------+---------------------------------+
      
    • 第二个查询:

       mysql> show variables like 'collation%';
      

      查询输出为:

       +----------------------+-----------------+
       | Variable_name        | Value           |
       +----------------------+-----------------+
       | collation_connection | utf8_general_ci |
       | collation_database   | utf8_unicode_ci |
       | collation_server     | utf8_unicode_ci |
       +----------------------+-----------------+
      

答案 2 :(得分:54)

在MySQL 5.5上我有my.cnf

[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

结果是

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)

答案 3 :(得分:49)

这个问题已经有了很多答案,但是Mathias Bynens提到了这个问题' utf8mb4'应该用'而不是'utf8'为了获得更好的UTF-8支持(' utf8'不支持4字节字符,字段为truncated on insert)。我认为这是一个重要的区别。所以这是关于如何设置默认字符集和排序规则的另一个答案。一个允许你插入一堆poo()。

这适用于MySQL 5.5.35。

请注意,某些设置可能是可选的。由于我并不完全确定我没有忘记任何事情,我将把这个答案作为一个社区维基。

旧设置

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)

配置

#  
# 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

新设置

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)

character_set_system is always utf8

这不会影响现有表格,它只是默认设置(用于新表格)。 以下ALTER code可用于转换现有表(不使用转储还原解决方法):

ALTER DATABASE databasename CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;
ALTER TABLE tablename CONVERT TO CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;

编辑:

在MySQL 5.0服务器上:character_set_client,character_set_connection,character_set_results,collat​​ion_connection保留在latin1。发布SET NAMES utf8(该版本不提供utf8mb4)也将这些设置为utf8。


<强>买者: 如果您的utf8表的索引列的类型为VARCHAR(255),则在某些情况下无法转换,因为超出了最大密钥长度(Specified key was too long; max key length is 767 bytes.)。如果可能,将列大小从255减小到191(因为191 * 4 = 764&lt; 767&lt; 192 * 4 = 768)。之后,表格可以转换。

答案 4 :(得分:26)

<强> 注意: my.cnf文件位于/etc/mysql/

添加以下行后:

[mysqld]
collation-server = utf8_unicode_ci
init-connect='SET NAMES utf8'
character-set-server = utf8
skip-character-set-client-handshake

[client]
default-character-set   = utf8

[mysql]
default-character-set   = utf8

别忘了重启服务器:

sudo service mysql restart

答案 5 :(得分:23)

NijaCat很接近,但指定过度杀伤:

要将默认值设置为UTF-8,您需要将以下内容添加到my.cnf

[client]
default-character-set=utf8

[mysqld]
default-character-set = utf8

然后,验证:

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)

答案 6 :(得分:23)

我还发现,在default-character-set = utf8标题下设置[mysqld]之后,MySQL 5.5.x将无法在Ubuntu 12.04(精确穿山甲)下开始。

答案 7 :(得分:21)

MySQL v5.5.3及更高版本:

只在[mysqld]部分添加三行:

[mysqld]
character-set-server = utf8
collation-server = utf8_unicode_ci
skip-character-set-client-handshake

注意:此处包括skip-character-set-client-handshake,无需在init-connect[mysqld]default-character-set中包含[client][mysql]部分。

答案 8 :(得分:9)

在Xubuntu 12.04下我简单地添加了

[mysqld]
character_set_server = utf8

到/etc/mysql/my.cnf

结果是

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

答案 9 :(得分:9)

此处列出的所有设置都是正确的,但这是最佳且最充分的解决方案:

[mysqld]
init_connect='SET collation_connection = utf8_unicode_ci'
character-set-server = utf8
collation-server = utf8_unicode_ci

[client]
default-character-set = utf8

将这些内容添加到/etc/mysql/my.cnf

请注意,由于性能问题,我选择了 utf8_unicode_ci 排序规则类型。

结果是:

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 |
+----------------------+-----------------+

这是您以非超级用户连接的时候!

例如,作为SUPER和非SUPER用户的连接之间的区别(当然在 utf8_unicode_ci 整理的情况下):

用户 SUPER 版权所有:

mysql> SHOW VARIABLES LIKE 'collation%';
+----------------------+-----------------+
| Variable_name        | Value           |
+----------------------+-----------------+
| collation_connection | utf8_general_ci | <---
| collation_database   | utf8_unicode_ci |
| collation_server     | utf8_unicode_ci |
+----------------------+-----------------+

用户非超级 priv。:

mysql> SHOW VARIABLES LIKE 'collation%';
+----------------------+-----------------+
| Variable_name        | Value           |
+----------------------+-----------------+
| collation_connection | utf8_unicode_ci |
| collation_database   | utf8_unicode_ci |
| collation_server     | utf8_unicode_ci |
+----------------------+-----------------+

我写了一篇comprehensive article(rus)详细解释为什么你应该使用一个或另一个选项。所有类型的字符集排序规则都被视为:服务器,数据库,连接,表格甚至列。

我希望这篇文章有助于澄清不明确的时刻。

答案 10 :(得分:5)

指令已更改为character-set-system=utf8

http://dev.mysql.com/doc/refman/5.6/en/charset-configuration.html

答案 11 :(得分:4)

在进行配置时,MySQL版本和Linux发行版可能很重要。

但是,鼓励[mysqld]部分下的更改。

我想简单解释一下tomazzlender的答案:

[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

[mysqld]

这会将collat​​ion_connection更改为utf8_unicode_ci

init_connect='SET collation_connection = utf8_unicode_ci'

使用SET NAMES

init_connect='SET NAMES utf8'

SET NAMES会影响三个字符,即:

character_set_client
character_set_results
character_set_connection

这将设置character_set_database & character_set_server

character-set-server=utf8

这只会影响collat​​ion_database&amp; collat​​ion_server的

collation-server=utf8_unicode_ci

对不起,我不太确定这是为了什么。但是,我没有使用它:

skip-character-set-client-handshake

答案 12 :(得分:4)

在Fedora 21上

$ vi /etc/my.cnf

添加以下内容:

[client]
default-character-set=utf8

[mysql]
default-character-set=utf8

[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

保存并退出。

最后请记住使用service mysqld restart重启服务mysqld。

答案 13 :(得分:1)

MySQL 5.5,您只需要:

[mysqld]
character_set_client=utf8
character_set_server=utf8
collation_server=utf8_unicode_ci

collation_server是可选的。

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)

答案 14 :(得分:1)

如果您对客户端的设置感到困惑,则在重启mysql服务后重置conn。尝试这些步骤(对我有用):

  1. vi /etc/my.cnf
  2. 添加内容点击和:wq [client] character-sets-dir=/usr/local/mysql/share/mysql/charsets
  3. 重新启动mysql并登录mysql,使用数据库,输入命令status;,你就可以找到客户端的字符集&#39;和&#39; conn&#39;设置为&#39; utf8&#39;。
  4. 查看reference了解详情。

答案 15 :(得分:1)

如果您在使用MySQL Workbench确认客户端的字符集支持时遇到问题,请记住以下注意事项:

重要 MySQL Workbench打开的所有连接都会自动设置客户端字符 设置为utf8。手动更改客户端字符集,例如使用SET NAMES ...,可能导致MySQL Workbench无法正确显示字符。对于 有关客户端字符集的其他信息,请参阅连接字符集 和整理。

因此我无法使用my.cnf更改覆盖MySQL Workbench的字符集。例如'set names utf8mb4'

答案 16 :(得分:0)

你可以按照它的方式去做,如果它不起作用,你需要重新启动mysql。

答案 17 :(得分:-1)

更改MySQL字符:

客户端

default-character-set=utf8

的mysqld

character_set_server=utf8

我们不应该在mysqld中写default-character-set=utf8,因为这可能会导致如下错误:

  

开始:作业无法启动

最后:

 +--------------------------+----------------------------+
 | 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/ |
 +--------------------------+----------------------------+