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

问题描述:

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

Currently we are using the following commands in PHP to set the character set to UTF-8 in our application.

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

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?

SET NAMES 'utf8'
SET CHARACTER SET utf8



我在/etc/my.cnf中查找了一个默认字符集,

I've looked for a default charset in /etc/my.cnf, but there's nothing there about charsets.

此时,我执行以下操作将MySQL字符集和排序规则变量设置为UTF-8:

At this point, I did the following to set the MySQL charset and collation variables to UTF-8:

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

这是正确的处理方式吗?

Is that a correct way to handle this?

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

To set the default to UTF-8, you want to add the following to 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

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

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.