Spring + MySQL (+ MyBatis) 환경에서 view에 있는 컬럼을 대상으로 조건문에 한글을 사용해 SELECT 문 실행 시 Cannot convert string from utf8mb4 to binary
오류가 뜨거나, 확실히 1건 이상의 검색 결과가 있어야 함에도 불구하고 검색이 되지 않는 오류가 발생했다.
SELECT
ARTICLE.ARTICLE_ID,
ARTICLE.ARTICLE_TITLE
FROM
VIEW_ARTICLE ARTICLE
WHERE
ARTICLE.ARTICLE_TITLE LIKE CONCAT('%', '테스트', '%')
Cannot convert string '\xEA\xB4...' from utf8mb4 to binary
Cannot convert string '\xEA\xB4...' from utf8mb4 to binary
Cannot convert string '\xEA\xB4...' from utf8mb4 to binary
Cannot convert string '\xEA\xB4...' from utf8mb4 to binary
Cannot convert string '\xEA\xB4...' from utf8mb4 to binary
Cannot convert string '\xEA\xB4...' from utf8mb4 to binary
Cannot convert string '\xEA\xB4...' from utf8mb4 to binary
Cannot convert string '\xEA\xB4...' from utf8mb4 to binary
Cannot convert string '\xEA\xB4...' from utf8mb4 to binary
=
검색SELECT
ARTICLE.ARTICLE_ID,
ARTICLE.ARTICLE_TITLE,
MEMBER.MEMBER_ID,
MEMBER.MEMBER_NAME
FROM
VIEW_ARTICLE ARTICLE
LEFT OUTER JOIN VIEW_MEMBER MEMBER
ON ARTICLE.WRITER_ID = MEMBER.MEMBER_ID
WHERE
MEMBER.MEMBER_NAME = '관리자'
Cannot convert string '\xEA\xB4...' from utf8mb4 to binary
Cannot convert string '\xEA\xB4...' from utf8mb4 to binary
Cannot convert string '\xEA\xB4...' from utf8mb4 to binary
Cannot convert string '\xEA\xB4...' from utf8mb4 to binary
Cannot convert string '\xEA\xB4...' from utf8mb4 to binary
Cannot convert string '\xEA\xB4...' from utf8mb4 to binary
Cannot convert string '\xEA\xB4...' from utf8mb4 to binary
Cannot convert string '\xEA\xB4...' from utf8mb4 to binary
Cannot convert string '\xEA\xB4...' from utf8mb4 to binary
MySql 8.0.32 버전에만 존재하는 버그로, SHOW VARIABLES LIKE 'version';
문 실행 시 나오는 버전이 해당 버전이라면 다음과 같은 방법을 통해 조치할 수 있다. 이후 버전에서는 수정되었다고 한다.
SELECT문 실행 전
SET optimizer_switch='derived_condition_pushdown=off';
문 실행
해당 버그 리포트는 다음 버그와 중복되는 내용이라고 한다.
MySQL Bug Report
Documented fix as follows in the MySQL 8.0.33 changelog:
When cloning a condition to push down to a derived table,
characters in strings representing conditions were converted to
utfmb4 correctly only for values less than 128 (the ASCII
subset), and code points outside the ASCII subset were converted
to invalid characters, causing the resulting character strings
to become invalid. For derived tables without UNIONS, this led
to problems when a column name from the derived table used
characters outside the ASCII subset, and was used in the WHERE
condition. For derived tables with UNIONS it created problems
when a character outside the ASCII subset was present in a WHERE
condition.
We fix these issues by initializing the string used in such
cases for representing the condition to the connection character
set.
Closed.
Posted by developer:
As a workaround, set derived condition pushdown to off like this:
set optimizer_switch="derived_condition_pushdown=off";