目前我们在PHP中使用以下命令在应用程序中将字符集设置为UTF-8。
因为这有点开销,所以我们希望将其设置为MySQL中的默认设置。我们可以在/etc/my.cnf或其他位置这样做吗?
SET NAMES 'utf8'
SET CHARACTER SET utf8
我在/etc/my.cnf中寻找了一个默认字符集,但那里没有关于字符集的任何内容。
在这一点上,我做了以下设置MySQL字符集和排序变量为UTF-8:
skip-character-set-client-handshake
character_set_client=utf8
character_set_server=utf8
这是正确的处理方式吗?
对于MySQL的最新版本,
default-character-set = utf8
造成问题。我认为这是不可取的。
正如Justin Ball在“升级到MySQL 5.5.12,现在MySQL无法启动,你应该:
Remove that directive and you should be good.
Then your configuration file ('/etc/my.cnf' for example) should look like that:
[mysqld]
collation-server = utf8_unicode_ci
init-connect='SET NAMES utf8'
character-set-server = utf8
Restart MySQL.
For making sure, your MySQL is UTF-8, run the following queries in your MySQL prompt:
First query:
mysql> show variables like 'char%';
The output should look like:
+--------------------------+---------------------------------+
| Variable_name | Value |
+--------------------------+---------------------------------+
| character_set_client | utf8 |
| character_set_connection | utf8 |
| character_set_database | utf8 |
| character_set_filesystem | binary |
| character_set_results | utf8 |
| character_set_server | utf8 |
| character_set_system | utf8 |
| character_sets_dir | /usr/local/mysql/share/charsets/|
+--------------------------+---------------------------------+
Second query:
mysql> show variables like 'collation%';
And the query output is:
+----------------------+-----------------+
| Variable_name | Value |
+----------------------+-----------------+
| collation_connection | utf8_general_ci |
| collation_database | utf8_unicode_ci |
| collation_server | utf8_unicode_ci |
+----------------------+-----------------+
在进行配置时,MySQL版本和Linux发行版可能很重要。
但是,[mysqld]部分下的更改是受鼓励的。
我想简单解释一下tomazzlender的回答:
[mysqld]
init_connect='SET collation_connection = utf8_unicode_ci'
init_connect='SET NAMES utf8'
character-set-server=utf8
collation-server=utf8_unicode_ci
skip-character-set-client-handshake
[mysqld]
这将把collation_connection更改为utf8_unicode_ci
init_connect='SET collation_connection = utf8_unicode_ci'
使用集合名称:
init_connect='SET NAMES utf8'
SET NAMES将影响三个字符,即:
character_set_client
character_set_results
character_set_connection
这将设置character_set_database & character_set_server
character-set-server=utf8
这只会影响collation_database和collation_server
collation-server=utf8_unicode_ci
对不起,我不太清楚这是干什么用的。但是我不用这个词:
skip-character-set-client-handshake