問題描述
目前我們在 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?的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!