mysql2 gem 0.3.15 gives ASCII-8BIT with encoding s

2019-04-20 00:56发布

问题:

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.

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

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

In Ruby, Encoding::default_internal == Encoding::default_external == Encoding::UTF_8.

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.

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 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

回答1:

First of all check in my.cnf collation for all server. You should have something like:

[mysqld]
init_connect=‘SET collation_connection = utf8_unicode_ci’
character-set-server = utf8
collation-server = utf8_unicode_ci

[client]
default-character-set = utf8

Second check collation for client in Rails:

ActiveRecord::Base.connection.collation