【问题标题】:How to set a global variable using MySQLContainer如何使用 MySQLContainer 设置全局变量
【发布时间】:2020-10-24 13:41:18
【问题描述】:

有没有办法设置全局变量:explicit_defaults_for_timestamp=true,使用测试docker容器?

我的单元测试中有以下代码:

@Container
public static final MySQLContainer<?> mySQLContainer = new MySQLContainer<>()
        .withDatabaseName("test")
        .withPassword("123")
        .withUsername("test")
        .withExposedPorts(3306)
        .withInitScript("database-creation-scripts/mysql-install.sql");

容器启动,但默认值为false:

mysql> SHOW GLOBAL VARIABLES LIKE '%timestamp%';
+---------------------------------+-------+
| Variable_name                   | Value |
+---------------------------------+-------+
| explicit_defaults_for_timestamp | OFF   |
| log_timestamps                  | UTC   |
+---------------------------------+-------+
2 rows in set (0.00 sec)

我找不到在 MySQLContainer 中设置全局变量的方法 .with[...]

【问题讨论】:

    标签: java docker testcontainers


    【解决方案1】:

    您可以使用以下命令行标志将配置参数传递给 MySQL docker 容器:

    docker run --name some-mysql -e MYSQL_ROOT_PASSWORD=my-secret-pw -d mysql:5.7 --explicit_defaults_for_timestamp
    

    您可以使用 `.withCommand() 将其镜像到 Testcontainers:

    @Container
    public static final MySQLContainer<?> mySQLContainer = new MySQLContainer<>()
            .withDatabaseName("test")
            .withPassword("123")
            .withUsername("test")
            .withExposedPorts(3306)
            .withInitScript("database-creation-scripts/mysql-install.sql")
            .withCommand("--explicit_defaults_for_timestamp");
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-01-27
      • 1970-01-01
      • 2021-09-01
      相关资源
      最近更新 更多