如何使用 MySQLContainer 设置全局变量

How to set a global variable using MySQLContainer

有一种设置全局变量的方法: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[...]

因为您可以将配置参数传递给您的 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");