Change MySQL default character set to UTF-8 in my.cnf?(在 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/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?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:在 my.cnf 中将 MySQL 默认字符集更改为 UTF-8?


- 远程 mySQL 连接抛出“无法使用旧的不安全身份验证连接到 MySQL 4.1+"来自 XAMPP 的错误 2022-01-01
- SQL 临时表问题 2022-01-01
- 如何使用 pip 安装 Python MySQLdb 模块? 2021-01-01
- 导入具有可变标题的 Excel 文件 2021-01-01
- 如何将 Byte[] 插入 SQL Server VARBINARY 列 2021-01-01
- 在SQL中,如何为每个组选择前2行 2021-01-01
- 使用 Oracle PL/SQL developer 生成测试数据 2021-01-01
- 以一个值为轴心,但将一行上的数据按另一行分组? 2022-01-01
- 如何将 SonarQube 6.7 从 MySQL 迁移到 postgresql 2022-01-01
- 更改自动增量起始编号? 2021-01-01