我正在将旧的 mysql 数据库迁移到新的数据库,所以我编写了一个连接到旧数据库的脚本,读取它并在新数据库中创建新实体。这里是:
old_db = Mysql2::Client.new(host: options[:db_host],
username: options[:db_user],
password: options[:db_password],
database: options[:db_name],
encoding: 'utf8')
old_categories = old_db.query('select id, title from catalog__category order by lvl asc')
old_categories.each do |old_c|
c = Catalog::Category.new
c.name = old_c["title"]
c.save!
end
然而,在迁移之后,类别名称出现了非常糟糕的状态。两个数据库都以 utf8 编码。客户端和服务器设置utf8编码
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/local/Cellar/mysql/5.5.27/share/mysql/charsets/ |
PHP 项目使用旧数据库并正确显示所有字符串,Rails 项目使用新数据库并正确显示所有内容,但导入了类别字符串。
有谁知道问题出在哪里以及如何解决?
谢谢你。