{"record":{"id":"0a4bdb854139e496","repo":"MyCATApache/Mycat-Server","slug":"please-sql-use-schema-before-load-data","errorCode":null,"errorMessage":"please sql:use schema  before load data","messagePattern":"please sql:use schema  before load data","errorType":"exception","errorClass":"java.lang.RuntimeException","httpStatus":null,"severity":"error","filePath":"src/main/java/io/mycat/server/handler/ServerLoadDataInfileHandler.java","lineNumber":166,"sourceCode":"        this.sql = sql;\n\n\n        SQLStatementParser parser = new MycatStatementParser(sql);\n        statement = (MySqlLoadDataInFileStatement) parser.parseStatement();\n        if (statement.getSetList().size() > 0) {\n            varColumns = parseParam2RealColumn(statement);\n        }\n        fileName = parseFileName(sql);\n\n        if (fileName == null) {\n            serverConnection.writeErrMessage(ErrorCode.ER_FILE_NOT_FOUND, \" file name is null !\");\n            clear();\n            return;\n        }\n        schema = MycatServer.getInstance().getConfig()\n                .getSchemas().get(serverConnection.getSchema());\n        if (schema == null) {\n            throw new RuntimeException(\"please sql:use schema  before load data\");\n        }\n        tableId2DataNodeCache = (LayerCachePool) MycatServer.getInstance().getCacheService().getCachePool(\"TableID2DataNodeCache\");\n        tableName = statement.getTableName().getSimpleName().toUpperCase();\n        tableConfig = schema.getTables().get(tableName);\n        if (tableConfig.getRule() != null && tableConfig.getRule().getRuleAlgorithm() instanceof SlotFunction) {\n            shoudAddSlot = true;\n        }\n        tempPath = SystemConfig.getHomePath() + File.separator + \"temp\" + File.separator + serverConnection.getId() + File.separator;\n        tempFile = tempPath + \"clientTemp.txt\";\n        tempByteBuffer = new ByteArrayOutputStream();\n\n        List<SQLExpr> columns = statement.getColumns();\n        if (tableConfig != null) {\n            String pColumn = getPartitionColumn();\n            //如果有变量set表达式，columns里面会包含变量的名称，改用varColumns以获取真实的列名称\n            List<SQLExpr> columnsTmp = varColumns == null ? columns : varColumns;\n            if (pColumn != null && columnsTmp != null && columns.size() > 0) {\n                for (int i = 0, columnsSize = columnsTmp.size(); i < columnsSize; i++) {","sourceCodeStart":148,"sourceCodeEnd":184,"githubUrl":"https://github.com/MyCATApache/Mycat-Server/blob/65f8d8beb752f935752f2a0eec0ab017facab9ef/src/main/java/io/mycat/server/handler/ServerLoadDataInfileHandler.java#L148-L184","documentation":"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.","triggerScenarios":"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).","commonSituations":"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.","solutions":["Run 'USE <schema>;' on the connection before the LOAD DATA INFILE statement.","Verify the schema name exists in Mycat's schema.xml / server config and matches exactly.","Add the missing schema to Mycat configuration if it is genuinely intended to exist.","Reconnect with a connection URL that selects the default schema (jdbc:mysql://host/db or mysql -D db)."],"exampleFix":"// before\nLOAD DATA INFILE '/tmp/data.csv' INTO TABLE mytable;\n// after\nUSE mydb;\nLOAD DATA INFILE '/tmp/data.csv' INTO TABLE mytable;","handlingStrategy":"validation","validationCode":"// ensure a schema is selected before LOAD DATA\ntry (Statement s = conn.createStatement()) {\n    s.execute(\"USE mydb\");\n}\nif (conn.getCatalog() == null || conn.getCatalog().isEmpty())\n    throw new IllegalStateException(\"no schema selected\");","typeGuard":null,"tryCatchPattern":"try { loadDataInfile(...); } catch (RuntimeException e) { if (e.getMessage().contains(\"use schema\")) { selectSchema(conn, \"mydb\"); retry(); } else throw e; }","preventionTips":["Always issue USE <schema> in load scripts.","Set a default schema in the connection URL.","Verify schema names in Mycat schema.xml before deployment."],"tags":["load-data","schema","precondition"],"backgroundTag":"authentication-required","analyzedSha":"65f8d8beb752f935752f2a0eec0ab017facab9ef","analyzedAt":"2026-09-11T00:12:21.696Z","contentChangedAt":"2026-09-11T00:12:21.696Z","schemaVersion":2},"datasetVersion":"2026-09-16T04:17:20.429Z"}