{"record":{"id":"e712bbf2f9f46cf4","repo":"apache/shardingsphere","slug":"1046","errorCode":"1046","errorMessage":"No database selected","messagePattern":"No database selected","errorType":"exception","errorClass":"NoDatabaseSelectedException","httpStatus":null,"severity":"error","filePath":"proxy/backend/dialect/mysql/src/main/java/org/apache/shardingsphere/proxy/backend/mysql/handler/admin/executor/select/UnicastResourceShowExecutor.java","lineNumber":99,"sourceCode":"        ShardingSpherePreconditions.checkState(metaData.getDatabase(databaseName).containsDataSource(), () -> new EmptyStorageUnitException(databaseName));\n        HintValueContext hintValueContext = SQLHintUtils.extractHint(sql);\n        try {\n            connectionSession.setCurrentDatabaseName(databaseName);\n            SQLStatementContext sqlStatementContext = new SQLBindEngine(metaData, connectionSession.getCurrentDatabaseName(), hintValueContext).bind(sqlStatement);\n            databaseProxyConnector = DatabaseProxyConnectorFactory.newInstance(new QueryContext(\n                    sqlStatementContext, sql, Collections.emptyList(), hintValueContext, connectionSession.getConnectionContext(), metaData), connectionSession.getDatabaseConnectionManager(), false);\n            responseHeader = databaseProxyConnector.execute();\n            mergedResult = new TransparentMergedResult(createQueryResult());\n        } finally {\n            connectionSession.setCurrentDatabaseName(originDatabase);\n            databaseProxyConnector.close();\n        }\n    }\n    \n    private String getFirstDatabaseName(final ShardingSphereMetaData metaData) {\n        Collection<ShardingSphereDatabase> databases = metaData.getAllDatabases();\n        if (databases.isEmpty()) {\n            throw new NoDatabaseSelectedException();\n        }\n        Optional<ShardingSphereDatabase> result = databases.stream().filter(ShardingSphereDatabase::containsDataSource).findFirst();\n        ShardingSpherePreconditions.checkState(result.isPresent(), EmptyStorageUnitException::new);\n        return result.get().getName();\n    }\n    \n    @Override\n    public QueryResultMetaData getQueryResultMetaData() {\n        List<RawQueryResultColumnMetaData> columns = ((QueryResponseHeader) responseHeader).getQueryHeaders().stream().map(QueryHeader::getColumnLabel)\n                .map(each -> new RawQueryResultColumnMetaData(\"\", each, each, Types.VARCHAR, \"VARCHAR\", 100, 0))\n                .collect(Collectors.toList());\n        return new RawQueryResultMetaData(columns);\n    }\n    \n    private QueryResult createQueryResult() throws SQLException {\n        List<MemoryQueryResultDataRow> rows = new LinkedList<>();\n        while (databaseProxyConnector.next()) {\n            List<Object> data = databaseProxyConnector.getRowData().getData();","sourceCodeStart":81,"sourceCodeEnd":117,"githubUrl":"https://github.com/apache/shardingsphere/blob/e952770a215630a3659c75d64369168cd3e26b82/proxy/backend/dialect/mysql/src/main/java/org/apache/shardingsphere/proxy/backend/mysql/handler/admin/executor/select/UnicastResourceShowExecutor.java#L81-L117","documentation":"NoDatabaseSelectedException (error code 1046, MySQL 'No database selected' semantics) thrown by UnicastResourceShowExecutor.getFirstDatabaseName when the proxy metadata contains zero databases. This executor answers unicast SHOW-type queries by picking one database that has a data source and forwarding the query there; with no databases at all there is nothing to route to. A related second guard throws EmptyStorageUnitException when databases exist but none contains a data source.","triggerScenarios":"Running a unicast SHOW statement (e.g. SHOW TABLE STATUS style / SHOW commands executed against a single backend) right after proxy startup before any database is created (no CREATE DATABASE / IMPORT), or in a cluster where metadata has not yet loaded, so metaData.getAllDatabases() is empty.","commonSituations":"Fresh proxy install queried before any database is registered; metadata not yet synchronized from the registry center after node restart (race at startup); accidentally connecting to the wrong (empty) proxy instance/cluster.","solutions":["Create or register at least one database with a storage unit (CREATE DATABASE / ALTER DATABASE ADD RESOURCE or IMPORT DATABASE CONFIGURATION) before running SHOW queries","Wait for metadata load to finish (check SHOW IDENTITY / proxy logs) after startup and retry","Verify you connected to the intended proxy/cluster and that the mode repository is reachable"],"exampleFix":"-- before (fresh proxy, no database)\nSHOW TABLE STATUS;\n\n-- after\nCREATE DATABASE demo;\nUSE demo;\nSHOW TABLE STATUS;","handlingStrategy":"validation","validationCode":"// before running unicast SHOW queries, ensure metadata has a database with a storage unit\nif (metaData.getAllDatabases().isEmpty()) {\n    throw new IllegalStateException(\"No database registered; create one before SHOW queries\");\n}\nboolean anyWithDataSource = metaData.getAllDatabases().stream().anyMatch(ShardingSphereDatabase::containsDataSource);\nif (!anyWithDataSource) { throw new IllegalStateException(\"No storage unit registered\"); }","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Run CREATE DATABASE / resource registration as part of provisioning before smoke-testing SHOW commands","After proxy restart, wait for metadata load from the registry center before querying"],"tags":["mysql","show","metadata","routing","startup"],"backgroundTag":null,"analyzedSha":"e952770a215630a3659c75d64369168cd3e26b82","analyzedAt":"2026-08-14T13:54:53.392Z","schemaVersion":2},"datasetVersion":"2026-08-15T17:31:12.345Z"}