apache/shardingsphere · error · NoDatabaseSelectedException
1046
1046
Error message
No database selected
What it means
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.
Source
Thrown at proxy/backend/dialect/mysql/src/main/java/org/apache/shardingsphere/proxy/backend/mysql/handler/admin/executor/select/UnicastResourceShowExecutor.java:99
ShardingSpherePreconditions.checkState(metaData.getDatabase(databaseName).containsDataSource(), () -> new EmptyStorageUnitException(databaseName));
HintValueContext hintValueContext = SQLHintUtils.extractHint(sql);
try {
connectionSession.setCurrentDatabaseName(databaseName);
SQLStatementContext sqlStatementContext = new SQLBindEngine(metaData, connectionSession.getCurrentDatabaseName(), hintValueContext).bind(sqlStatement);
databaseProxyConnector = DatabaseProxyConnectorFactory.newInstance(new QueryContext(
sqlStatementContext, sql, Collections.emptyList(), hintValueContext, connectionSession.getConnectionContext(), metaData), connectionSession.getDatabaseConnectionManager(), false);
responseHeader = databaseProxyConnector.execute();
mergedResult = new TransparentMergedResult(createQueryResult());
} finally {
connectionSession.setCurrentDatabaseName(originDatabase);
databaseProxyConnector.close();
}
}
private String getFirstDatabaseName(final ShardingSphereMetaData metaData) {
Collection<ShardingSphereDatabase> databases = metaData.getAllDatabases();
if (databases.isEmpty()) {
throw new NoDatabaseSelectedException();
}
Optional<ShardingSphereDatabase> result = databases.stream().filter(ShardingSphereDatabase::containsDataSource).findFirst();
ShardingSpherePreconditions.checkState(result.isPresent(), EmptyStorageUnitException::new);
return result.get().getName();
}
@Override
public QueryResultMetaData getQueryResultMetaData() {
List<RawQueryResultColumnMetaData> columns = ((QueryResponseHeader) responseHeader).getQueryHeaders().stream().map(QueryHeader::getColumnLabel)
.map(each -> new RawQueryResultColumnMetaData("", each, each, Types.VARCHAR, "VARCHAR", 100, 0))
.collect(Collectors.toList());
return new RawQueryResultMetaData(columns);
}
private QueryResult createQueryResult() throws SQLException {
List<MemoryQueryResultDataRow> rows = new LinkedList<>();
while (databaseProxyConnector.next()) {
List<Object> data = databaseProxyConnector.getRowData().getData();View on GitHub (pinned to e952770a21)
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
Example fix
-- before (fresh proxy, no database) SHOW TABLE STATUS; -- after CREATE DATABASE demo; USE demo; SHOW TABLE STATUS;
Defensive patterns
Strategy: validation
Validate before calling
// before running unicast SHOW queries, ensure metadata has a database with a storage unit
if (metaData.getAllDatabases().isEmpty()) {
throw new IllegalStateException("No database registered; create one before SHOW queries");
}
boolean anyWithDataSource = metaData.getAllDatabases().stream().anyMatch(ShardingSphereDatabase::containsDataSource);
if (!anyWithDataSource) { throw new IllegalStateException("No storage unit registered"); } Prevention
- 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
When it happens
Trigger: 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.
Common situations: 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.
Related errors
- Can not support type `%s`.
- HiveServer2 in embedded mode has been deprecated by Apache H
- 6
- Unsupported Firebird format code `%s`
- Unsupported format type %s
AI-assisted analysis of apache/shardingsphere@e952770a21 (2026-08-14).
Data as JSON: /api/errors/e712bbf2f9f46cf4.
Report an issue: GitHub.