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

目前,我们在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

这是正确的处理方式吗?

585016 次浏览

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

[client]
default-character-set=utf8mb4


[mysql]
default-character-set=utf8mb4




[mysqld]
collation-server = utf8mb4_unicode_520_ci
init-connect='SET NAMES utf8mb4'
character-set-server = utf8mb4

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

编辑:我在原始答案中用utf8mb4替换了utf8,因为utf8只是UTF-8的一个子集。MySQL和MariaDB都调用UTF-8 utf8mb4

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

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

《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)

在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)

我还发现,在[mysqld]标题下设置default-character-set = utf8后,MySQL 5.5。x将不会在12.04 Ubuntu  (Precise穿山甲)下开始。

在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

对于MySQL的最新版本,

default-character-set = utf8

造成问题。我认为这是不可取的。

正如贾斯汀球在“升级到MySQL 5.5.12, MySQL将无法启动”中所说,你应该:

  1. 去掉这个指令,你应该就没事了。

  2. 然后你的配置文件(例如'/etc/my.cnf')应该是这样的:

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

  4. For making sure, your MySQL is UTF-8, run the following queries in your MySQL prompt:

    • First query:

       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/|
      +--------------------------+---------------------------------+
      
    • Second query:

       mysql> show variables like 'collation%';
      

      查询输出为:

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

这里列出的所有设置都是正确的,但这里是最优和最充分的解决方案:

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

这是当你以non-SUPER用户连接时!

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

用户超级 priv.:

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

用户non-SUPER priv.:

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

我写了一个全面的文章 (rus)详细解释为什么你应该使用一个或另一个选项。所有类型的字符集整理都被考虑:用于服务器、数据库、连接、表甚至列。

我希望这篇文章和这篇文章将有助于澄清不清楚的时刻。

更改MySQL字符:

客户端

default-character-set=utf8

mysqld

character_set_server=utf8

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

start:任务启动失败

最后:

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

在进行配置时,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)

这将把collation_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

这将只影响collation_database &collation_server

collation-server=utf8_unicode_ci

对不起,我不太清楚这是干什么用的。但是我不用这个词:

skip-character-set-client-handshake

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可以避免在[mysqld]中包含init-connect,在[client][mysql]部分中包含default-character-set

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

< >强重要 MySQL Workbench打开的所有连接都会自动设置客户端字符 设置为utf8。手动更改客户端字符集,例如使用set NAMES ...,可能导致MySQL Workbench不能正确显示字符。为 有关客户端字符集的其他信息,请参见连接字符集 和排序。< / p >

因此,我无法用my.cnf更改覆盖MySQL Workbench的字符集。如。设置名称utf8mb4

这个问题已经有很多答案,但Mathias Bynens提到,为了更好地支持UTF-8,应该使用'utf8mb4'而不是'utf8' ('utf8'不支持4字节字符,字段为在插入时被截断)。我认为这是一个重要的区别。这里有另一个关于如何设置默认字符集和排序规则的答案。它可以让你插入一堆便便(💩)。

这适用于MySQL 5.5.35。

注意,有些设置可能是可选的。因为我不完全确定我没有忘记任何东西,我将把这个答案放在社区wiki上。

旧的设置

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 总是utf8吗

这不会影响现有的表,这只是默认设置(用于新表)。 下面的修改代码可以用来转换一个现有的表(没有转储-恢复的变通方法)

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, collation_connection保持为latin1。发出SET NAMES utf8 (utf8mb4在该版本中不可用)也会将这些设置为utf8。


< p > 警告: 如果你有一个索引列类型为VARCHAR(255)的utf8表,在某些情况下它不能被转换,因为超过了最大键长度(Specified key was too long; max key length is 767 bytes.)。如果可能,将列大小从255减小到191(因为191 * 4 = 764 <767 & lt;192 * 4 = 768)。

在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。

如果你对客户端的设置感到困惑,重启mysql服务后conn被重置。试试下面这些步骤(对我很有效):

  1. vi /etc/my.cnf
  2. 添加内容blow和:wq (客户端) character-sets-dir = / usr /地方/ mysql /分享/ mysql /数据集< /代码> < /李>
  3. 重启mysql并登录mysql,使用数据库,输入命令status;,你会发现'client'和'conn'的字符集被设置为'utf8'。

查看参考获取更多信息。

< >强注意: 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

你可以这样做,如果它不工作,你需要重新启动mysql。

enter image description here

对于utf8mb4_general_ci

[client]
default-character-set=utf8mb4


[mysql]
default-character-set=utf8mb4
    

[mysqld]
collation-server = utf8mb4_general_ci
init-connect='SET NAMES utf8mb4'
character-set-server = utf8mb4

根据symfony框架文档https://symfony.com/doc/2.6/book/doctrine.html#configuring-the-database

我们建议不要使用MySQL的utf8字符集,因为它没有 支持4字节unicode字符,包含它们的字符串将是 截断。新的utf8mb4字符集修复了这个问题