目前我们在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
这是处理这个问题的正确方法吗?
要将默认值设置为UTF-8,您需要将以下内容添加到my.cnf
[client] default-character-set=utf8 [mysql] default-character-set=utf8 [mysqld] collation-server = utf8_unicode_ci init-connect='SET NAMES utf8' character-set-server = utf8
如果您想更改现有数据库的字符集,请告诉我...您的问题没有直接指定,所以我不确定这是否是您想要做的.
对于最新版本的MySQL,
default-character-set = utf8
导致问题.我认为它已被弃用了.
正如Justin Ball在" 升级到MySQL 5.5.12并且现在MySQL无法启动 "中所说,你应该:
删除该指令,你应该是好的.
然后您的配置文件(例如'/etc/my.cnf')应如下所示:
[mysqld] collation-server = utf8_unicode_ci init-connect='SET NAMES utf8' character-set-server = utf8
重启MySQL.
为了确保您的MySQL是UTF-8,请在MySQL提示符中运行以下查询:
第一个查询:
mysql> show variables like 'char%';
输出应如下所示:
+--------------------------+---------------------------------+ | 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/| +--------------------------+---------------------------------+
第二个查询:
mysql> show variables like 'collation%';
查询输出是:
+----------------------+-----------------+ | Variable_name | Value | +----------------------+-----------------+ | collation_connection | utf8_general_ci | | collation_database | utf8_unicode_ci | | collation_server | utf8_unicode_ci | +----------------------+-----------------+
在MySQL 5.5上我有my.cnf
[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
结果是
mysql> show variables like "%character%";show variables like "%collation%"; +--------------------------+----------------------------+ | 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/share/mysql/charsets/ | +--------------------------+----------------------------+ 8 rows in set (0.00 sec) +----------------------+-----------------+ | Variable_name | Value | +----------------------+-----------------+ | collation_connection | utf8_unicode_ci | | collation_database | utf8_unicode_ci | | collation_server | utf8_unicode_ci | +----------------------+-----------------+ 3 rows in set (0.00 sec)
这个问题已经有了很多答案,但Mathias Bynens提到应该使用'utf8mb4'代替'utf8'以获得更好的UTF-8支持('utf8'不支持4字节字符,字段在插入时被截断).我认为这是一个重要的区别.所以这是关于如何设置默认字符集和排序规则的另一个答案.一个允许你插入一堆便便()的人.
这适用于MySQL 5.5.35.
请注意,某些设置可能是可选的.由于我不完全确定我没有忘记任何事情,我会将此答案作为社区维基.
mysql> SHOW VARIABLES LIKE 'char%'; SHOW VARIABLES LIKE 'collation%'; +--------------------------+----------------------------+ | Variable_name | Value | +--------------------------+----------------------------+ | character_set_client | utf8 | | character_set_connection | utf8 | | character_set_database | latin1 | | character_set_filesystem | binary | | character_set_results | utf8 | | character_set_server | latin1 | | character_set_system | utf8 | | character_sets_dir | /usr/share/mysql/charsets/ | +--------------------------+----------------------------+ 8 rows in set (0.00 sec) +----------------------+-------------------+ | Variable_name | Value | +----------------------+-------------------+ | collation_connection | utf8_general_ci | | collation_database | latin1_swedish_ci | | collation_server | latin1_swedish_ci | +----------------------+-------------------+ 3 rows in set (0.00 sec)
# # UTF-8 should be used instead of Latin1. Obviously. # NOTE "utf8" in MySQL is NOT full UTF-8: http://mathiasbynens.be/notes/mysql-utf8mb4 [client] default-character-set = utf8mb4 [mysqld] character-set-server = utf8mb4 collation-server = utf8mb4_unicode_ci [mysql] default-character-set = utf8mb4
mysql> SHOW VARIABLES LIKE 'char%'; SHOW VARIABLES LIKE 'collation%'; +--------------------------+----------------------------+ | Variable_name | Value | +--------------------------+----------------------------+ | character_set_client | utf8mb4 | | character_set_connection | utf8mb4 | | character_set_database | utf8mb4 | | character_set_filesystem | binary | | character_set_results | utf8mb4 | | character_set_server | utf8mb4 | | character_set_system | utf8 | | character_sets_dir | /usr/share/mysql/charsets/ | +--------------------------+----------------------------+ 8 rows in set (0.00 sec) +----------------------+--------------------+ | Variable_name | Value | +----------------------+--------------------+ | collation_connection | utf8mb4_general_ci | | collation_database | utf8mb4_unicode_ci | | collation_server | utf8mb4_unicode_ci | +----------------------+--------------------+ 3 rows in set (0.00 sec)
character_set_system 总是utf8.
这不会影响现有表,它只是默认设置(用于新表).以下ALTER代码可用于转换现有表(不使用dump-restore解决方法):
ALTER DATABASE databasename CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci; ALTER TABLE tablename CONVERT TO CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;
编辑:
在MySQL 5.0服务器上:character_set_client,character_set_connection,character_set_results,collation_connection保留在latin1.发布SET NAMES utf8
(在该版本中不提供utf8mb4)也将这些设置为utf8.
警告:如果你有一个带有VARCHAR(255)类型索引列的utf8表,在某些情况下它无法转换,因为超出了最大密钥长度(Specified key was too long; max key length is 767 bytes.
).如果可能,将列大小从255减小到191(因为191*4 = 764 <767 <192*4 = 768).之后,表格可以转换.
注意:
my.cnf文件位于/etc/mysql/
添加这些行后:
[mysqld] collation-server = utf8_unicode_ci init-connect='SET NAMES utf8' character-set-server = utf8 skip-character-set-client-handshake [client] default-character-set = utf8 [mysql] default-character-set = utf8
别忘了重启服务器:
sudo service mysql restart
NijaCat很接近,但指定过度杀伤:
要将默认值设置为UTF-8,您需要将以下内容添加到my.cnf
[client] default-character-set=utf8 [mysqld] default-character-set = utf8
然后,验证:
mysql> show variables like "%character%";show variables like "%collation%"; +--------------------------+----------------------------+ | 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/share/mysql/charsets/ | +--------------------------+----------------------------+ 8 rows in set (0.00 sec) +----------------------+-----------------+ | Variable_name | Value | +----------------------+-----------------+ | collation_connection | utf8_general_ci | | collation_database | utf8_general_ci | | collation_server | utf8_general_ci | +----------------------+-----------------+ 3 rows in set (0.00 sec)
我还发现,设置后default-character-set = utf8
在[mysqld]
标题中,MySQL的5.5.x不会下启动的Ubuntu 12.04(精确穿山甲).
MySQL v5.5.3及更高版本:
只需在[mysqld]部分添加三行:
[mysqld] character-set-server = utf8 collation-server = utf8_unicode_ci skip-character-set-client-handshake
注:包括skip-character-set-client-handshake
这里省却了包括init-connect
在[mysqld]
和default-character-set
中[client]
和[mysql]
节.
在Xubuntu 12.04下我简单地补充道
[mysqld] character_set_server = utf8
到/etc/mysql/my.cnf
结果是
mysql> show variables like "%character%";show variables like "%collation%"; +--------------------------+----------------------------+ | 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/share/mysql/charsets/ | +--------------------------+----------------------------+ 8 rows in set (0.00 sec) +----------------------+-----------------+ | Variable_name | Value | +----------------------+-----------------+ | collation_connection | utf8_general_ci | | collation_database | utf8_general_ci | | collation_server | utf8_general_ci | +----------------------+-----------------+ 3 rows in set (0.00 sec)
另请查看http://dev.mysql.com/doc/refman/5.6/en/charset-server.html
此处列出的所有设置都是正确的,但这里是最佳且充分的解决方案:
[mysqld] init_connect='SET collation_connection = utf8_unicode_ci' character-set-server = utf8 collation-server = utf8_unicode_ci [client] default-character-set = utf8
添加这些/etc/mysql/my.cnf
.
请注意,由于性能问题,我选择utf8_unicode_ci类型的排序规则.
结果是:
mysql> SHOW VARIABLES LIKE 'character%'; +--------------------------+----------------------------+ | 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/share/mysql/charsets/ | +--------------------------+----------------------------+ mysql> SHOW VARIABLES LIKE 'collation%'; +----------------------+-----------------+ | Variable_name | Value | +----------------------+-----------------+ | collation_connection | utf8_unicode_ci | | collation_database | utf8_unicode_ci | | collation_server | utf8_unicode_ci | +----------------------+-----------------+
这是当您作为非SUPER用户连接时!
例如,作为SUPER和非SUPER用户的连接之间的区别(当然在utf8_unicode_ci整理的情况下):
超级私人用户:
mysql> SHOW VARIABLES LIKE 'collation%'; +----------------------+-----------------+ | Variable_name | Value | +----------------------+-----------------+ | collation_connection | utf8_general_ci | <--- | collation_database | utf8_unicode_ci | | collation_server | utf8_unicode_ci | +----------------------+-----------------+
非SUPER私人用户:
mysql> SHOW VARIABLES LIKE 'collation%'; +----------------------+-----------------+ | Variable_name | Value | +----------------------+-----------------+ | collation_connection | utf8_unicode_ci | | collation_database | utf8_unicode_ci | | collation_server | utf8_unicode_ci | +----------------------+-----------------+
我写了一篇全面的文章(rus),详细解释了为什么你应该使用一个或另一个选项.考虑所有类型的字符集和排序规则:用于服务器,用于数据库,用于连接,用于表,甚至用于列.
我希望这篇文章和文章将有助于澄清不明确的时刻.
该指令已更改为 character-set-system=utf8
http://dev.mysql.com/doc/refman/5.6/en/charset-configuration.html