问题描述
目前我们在 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/my.ini中添加以下内容
To set the default to UTF-8, you want to add the following to 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.
由于 utf8
只是 UTF-8 的一个子集,我在原始答案中用 utf8mb4
替换了 utf8
.MySQL 和 MariaDB 都调用 UTF-8 utf8mb4
.
I replaced utf8
with utf8mb4
in the original answer due to utf8
only being a subset of UTF-8. MySQL and MariaDB both call UTF-8 utf8mb4
.
这篇关于在 my.cnf 中将 MySQL 默认字符集更改为 UTF-8?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!