簡體   English   中英

MySQL TestContainers 初始化腳本架構創建失敗

[英]MySQL TestContainers init-script Schema Creation Failing

嘗試在 MySQL 測試容器實例中創建架構時遇到以下問題。 似乎測試用戶沒有適當的權限?

配置:

spring.datasource.driver-class-name=org.testcontainers.jdbc.ContainerDatabaseDriver
spring.datasource.url=jdbc:tc:mysql:5.7.22:///test?TC_INITSCRIPT=component/db/init_mysql.sql
Caused by: org.testcontainers.ext.ScriptUtils$UncategorizedScriptException: Failed to execute database script from resource [create schema new_schema CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;]
    at org.testcontainers.ext.ScriptUtils.executeDatabaseScript(ScriptUtils.java:375)
    at org.testcontainers.ext.ScriptUtils.executeDatabaseScript(ScriptUtils.java:313)
    at org.testcontainers.jdbc.ContainerDatabaseDriver.runInitScriptIfRequired(ContainerDatabaseDriver.java:196)
    at org.testcontainers.jdbc.ContainerDatabaseDriver.connect(ContainerDatabaseDriver.java:132)
    at com.zaxxer.hikari.util.DriverDataSource.getConnection(DriverDataSource.java:136)
    at com.zaxxer.hikari.pool.PoolBase.newConnection(PoolBase.java:369)
    at com.zaxxer.hikari.pool.PoolBase.newPoolEntry(PoolBase.java:198)
    at com.zaxxer.hikari.pool.HikariPool.createPoolEntry(HikariPool.java:467)
    at com.zaxxer.hikari.pool.HikariPool.checkFailFast(HikariPool.java:541)
    ... 61 more
Caused by: org.testcontainers.ext.ScriptUtils$ScriptStatementFailedException: Script execution failed (component/db/init_mysql.sql:1): create schema new_schema CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci
    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:362)
    ... 69 more
Caused by: java.sql.SQLSyntaxErrorException: Access denied for user 'test'@'%' to database 'new_schema'
    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)
    ... 71 more

組件/db/init_mysql.sql

create schema new_schema CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;

在 JDBC 集成之外創建 mysql 映像時看到類似問題:

private static final MySQLContainer mysqlContainer = new MySQLContainer<>(DockerImageName.parse("mysql:5.7"))
            .withInitScript("component/db/init_mysql.sql")
            .withUsername("user")
            .withPassword("password")
            .withExposedPorts(3306)
            .withReuse(true);

關於如何在不使用預先創建的架構創建自定義 docker 圖像的情況下實現這一目標的任何想法?

任何幫助將不勝感激我覺得我必須在這里遺漏一些東西。

嘗試創建whatever容器並使用與test用戶相同的root密碼,在本例中為 .

@Container
private static MySQLContainer database = new MySQLContainer(MYSQL_DOCKER_TEST_VERSION)
            .withUsername("test")
            .withPassword("whatever")
...

通過@DynamicPropertySource 動態添加屬性。

    @DynamicPropertySource
    static void databaseProperties(DynamicPropertyRegistry registry) {
        registry.add("spring.datasource.url", database::getJdbcUrl);
        registry.add("spring.datasource.username", database::getUsername);
        registry.add("spring.datasource.password", database::getPassword);
        registry.add("flyway.user", () -> "root");
        registry.add("flyway.password", database::getPassword);
    }

我沒有使用flyway,但我相信它應該可以工作。

通過使用預先創建的模式創建自定義 MySQL 映像來解決此問題。 很想聽聽是否有更好的方法。

正如在 testcontainers 存儲庫上創建的測試中所見,解決方法是指定 root 用戶的密碼並在測試中使用 root 用戶:

MySQLContainer<?> db = new MySQLContainer<>(image)
            .withUsername("test")
            .withPassword("test")
            .withEnv("MYSQL_ROOT_PASSWORD", "test")
            ...

Reference: https://github.com/testcontainers/testcontainers-java/commit/2fa00ddbd1f42a20656d5e39d58cdedfbd483dcf#diff-f49cf0d09246a5351504ca32a08deb74e80ac7d61ad012f02d8f09c36508737bR65

我遇到了同樣的問題,我使用以下方法解決了它:

    @Container
     public static JdbcDatabaseContainer mariaDB = (JdbcDatabaseContainer) new MariaDBContainer("mariadb:10.5.11-focal")
    .withDatabaseName("dbName")
    .withInitScript("db.migration/init.sql")
    .withUsername("your user")
    .withPassword("your password")
    .withUrlParam("param", "1")
    .withUrlParam("param", "2");

基本上,我通過 POJO 創建數據庫。 有點hackish但有效。 但也許使用自定義密碼創建 root 用戶是一個更好的解決方案。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM