Mysql2 :: Error无法初始化字符集utf8mb4(路径:/ usr / share / mysql / charsets /)

时间:2017-08-31 12:13:06

标签: mysql ruby-on-rails ruby-on-rails-4 centos6 utf8mb4

在我的Rails 4.2应用程序中,我偶然发现了一个奇怪的问题。我不得不将mysql编码切换到utf8mb4(允许在帖子中使用表情符号)。 我已将MySql更新为5.7,更改了表格等。更新了database.yml:

encoding: utf8mb4
collation: utf8mb4_unicode_ci

一切都在我运行mac OS的本地计算机上运行。

在测试服务器上,我完成了相同的步骤和配置。但是,如果我设置

encoding: utf8mb4
在database.yml中的

我得到了

Mysql2::Error Can't initialize character set utf8mb4 (path: /usr/share/mysql/charsets/)

mysql的其他MySQL设置我已在两台机器上的my.cnf中完成:

innodb_file_format = Barracuda
innodb_large_prefix
innodb_file_per_table = 1

init_connect='SET collation_connection = utf8_unicode_ci'
init_connect='SET NAMES utf8mb4'
innodb_file_format_max = Barracuda
innodb_strict_mode = 1

character-set-client-handshake = FALSE
character-set-server = utf8mb4
collation-server = utf8mb4_unicode_ci

我可以在测试服务器上添加表情符号,但不能通过Rails应用程序添加。 在这两台机器上,我都是mysql2版本0.3.21。 什么可能导致这个问题?

PS。即使我没有在我的本地,我已经在测试机上添加了utf8mb4到Index.xml,但它没有帮助。

1 个答案:

答案 0 :(得分:0)

也许这会有所帮助:RoR:application_controller.rb

  def configure_charsets
    response.headers["Content-Type"] = "text/html; charset=UTF-8"
    suppress(ActiveRecord::StatementInvalid) do
      ActiveRecord::Base.connection.execute 'SET NAMES utf8mb4'
    end
  end