MySQL 5.7.12 import cannot create a JSON value from a string with CHARACTER SET 'binary'

后端 未结 12 1827
攒了一身酷
攒了一身酷 2020-12-07 08:45

I exported my database with JSON columns in it. After I migrated to a new server, my import crashed every time with an error like:

cannot create a JSO

相关标签:
12条回答
  • 2020-12-07 09:10

    I had this problem with a dump. i was able to fix it by changing the line in the dump file from:

    /*!40101 SET NAMES binary*/;
    

    to

    /*!40101 SET NAMES utf8mb4*/;
    
    0 讨论(0)
  • 2020-12-07 09:11

    This worked for me, (I had control of the export to the sql file as well). There're lots of caveats; e.g. I knew that the fields would never be bigger than 1000 and wouldn't contain any non-ascii chars. Please do comment and tell me all the whys this is so bad tho :)

    Before export

    alter table <table> modify <json_column> varchar(1000);
    

    Then after import

    alter table <table> modify <json_column> json;
    
    0 讨论(0)
  • 2020-12-07 09:14

    For the ones like me arived here using Symfony 4 / Doctrine : For some reasons the same entity can be resolved in a longtext MySQL type storing JSON; or a json MySQL type storing json. Manually setting longtext MySQL type resolved the problem in my particular case.

    0 讨论(0)
  • 2020-12-07 09:20

    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

    You need to uncheck the Output BLOB fields as hex option.

    0 讨论(0)
  • 2020-12-07 09:21

    You can apply a regex to the SQL text which you exported which will convert your binary strings into an insertable format. This was my quick and dirty fix when I faced this issue

    (X'[^,\)]*')
    CONVERT($1 using utf8mb4)
    

    Applying this regex means

    INSERT INTO json_table (json_column) VALUES (X'7B22666F6F223A2022626172227D');
    

    will now become

    INSERT INTO json_table (json_column) VALUES (CONVERT(X'7B22666F6F223A2022626172227D' using utf8mb4));
    
    0 讨论(0)
  • 2020-12-07 09:22

    vim version For Lorcan O'Neill's answer

    vi xxxx.sql
    :%s/\(X'[^,\)]*'\)/CONVERT(\1 using utf8mb4)/g
    
    0 讨论(0)
提交回复
热议问题