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

这是正确的处理方式吗?


当前回答

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

其他回答

《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的最新版本,

default-character-set = utf8

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

正如Justin Ball在“升级到MySQL 5.5.12,现在MySQL无法启动,你应该:

Remove that directive and you should be good. Then your configuration file ('/etc/my.cnf' for example) should look like that: [mysqld] collation-server = utf8_unicode_ci init-connect='SET NAMES utf8' character-set-server = utf8 Restart MySQL. For making sure, your MySQL is UTF-8, run the following queries in your MySQL prompt: First query: mysql> show variables like 'char%'; The output should look like: +--------------------------+---------------------------------+ | 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%'; And the query output is: +----------------------+-----------------+ | Variable_name | Value | +----------------------+-----------------+ | collation_connection | utf8_general_ci | | collation_database | utf8_unicode_ci | | collation_server | utf8_unicode_ci | +----------------------+-----------------+

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

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

查看参考资料了解更多信息。

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

对于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字符集修复了这个问题。