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

后端 未结 12 1828
攒了一身酷
攒了一身酷 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:23

    I had this problem dealing with exports made by Sequel Pro. I unchecked the Output BLOB fields as hex option and the problem went away. Visually inspecting the export showed legible JSON instead of binary.

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

    This odd issue was occurring when running a simple UPDATE query:

    update some_table set json_attr = '{"test":168}' where id = 123456;
    

    Restarting MySQL fixed it. Was not able to pinpoint the cause.

    Edit: We are using Aurora. It looks like it was related to us having a weird configuration where the same instance handled both master & slave/reader connections.

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

    change collation to utf8_general_ci. worked for me.

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

    All MySQL JSON data type information must be UTF8MB4 character set not BINARY.

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

    Lorcan's answer did help me well as a start, but converting all binary values created a bunch of other error messages like Duplicate entry [...] for key 'PRIMARY'. Finally I figured out that JSON entries all started with 5B or 7B, and closed with 5D or 7D, which of course means they start with [ or { and end with ] or }. So what worked for me was to regex-replace only those entries:

    Find:    (X'5B[^,\)]*5D')
    Replace: CONVERT($1 using utf8mb4)
    

    then

    Find:    (X'7B[^,\)]*7D')
    Replace: CONVERT($1 using utf8mb4)
    

    Et voilá, all import errors gone! (At least for my case)

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

    For those using Sequel Pro around June 2019, in addition to unchecking the "Output BLOB fields as hex option" (as mentioned above) - you also need to use the nightly build, which added support for JSON types 2 years ago. This support has not yet made it to the official release.

    0 讨论(0)
提交回复
热议问题