如何在一个 mysql db 容器上创建多个数据库?或者我是否必须为每个数据库创建 db 容器?当我尝试从 sql 文件脚本创建它时:
@ClassRule public static MySQLContainer dbService = (MySQLContainer) new MySQLContainer()
.withPassword("test")
.withUsername("mysqlroot")
.withDatabaseName("test")
.withInitScript("init_mysql.sql")
.withExposedPorts(DB_PORT)
.withNetwork(network)
.withNetworkAliases("dbService")
;
我收到一个错误:
Caused by: org.testcontainers.ext.ScriptUtils$ScriptStatementFailedException: Script execution failed (init_mysql.sql:2): CREATE DATABASE IF NOT EXISTS `b2c`
at org.testcontainers.jdbc.JdbcDatabaseDelegate.execute(JdbcDatabaseDelegate.java:49)
at org.testcontainers.delegate.AbstractDatabaseDelegate.execute(AbstractDatabaseDelegate.java:34)
at org.testcontainers.ext.ScriptUtils.executeDatabaseScript(ScriptUtils.java:331)
... 19 more
Caused by: java.sql.SQLSyntaxErrorException: Access denied for user 'mysqlroot'@'%' to database 'b2c'
at com.mysql.cj.jdbc.exceptions.SQLError.createSQLException(SQLError.java:120)
at com.mysql.cj.jdbc.exceptions.SQLError.createSQLException(SQLError.java:97)
at com.mysql.cj.jdbc.exceptions.SQLExceptionsMapping.translateException(SQLExceptionsMapping.java:122)
at com.mysql.cj.jdbc.StatementImpl.executeInternal(StatementImpl.java:782)
at com.mysql.cj.jdbc.StatementImpl.execute(StatementImpl.java:666)
at org.testcontainers.jdbc.JdbcDatabaseDelegate.execute(JdbcDatabaseDelegate.java:42)
... 21 more
内容init_mysql.sql
CREATE TABLE bar (
foo VARCHAR(255)
);
CREATE DATABASE IF NOT EXISTS `b2c`;
我尝试了很多配置。对我来说,除了在容器创建时配置的专用数据库外,我似乎无法执行 sql。请帮忙。我有 6 个数据库,为每个数据库创建专用容器是浪费资源。