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

久未见 提交于 2020-03-12 18:46:40

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

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


#1楼

在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


#2楼

对于最新版本的MySQL,

default-character-set = utf8

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

正如Justin Ball在“ 升级到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. 重启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 | +----------------------+-----------------+

#3楼

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

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

超级私人用户:

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),详细解释了为什么你应该使用一个或另一个选项。 考虑所有类型的字符集排序规则 :用于服务器,用于数据库,用于连接,用于表,甚至用于列。

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


#4楼

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

#5楼

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)
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!