You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
databasename part of jdbc:tc URIs іs ignored when optional host:port pair is omitted #4121
Note that this issue is related to behavior mentioned in #1846.
When one uses "host-less URI" as specified on the JDBC support page, e.g.: jdbc:tc:mysql:5.7.12:///mydatabase, DB name is also ignored and a default test name is used instead.
This code fragment from org.testcontainers.jdbc.ConnectionUrl#parseUrl method clearly shows how it is handled:
//In case it matches to the default patternMatcherdbInstanceMatcher = Patterns.DB_INSTANCE_MATCHING_PATTERN.matcher(dbHostString);
if (dbInstanceMatcher.matches()) {
databaseHost = Optional.of(dbInstanceMatcher.group(1));
databasePort = Optional.ofNullable(dbInstanceMatcher.group(3)).map(value -> Integer.valueOf(value));
databaseName = Optional.of(dbInstanceMatcher.group(4));
}
then org.testcontainers.containers.JdbcDatabaseContainerProvider#newInstanceFromConnectionUrl defaults DB name to test:
In my particular case an init SQL script, which starts with the following instruction, fails with Caused by: java.sql.SQLSyntaxErrorException: Access denied for user 'test'@'%' to 'mydatabase' error:
Note that the hostname, port and database name will be ignored; you can leave these as-is or set them to any value.
which apparently is not the case and /// syntax, which includes DB name only, is recommended and multiple examples are provided.
I suggest to improve "host-less" URI handling by honoring DB name and clarify the documentation on this matter. Will make a PR for this if project owners are OK with such resolution.
changed the title [-]`databasename` part of jdbc:tc URI іs ignored when optional host:port pair is omitted[/-][+]`databasename` part of `jdbc:tc` URIs іs ignored when optional host:port pair is omitted[/+]on May 24, 2021
Other than specifying host/port, I found another workaround was just to add a fourth / which causes it to put / in databaseHost which is subsequently ignored by testcontainers anyway.
Let's keep this issue open, thanks for providing the reproducer @chadlwilson and thanks @Alexander-Kiselyov for the detailed investigation.
Before we invest any work on this issue, is there a good reason to use the host-less URI format? Bringing the docs in line with the implementation is of course critical.
From my perspective, I think it's less confusing when reading one's config to have host-less URIs because you don't have to see a misleading localhost:3306 which is going to be thrown away/ignored by testcontainers anyway. Probably makes the docs a bit cleaner to read where there are a lot of different examples for different DBs as well.
That's the justification in the current docs as well:
We will use /// (host-less URIs) from now on to emphasis the unimportance of the host:port pair.
From Testcontainers' perspective, jdbc:mysql:5.7.34://localhost:3306/databasename and jdbc:mysql:5.7.34:///databasename is the same URI.
Is there any reason that this is not approached (either by fixing the implementation and/or the docs)? I just spent half a day trying to find out why test was used instead of the URL-defined database name. 😕
In fact, my tests showed that testcontainers-postgresql, testcontainers-mssqlserver, testcontainers-clickhouse, and even the external org.firebirdsql:firebird-testcontainers-java did not have this problem.
This issue only occurs in the Maven module of testcontainers-mysql.
Seems like a perfectly good candidate for an open source pull request, for those it is important to - especially given there are simple enough reproducers.
Seems like a perfectly good candidate for an open source pull request, for those it is important to - especially given there are simple enough reproducers.
Fair, and I have submitted pull requests to various other projects in the past. We are not currently using this library however, so it won’t be me for anyone reading.
For triage: this looks fixed by #10603 ("Fix support to set a custom database name in Host-less JDBC urls"), released in 2.0.0. On main, ConnectionUrl parses jdbc:tc:mysql:5.7.12:///mydatabase (and the same with a query string) to database name mydatabase with no host, so the provider no longer falls back to test. The issue can probably be closed.
Activity
[-]`databasename` part of jdbc:tc URI іs ignored when optional host:port pair is omitted[/-][+]`databasename` part of `jdbc:tc` URIs іs ignored when optional host:port pair is omitted[/+]chadlwilson commentedon Oct 14, 2021
I just came across this too, with both
1.15.3and1.16.0.Given it contradicts the documentation and the
///syntax works fine forr2dbcconnections, I think this is a bug in the URL parsing?There are also no tests for such URLs at https://github.com/testcontainers/testcontainers-java/blob/3aa5fbfa17be09f4e42b2300396890f76f508c86/modules/jdbc/src/test/java/org/testcontainers/jdbc/ConnectionUrlTest.java
This seems to be a re-prod:
Other than specifying host/port, I found another workaround was just to add a fourth
/which causes it to put/indatabaseHostwhich is subsequently ignored by testcontainers anyway.chadlwilson commentedon Oct 14, 2021
Seems this is a dupe of #2544 which keeps getting closed for staleness.
kiview commentedon Oct 14, 2021
Let's keep this issue open, thanks for providing the reproducer @chadlwilson and thanks @Alexander-Kiselyov for the detailed investigation.
Before we invest any work on this issue, is there a good reason to use the host-less URI format? Bringing the docs in line with the implementation is of course critical.
chadlwilson commentedon Oct 14, 2021
From my perspective, I think it's less confusing when reading one's config to have host-less URIs because you don't have to see a misleading
localhost:3306which is going to be thrown away/ignored by testcontainers anyway. Probably makes the docs a bit cleaner to read where there are a lot of different examples for different DBs as well.That's the justification in the current docs as well:
eschulma commentedon Jan 30, 2023
I like the host-less URIs as well, but until this is fixed please update the documentation. I wasted a day on this.
odrotbohm commentedon Jul 9, 2024
Is there any reason that this is not approached (either by fixing the implementation and/or the docs)? I just spent half a day trying to find out why
testwas used instead of the URL-defined database name. 😕linghengqian commentedon Dec 23, 2025
In fact, my tests showed that
testcontainers-postgresql,testcontainers-mssqlserver,testcontainers-clickhouse, and even the externalorg.firebirdsql:firebird-testcontainers-javadid not have this problem.This issue only occurs in the Maven module of
testcontainers-mysql.eschulma commentedon Dec 23, 2025
So will it be fixed?
chadlwilson commentedon Dec 23, 2025
Seems like a perfectly good candidate for an open source pull request, for those it is important to - especially given there are simple enough reproducers.
eschulma commentedon Dec 23, 2025
Fair, and I have submitted pull requests to various other projects in the past. We are not currently using this library however, so it won’t be me for anyone reading.
sharanggupta commentedon Oct 14, 2026
For triage: this looks fixed by #10603 ("Fix support to set a custom database name in Host-less JDBC urls"), released in 2.0.0. On
main,ConnectionUrlparsesjdbc:tc:mysql:5.7.12:///mydatabase(and the same with a query string) to database namemydatabasewith no host, so the provider no longer falls back totest. The issue can probably be closed.