13
votes

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
2
mysql2 0.3.15 was released on January 23, 2014, almost 2 years ago. Have you tried the latest version (currently 0.4.2)?Jared Beck
Hi, I'm having the same issue and wondering if you've resolved this issue or found a suitable workaround?Nick
mysql2 returns ASCII-8BIT for binary data like JSON serialized attrs.Paul Keen

2 Answers

1
votes

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

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

0
votes

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