mysql2 gem 0.3.15给出ASCII-8BIT,其编码设置为"utf8". [英] mysql2 gem 0.3.15 gives ASCII-8BIT with encoding set to "utf8"

查看:71
本文介绍了mysql2 gem 0.3.15给出ASCII-8BIT,其编码设置为"utf8".的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我将MySQL 5.1.71和在Rails 4.0.4上运行在Ruby 2.0.0-p353上(通过rbenv + ruby​​-build)和mysql2 gem 0.3.15一起使用. CentOS 6.5.

I am using MySQL 5.1.71 with Rails 4.0.4 running on Ruby 2.0.0-p353 (via rbenv + ruby-build), with mysql2 gem 0.3.15. CentOS 6.5.

在database.yml中,对于所有环境,编码都设置为"utf8",而适配器则设置为"mysql2".

In database.yml, encoding is set to "utf8" and adapter is "mysql2" for all environments.

我的表都使用UTF-8,"DEFAULT CHARSET = utf8 COLLATE = utf8_unicode_ci".

My tables are all using UTF-8, "DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci".

在Ruby中,Encoding::default_internal == Encoding::default_external == Encoding::UTF_8.

关于我还能在其他地方看到的任何想法,为什么ActiveRecord仍然向我提供ASCII-8BIT字符串?在开发中的Mac上使用UTF-8,但在Linux上的生产中使用ASCII-8BIT.

Any ideas on where else I can look to see why ActiveRecord still hands me ASCII-8BIT strings? I get UTF-8 on my Mac in development, but ASCII-8BIT in production on Linux.

当我启动一个控制台并直接使用mysql2时,我得到了ASCII,所以这似乎是问题所在.

When I fire up a console and use mysql2 directly I get ASCII, so that seems to be where the problem lies.

mysql> SHOW VARIABLES LIKE 'character_set%';
+--------------------------+----------------------------+
| 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)

mysql> SHOW VARIABLES LIKE 'collation%';
+----------------------+-----------------+
| Variable_name        | Value           |
+----------------------+-----------------+
| collation_connection | utf8_general_ci |
| collation_database   | utf8_unicode_ci |
| collation_server     | utf8_unicode_ci |
+----------------------+-----------------+
3 rows in set (0.00 sec)

SHOW CREATE TABLE产品:

SHOW CREATE TABLE product:

CREATE TABLE `product` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `name` varbinary(255) DEFAULT NULL,
  `price` decimal(12,2) DEFAULT NULL,
  `created_at` datetime DEFAULT NULL,
  `updated_at` datetime DEFAULT NULL,
  `category` varbinary(255) DEFAULT NULL,
  `quantity` int(11) NOT NULL,
  `package_id` int(11) DEFAULT NULL,
  PRIMARY KEY (`id`),
  KEY `index_product_on_package_id` (`package_id`)
) ENGINE=InnoDB AUTO_INCREMENT=18 DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci

推荐答案

请记住,MySQL的"utf8"编码并不是真正的utf8.您需要使用utf8mb4.

Keep in mind that the "utf8" encoding from MySQL is not really utf8. You need to use utf8mb4.

有关更多详细信息,请参见: https://medium.com/@adamhooper/in-mysql-never-use-utf8-use-utf8mb4-11761243e434

For more details see: https://medium.com/@adamhooper/in-mysql-never-use-utf8-use-utf8mb4-11761243e434

这篇关于mysql2 gem 0.3.15给出ASCII-8BIT,其编码设置为"utf8".的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆