Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

mysql2 gem 0.3.15 gives ASCII-8BIT with encoding set to "utf8"

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
like image 238
Sami Samhuri Avatar asked Mar 28 '14 17:03

Sami Samhuri


1 Answers

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

like image 185
Kamil Gwóźdź Avatar answered Sep 26 '22 19:09

Kamil Gwóźdź