MyCATApache/Mycat-Server · error · ParserException

syntax error, illegal charset

Error message

syntax error, illegal charset

What it means

The LOAD DATA INFILE parser requires the CHARACTER SET keyword to be followed by a quoted charset name (a string literal token). If the lexer does not produce LITERAL_CHARS after 'CHARACTER SET', Mycat throws this ParserException because the charset clause cannot be resolved. It is a SQL syntax guard inside Mycat's Druid-based LOAD DATA parsing (parseLoadDataInFile).

Solutions

  1. Quote the charset name in the LOAD DATA statement: LOAD DATA INFILE 'f.csv' INTO TABLE t CHARACTER SET 'utf8'.
  2. Remove the CHARACTER SET clause entirely if the server/connection default charset is acceptable.
  3. Check the full SQL text reaching Mycat for truncation or stray tokens after CHARACTER SET (e.g. logs/proxy rewriting).

Example fix

// before
LOAD DATA INFILE '/tmp/a.csv' INTO TABLE t CHARACTER SET utf8;
// after
LOAD DATA INFILE '/tmp/a.csv' INTO TABLE t CHARACTER SET 'utf8';
Defensive patterns

Strategy: validation

Validate before calling

// Validate the LOAD DATA statement before sending through Mycat
if (/CHARACTER\s+SET/i.test(sql) && !/CHARACTER\s+SET\s+'[^']+'/i.test(sql)) {
  throw new Error("CHARACTER SET clause must use a quoted charset name");
}

Prevention

When it happens

Trigger: Executing a LOAD DATA INFILE statement whose 'CHARACTER SET' clause is followed by something other than a quoted string literal, e.g. CHARACTER SET utf8 without quotes, a truncated statement, or a comment/garbage token where the charset name should be.

Common situations: Clients generating LOAD DATA statements with unquoted charsets (some MySQL docs examples omit quotes); truncated or hand-edited SQL sent to Mycat; Mycat's stricter parser rejecting SQL that plain MySQL tolerates.

Understand the failure class

Background: "Invalid ... format", "must be in format X", "does not look like a ..." — invalid argument format errors across CLI tools and libraries — this error's family across 17 libraries.

Related errors


AI-assisted analysis of MyCATApache/Mycat-Server@65f8d8beb7 (2026-09-11). Data as JSON: /api/errors/93c94ff035e9cca6. Report an issue: GitHub.

Appendix: source

Thrown at src/main/java/io/mycat/route/parser/druid/MycatStatementParser.java:98

        }

        if (identifierEquals(IGNORE)) {
            stmt.setIgnore(true);
            lexer.nextToken();
        }

        accept(Token.INTO);
        accept(Token.TABLE);

        SQLName tableName = exprParser.name();
        stmt.setTableName(tableName);

        if (identifierEquals(CHARACTER)) {
            lexer.nextToken();
            accept(Token.SET);

            if (lexer.token() != Token.LITERAL_CHARS) {
                throw new ParserException("syntax error, illegal charset");
            }

            String charset = lexer.stringVal();
            lexer.nextToken();
            stmt.setCharset(charset);
        }

        if (identifierEquals("FIELDS") || identifierEquals("COLUMNS")) {
            lexer.nextToken();
            if (identifierEquals("TERMINATED")) {
                lexer.nextToken();
                accept(Token.BY);
                stmt.setColumnsTerminatedBy(new SQLCharExpr(lexer.stringVal()));
                lexer.nextToken();
            }

            if (identifierEquals("OPTIONALLY")) {
                stmt.setColumnsEnclosedOptionally(true);

View on GitHub (pinned to 65f8d8beb7)