问题
I am facing this issue when importing an SQL file with a table with JSON column.
The database has
Encoding: utf8mb4
Collation: utf8mb4_unicode_ci
Here's the table with the json column:
CREATE TABLE `tracking_data` (
`id` int(10) unsigned NOT NULL AUTO_INCREMENT,
`route` varchar(191) COLLATE utf8mb4_unicode_ci NOT NULL,
`data` json NOT NULL,
`user_id` int(10) unsigned NOT NULL,
`created_at` timestamp NULL DEFAULT NULL,
`updated_at` timestamp NULL DEFAULT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;
I have read answers from this question on stack overflow which suggest the table must be on utf8mb4
. Mine's already, but I am facing this issue still.
This is error is still thrown:
Line XXX: Cannot create a JSON value from a string with CHARACTER SET 'binary'.
When I run show session variables like 'char%';
:
Variable_name Value
character_set_client utf8mb4
character_set_connection utf8mb4
character_set_database utf8mb4
character_set_filesystem binary
character_set_results utf8mb4
character_set_server latin1
character_set_system utf8
character_sets_dir /usr/share/mysql/charsets/
How can I fix this? Thanks!
回答1:
I can reproduce your error if my session character set is binary
:
mysql [localhost] {msandbox} (test) > set names binary;
Query OK, 0 rows affected (0.00 sec)
mysql [localhost] {msandbox} (test) > insert into tracking_data (route, data, user_id) values ('route', '{"route": "value"}', 1);
ERROR 3144 (22032): Cannot create a JSON value from a string with CHARACTER SET 'binary'.
Then I can fix it by setting the session character set to match the table:
mysql [localhost] {msandbox} (test) > set names utf8mb4;
Query OK, 0 rows affected (0.00 sec)
mysql [localhost] {msandbox} (test) > insert into tracking_data (route, data, user_id) values ('route', '{"route": "value"}', 1);
Query OK, 1 row affected (0.01 sec)
So I conclude that you have set the session character set to binary
.
Run show session variables like 'char%';
and see what it tells you.
回答2:
I faced the same issue today. Below were the findings for my case,
I asked one of my friend to generate an SQL dump for me to import. He used sequel-pro
to generate the dump (export database). When I did the import it threw an error Cannot create a JSON value from a string with CHARACTER SET 'binary'
So, there was an issue with the generated dump, all the json
fields were converted to some raw format i.e. instead of value being
"{'key1':'value1', 'key2':'value2'}"
it was,
X'nfdsklsdsklnfjkbvkjsdbvkjhdfsbvkjdsbnvljkdsbvkjhdfbvkjdfbvjkdfb'
So, when importing the dump i.e. running the insert
statements mysql
could not process the data as it was not of json
type.
Here is a link to the bug reported
https://github.com/sequelpro/sequelpro/issues/2397
来源:https://stackoverflow.com/questions/48526332/mysql-5-7-20-cannot-create-a-json-value-from-a-string-with-character-set-binar