MyCATApache/Mycat-Server · error · java.lang.RuntimeException

please sql:use schema before load data

Error message

please sql:use schema  before load data

What it means

ServerLoadDataInfileHandler.start resolves the current schema from the ServerConnection before processing a LOAD DATA INFILE. If the connection has no schema set (serverConnection.getSchema() returns null or maps to nothing in the Mycat config), it throws this RuntimeException because LOAD DATA requires a schema context to resolve the target table.

Solutions

  1. Run 'USE <schema>;' on the connection before the LOAD DATA INFILE statement.
  2. Verify the schema name exists in Mycat's schema.xml / server config and matches exactly.
  3. Add the missing schema to Mycat configuration if it is genuinely intended to exist.
  4. Reconnect with a connection URL that selects the default schema (jdbc:mysql://host/db or mysql -D db).

Example fix

// before
LOAD DATA INFILE '/tmp/data.csv' INTO TABLE mytable;
// after
USE mydb;
LOAD DATA INFILE '/tmp/data.csv' INTO TABLE mytable;
Defensive patterns

Strategy: validation

Validate before calling

// ensure a schema is selected before LOAD DATA
try (Statement s = conn.createStatement()) {
    s.execute("USE mydb");
}
if (conn.getCatalog() == null || conn.getCatalog().isEmpty())
    throw new IllegalStateException("no schema selected");

Try / catch

try { loadDataInfile(...); } catch (RuntimeException e) { if (e.getMessage().contains("use schema")) { selectSchema(conn, "mydb"); retry(); } else throw e; }

Prevention

When it happens

Trigger: Executing 'LOAD DATA INFILE ...' on a Mycat connection without first executing 'USE <schema>', or using a schema name not present in Mycat's schema configuration (schema.xml).

Common situations: mysql client sessions that issue LOAD DATA immediately after connect without USE; load scripts pointed at the default MySQL 'test' schema absent from Mycat config; misconfigured schema names differing in case.

Related errors


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

Appendix: source

Thrown at src/main/java/io/mycat/server/handler/ServerLoadDataInfileHandler.java:166

        this.sql = sql;


        SQLStatementParser parser = new MycatStatementParser(sql);
        statement = (MySqlLoadDataInFileStatement) parser.parseStatement();
        if (statement.getSetList().size() > 0) {
            varColumns = parseParam2RealColumn(statement);
        }
        fileName = parseFileName(sql);

        if (fileName == null) {
            serverConnection.writeErrMessage(ErrorCode.ER_FILE_NOT_FOUND, " file name is null !");
            clear();
            return;
        }
        schema = MycatServer.getInstance().getConfig()
                .getSchemas().get(serverConnection.getSchema());
        if (schema == null) {
            throw new RuntimeException("please sql:use schema  before load data");
        }
        tableId2DataNodeCache = (LayerCachePool) MycatServer.getInstance().getCacheService().getCachePool("TableID2DataNodeCache");
        tableName = statement.getTableName().getSimpleName().toUpperCase();
        tableConfig = schema.getTables().get(tableName);
        if (tableConfig.getRule() != null && tableConfig.getRule().getRuleAlgorithm() instanceof SlotFunction) {
            shoudAddSlot = true;
        }
        tempPath = SystemConfig.getHomePath() + File.separator + "temp" + File.separator + serverConnection.getId() + File.separator;
        tempFile = tempPath + "clientTemp.txt";
        tempByteBuffer = new ByteArrayOutputStream();

        List<SQLExpr> columns = statement.getColumns();
        if (tableConfig != null) {
            String pColumn = getPartitionColumn();
            //如果有变量set表达式,columns里面会包含变量的名称,改用varColumns以获取真实的列名称
            List<SQLExpr> columnsTmp = varColumns == null ? columns : varColumns;
            if (pColumn != null && columnsTmp != null && columns.size() > 0) {
                for (int i = 0, columnsSize = columnsTmp.size(); i < columnsSize; i++) {

View on GitHub (pinned to 65f8d8beb7)