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?
基础教程推荐
- SSMS 中的权限问题:“对象 'extended_properties'、数据库 'mssqlsystem_resource'、... 错误 229)上的 SELECT 权限被拒绝" 2022-01-01
- 如何使用 mysql.connector 禁用查询缓存 2022-01-01
- 将 SQL Server DateTime 列迁移到 DateTimeOffset 2021-01-01
- 无法解决整理冲突 2021-01-01
- 在 SQL 中连接多个表 2021-01-01
- SQL 效率:WHERE IN 子查询 vs. JOIN 然后 GROUP 2021-01-01
- 需要 MySQL 5.1 中的抽象触发器来更新审计日志 2021-01-01
- SQL:使用来自具有相同列名的两个表中的数据... 2021-01-01
- SQL Server 实例在登录协商期间返回无效或不受支持的协议版本 2021-01-01
- 是否可以执行按位分组功能? 2021-01-01
