简体   繁体   English

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

[英]Change MySQL default character set to UTF-8 in my.cnf?

Currently we are using the following commands in PHP to set the character set to UTF-8 in our application.目前我们在 PHP 中使用以下命令在我们的应用程序中将字符集设置为UTF-8

Since this is a bit of overhead, we'd like to set this as the default setting in MySQL. Can we do this in /etc/my.cnf or in another location?由于这有点开销,我们想将其设置为 MySQL 中的默认设置。我们可以在 /etc/my.cnf 或其他位置执行此操作吗?

SET NAMES 'utf8'
SET CHARACTER SET utf8

I've looked for a default charset in /etc/my.cnf, but there's nothing there about charsets.我在 /etc/my.cnf 中寻找默认字符集,但那里没有关于字符集的信息。

At this point, I did the following to set the MySQL charset and collation variables to UTF-8:此时,我执行了以下操作,将 MySQL 字符集和排序规则变量设置为 UTF-8:

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

Is that a correct way to handle this?这是处理这个问题的正确方法吗?

To set the default to UTF-8, you want to add the following to my.cnf/my.ini要将默认设置为 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

If you want to change the character set for an existing DB, let me know... your question didn't specify it directly so I am not sure if that's what you want to do.如果您想更改现有数据库的字符集,请告诉我......您的问题没有直接指定它,所以我不确定这是否是您想要做的。

Edit: I replaced utf8 with utf8mb4 in the original answer due to utf8 only being a subset of UTF-8.编辑:我在原始答案中用utf8mb4替换了utf8 ,因为utf8只是 UTF-8 的一个子集。 MySQL and MariaDB both call UTF-8 utf8mb4 . MySQL 和 MariaDB 都调用 UTF-8 utf8mb4

For the recent version of MySQL,对于最新版本的 MySQL,

default-character-set = utf8

causes a problem.导致问题。 It's deprecated I think.我认为它已被弃用。

As Justin Ball says in " Upgrade to MySQL 5.5.12 and now MySQL won't start , you should:正如Justin Ball在“ 升级到 MySQL 5.5.12 并且现在 MySQL 无法启动”中所说的那样,您应该:

  1. Remove that directive and you should be good.删除该指令,您应该会很好。

  2. Then your configuration file ('/etc/my.cnf' for example) should look like that:然后你的配置文件(例如'/etc/my.cnf')应该是这样的:

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

  4. For making sure, your MySQL is UTF-8, run the following queries in your MySQL prompt:为了确保您的 MySQL 是 UTF-8,请在您的 MySQL 提示符中运行以下查询:

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

This question already has a lot of answers, but Mathias Bynens mentioned that 'utf8mb4' should be used instead of 'utf8' in order to have better UTF-8 support ('utf8' does not support 4 byte characters, fields are truncated on insert ).这个问题已经有很多答案了,但 Mathias Bynens 提到应该使用 'utf8mb4' 而不是 'utf8' 以获得更好的 UTF-8 支持('utf8' 不支持 4 字节字符,字段在插入时截断)。 I consider this to be an important difference.我认为这是一个重要的区别。 So here is yet another answer on how to set the default character set and collation.所以这里是关于如何设置默认字符集和排序规则的另一个答案。 One that'll allow you to insert a pile of poo (💩).一个可以让你插入一堆便便(💩)。

This works on MySQL 5.5.35.这适用于 MySQL 5.5.35。

Note, that some of the settings may be optional.请注意,某些设置可能是可选的。 As I'm not entirely sure that I haven't forgotten anything, I'll make this answer a community wiki.由于我不完全确定我没有忘记任何事情,我会将这个答案设为社区维基。

Old Settings旧设置

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)

Config配置

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

New Settings新设置

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 . character_set_system 总是 utf8

This won't affect existing tables, it's just the default setting (used for new tables).这不会影响现有表,它只是默认设置(用于新表)。 The following ALTER code can be used to convert an existing table (without the dump-restore workaround):以下ALTER 代码可用于转换现有表(无需转储还原解决方法):

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

Edit:编辑:

On a MySQL 5.0 server: character_set_client, character_set_connection, character_set_results, collation_connection remain at latin1.在 MySQL 5.0 服务器上:character_set_client、character_set_connection、character_set_results、collat​​ion_connection 保持在 latin1。 Issuing SET NAMES utf8 (utf8mb4 not available in that version) sets those to utf8 as well.发出SET NAMES utf8 (utf8mb4 在该版本中不可用)也会将它们设置为 utf8。


Caveat : If you had a utf8 table with an index column of type VARCHAR(255), it can't be converted in some cases, because the maximum key length is exceeded ( Specified key was too long; max key length is 767 bytes. ).警告:如果您有一个索引列类型为 VARCHAR(255) 的 utf8 表,在某些情况下无法转换它,因为超出了最大密钥长度( Specified key was too long; max key length is 767 bytes. )。 If possible, reduce the column size from 255 to 191 (because 191 * 4 = 764 < 767 < 192 * 4 = 768).如果可能,将列大小从 255 减少到191 (因为 191 * 4 = 764 < 767 < 192 * 4 = 768)。 After that, the table can be converted.之后,可以转换表。

On MySQL 5.5 I have in my.cnf在 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

Result is结果是

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)

Note: my.cnf file is located at /etc/mysql/注意: my.cnf 文件位于/etc/mysql/

After adding these lines:添加这些行后:

[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

Don't forget to restart server:不要忘记重新启动服务器:

sudo service mysql restart

MySQL v5.5.3 and greater: MySQL v5.5.3 及更高版本:

Just add three lines only in the [mysqld] section:只需在 [mysqld] 部分添加三行:

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

Note: Including skip-character-set-client-handshake here obviates the need to include both init-connect in [mysqld] and default-character-set in the [client] and [mysql] sections.注意:在此处包含skip-character-set-client-handshake可以避免在[mysqld]包含init-connect并在[client][mysql]部分中包含default-character-set

NijaCat was close, but specified overkill: NijaCat 很接近,但指定了矫枉过正:

To set the default to UTF-8, you want to add the following to my.cnf要将默认设置为 UTF-8,您需要在 my.cnf 中添加以下内容

[client]
default-character-set=utf8

[mysqld]
default-character-set = utf8

Then, to verify:然后,验证:

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)

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

Under Xubuntu 12.04 I simply added在Xubuntu 12.04下我只是添加了

[mysqld]
character_set_server = utf8

to /etc/mysql/my.cnf到 /etc/mysql/my.cnf

And the result is结果是

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)

Also take a look at http://dev.mysql.com/doc/refman/5.6/en/charset-server.html也看看http://dev.mysql.com/doc/refman/5.6/en/charset-server.html

All settings listed here are correct, but here are the most optimal and sufficient solution:这里列出的所有设置都是正确的,但这里是最优化和最充分的解决方案:

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

[client]
default-character-set = utf8

Add these to /etc/mysql/my.cnf .将这些添加到/etc/mysql/my.cnf

Please note, I choose utf8_unicode_ci type of collation due to the performance issue.请注意,由于性能问题,我选择了utf8_unicode_ci类型的排序规则。

The result is:结果是:

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

And this is when you connect as non-SUPER user !这是当您以非超级用户身份连接时!

For example, the difference between connection as SUPER and non-SUPER user (of course in case of utf8_unicode_ci collation):例如,作为超级用户和非超级用户的连接之间的区别(当然在utf8_unicode_ci排序规则的情况下):

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

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

I wrote a comprehensive article (rus) explaining in details why you should use one or the other option.我写了一篇综合文章(rus) 详细解释了为什么应该使用一个或另一个选项。 All types of Character Sets and Collations are considered: for server, for database, for connection, for table and even for column.考虑所有类型的字符集排序规则:用于服务器、数据库、连接、表甚至列。

I hope this and the article will help to clarify unclear moments.我希望这篇文章和这篇文章将有助于澄清不清楚的时刻。

On Fedora 21在 Fedora 21 上

$ vi /etc/my.cnf

Add follow:添加关注:

[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

Save and exit.保存并退出。

Final remember restart service mysqld with service mysqld restart .最后记得用service mysqld restart

The directive has changed to character-set-system=utf8该指令已更改为character-set-system=utf8

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

MySQL versions and Linux distributions may matter when making configurations.在进行配置时,MySQL 版本和 Linux 发行版可能很重要。

However, the changes under [mysqld] section is encouraged.但是,鼓励在[mysqld]部分下进行更改。

I want to give a short explanation of tomazzlender's answer:我想对 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] [mysqld]

This will change collation_connection to utf8_unicode_ci这会将 collat​​ion_connection 更改为 utf8_unicode_ci

init_connect='SET collation_connection = utf8_unicode_ci'

Using SET NAMES :使用SET NAMES

init_connect='SET NAMES utf8'

The SET NAMES will influence three characters, that is: SET NAMES 将影响三个字符,即:

character_set_client
character_set_results
character_set_connection

This will set character_set_database & character_set_server这将设置character_set_database & character_set_server

character-set-server=utf8

This will only affect collation_database & collation_server这只会影响 collat​​ion_database & collat​​ion_server

collation-server=utf8_unicode_ci

Sorry, I'm not so sure what is this for.抱歉,我不太确定这是做什么用的。 I don't use it however:但是我不使用它:

skip-character-set-client-handshake

MySQL 5.5, all you need is: MySQL 5.5,您只需要:

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

collation_server is optional. 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)

If you're having trouble confirming the client's character-set support using MySQL Workbench, then keep the following note in mind:如果您在使用 MySQL Workbench 确认客户端的字符集支持时遇到问题,请记住以下注意事项:

Important All connections opened by MySQL Workbench automatically set the client character set to utf8.重要MySQL Workbench 打开的所有连接都会自动将客户端字符集设置为 utf8。 Manually changing the client character set, such as using SET NAMES ..., may cause MySQL Workbench to not correctly display the characters.手动更改客户端字符集,例如使用 SET NAMES ...,可能会导致 MySQL Workbench 无法正确显示字符。 For additional information about client character sets, see Connection Character Sets and Collations.有关客户端字符集的其他信息,请参阅连接字符集和排序规则。

Thus I was unable to override MySQL Workbench's character sets with my.cnf changes.因此,我无法使用 my.cnf 更改覆盖 MySQL Workbench 的字符集。 eg 'set names utf8mb4'例如“设置名称 utf8mb4”

If you are confused by your setting for client and conn is reseted after restart mysql service.如果您对客户端的设置感到困惑,并且在重新启动 mysql 服务后会重置 conn。 Try these steps (which worked for me):尝试以下步骤(对我有用):

  1. vi /etc/my.cnf
  2. add the contents blow and :wq [client] character-sets-dir=/usr/local/mysql/share/mysql/charsets添加内容吹和:wq [client] character-sets-dir=/usr/local/mysql/share/mysql/charsets
  3. restart mysql and login mysql , use database, input command status;重启mysql并登录mysql,使用数据库,输入命令status; , you'll find the character-set for 'client' and 'conn' is set to 'utf8'. ,您会发现“client”和“conn”的字符集设置为“utf8”。

Check thereference for more info.查看参考以获取更多信息。

在此处输入图像描述

For utf8mb4_general_ci对于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

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

We recommend against MySQL's utf8 character set, since it does not support 4-byte unicode characters, and strings containing them will be truncated.我们不建议使用 MySQL 的 utf8 字符集,因为它不支持 4 字节的 unicode 字符,包含它们的字符串将被截断。 This is fixed by the newer utf8mb4 character set.这是由较新的 utf8mb4 字符集修复的。

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

Change MySQL character:更改 MySQL 字符:

Client客户

default-character-set=utf8

mysqld mysqld

character_set_server=utf8

We should not write default-character-set=utf8 in mysqld, because that could result in an error like:我们不应该在 mysqld 中写入default-character-set=utf8 ,因为这可能会导致如下错误:

start: Job failed to start开始:作业未能启动

At last:最后:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM