如何在MySQL上配置全局字符集


12

我试图通过my.cnf更改MySQL的字符集,但失败了。我添加了下面列出的字符集设置:

# The MySQL server
[mysqld]
port            = 3306
socket          = /var/lib/mysql/mysql.sock
skip-locking
key_buffer = 16M
max_allowed_packet = 1M
table_cache = 64
sort_buffer_size = 512K
net_buffer_length = 8K
read_buffer_size = 256K
read_rnd_buffer_size = 512K
myisam_sort_buffer_size = 8M

# ------------- MYSQL CHARSETS --------------
character_set_system = utf8
character_set_server = utf8
character_set_results = utf8
character_set_database = utf8
character_set_connection = utf8
character_set_client = utf8

collation_connection = utf8_general_ci
collation_database = utf8_general_ci
collation_server = utf8_general_ci

启用这些功能后,MySQL无法启动。如何确定所有内容都默认为utf8-始终?

更新资料

还有一些地方存在UTF8问题

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/
collation_connection    utf8_general_ci
collation_database  latin1_swedish_ci
collation_server    latin1_swedish_ci

不幸的是,我仍然没有想出一个解决方案...有什么想法吗?

目前,我正在成功使用此配置:

[mysqld]
default-character-set = utf8
init-connect='SET NAMES utf8'
character-set-server = utf8


[mysql]
default-character-set = utf8

但是PHPMyAdmin仍然显示一些latin1

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/
collation_connection    utf8_general_ci
collation_database  latin1_swedish_ci
collation_server    latin1_swedish_ci

Answers:


23

您需要将以下内容合并到my.cnf文件中

[client]
default-character-set=utf8

[mysql]
default-character-set=utf8


[mysqld]
default-character-set = utf8    
collation-server = utf8_general_ci
init-connect='SET NAMES utf8'
character-set-server = utf8

您可以在以下手册页上找到更多信息

为了回答Mark的答案,如果您需要将现有的表/数据库转换为正确的字符集,则需要一个合适的ALTER查询。类似于以下内容:

ALTER TABLE `table` CONVERT TO CHARACTER SET utf8 COLLATE utf8_unicode_ci;

另外还有一个很大的PHP脚本在这里它会为你。


我已经按照您的指示进行了操作,并检查了文档链接...仍然存在一些问题(请参阅^)

每当我添加collation-server = utf8_general_ci到“ mysqldmysql无法重新启动” 部分时

您可以查看日志文件中的内容吗?它应该给您一条错误消息,说明失败的原因。我认为您的mysqld是使用utf8常规字符集编译的吗?

是的,我认为是...哪个日志文件?

1
适用于我(Debian 8,MySQL 5.5),但本节default-character-set = utf8下的[mysqld]内容除外。添加这一行会导致mysqld永久重启。但是,即使没有此行,Unicode支持也可以很好地工作。
巴斯

2

Ben的答案是可以的,但是更改设置绝对不会更改现有表的字符集或排序规则。使用适当的ALTER TABLE语句执行此操作。

请注意,如果未指定,表将以数据库默认字符集创建。这是每个数据库的设置,可以使用ALTER DATABASE进行调整。在my.cnf中也未设置。


1
新数据库将继承默认的mysql设置,对吗?
By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.