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

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

由于这是一个开销,我们希望将其设置为MySQL中的默认设置。 我们可以在/etc/my.cnf或其他位置执行此操作吗?

SET NAMES 'utf8' SET CHARACTER SET utf8 

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

在这一点上,我做了以下操作将MySQL字符集和归类variables设置为UTF-8:

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

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

要将默认值设置为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 

如果你想改变一个现有的数据库的字符集,让我知道…你的问题没有直接指定,所以我不知道这是你想要做的。

对于最近的MySQL版本,

 default-character-set = utf8 

导致一个问题。 我认为这已经过时了。

正如Justin Ball在“ 升级到MySQL 5.5.12 ”中所说, 现在MySQL不会启动 ,您应该:

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

  2. 那么你的configuration文件(例如'/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 | +----------------------+-----------------+ 

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

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

这适用于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) 

configuration

 # 💩 𝌆 # 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代码可用于转换现有表(不包含转储还原解决方法):

 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。


注意 :如果你有一个带VARCHAR(255)types的索引列的utf8表,在某些情况下它不能被转换,因为超过了最大密钥长度( Specified key was too long; max key length is 767 bytes. )。 如果可能,请将列大小从255减less到191 (因为191 * 4 = 764 <767 <192 * 4 = 768)。 之后,表格可以被转换。

我还发现,在[mysqld] title下设置default-character-set = utf8 ,MySQL 5.5.x将不会在Ubuntu 12.04 (Precise Pangolin)下启动。

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

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

 [client] default-character-set=utf8 [mysqld] default-character-set = utf8 

然后,validation:

 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]部分添加三行即可:

 [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

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

在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

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

 [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

请注意,由于性能问题,我select了utf8_unicode_citypes的sorting规则。

结果是:

 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和非SUPER用户的连接之间的区别(当然在utf8_unicode_ci整理的情况下):

用户SUPER priv .:

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

非SUPER权限的用户

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

我写了一篇全面的文章 (rus)详细解释了为什么你应该使用一个或另一个选项。 所有types的字符集sorting规则都考虑在内:服务器,数据库,连接,表甚至列。

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

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

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

在进行configuration时,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和collat​​ion_server

 collation-server=utf8_unicode_ci 

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

 skip-character-set-client-handshake 

在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

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 Workbench确认客户端的字符集支持时遇到问题,请记住以下注意事项:

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

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

如果您对客户端的设置感到困惑,并且在重新启动mysql服务之后conn被重新设置。 尝试这些步骤(这对我来说):

  1. vi /etc/my.cnf
  2. 添加内容和:wq [client] character-sets-dir=/usr/local/mysql/share/mysql/charsets
  3. 重启mysql并loginmysql,使用数据库,input命令status; ,你会发现'client'的字符集,'conn'被设置为'utf8'。

检查参考了解更多信息。

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

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