3

May I know how do we configure the time zone in Testcontainers MySQL during testing? Also, how can we verify the time zone in Testcontainers MySQL?

user3357926
  • 435
  • 1
  • 4
  • 13
  • Can you tell which parts of this document are unclear: https://dev.mysql.com/doc/refman/8.0/en/time-zone-support.html ? – Luuk Aug 29 '21 at 09:03
  • 1
    See the link [here](https://www.testcontainers.org/modules/databases/mysql/) to configure the time zone in Testcontainers MYSQL module. We can define the time zone in my.cnf as one of our test resources, and tell Testcontainers to load it via the URL, e.g. "jdbc:tc:mysql:5.7.34://hostname/databasename?TC_MY_CNF=somepath/mysql_conf_override". – user3357926 Sep 12 '21 at 08:27

2 Answers2

0

You may use the jdbc connection string to make the MySQL testcontainer use your "cnf" file, which sets the timezone

jdbc:tc:mysql:5.7.34://hostname/databasename?TC_MY_CNF=mysql_conf_override

And under mysql_conf_override folder you need to have a my.cnf file with the following content:

[mysqld]
default-time-zone = "Europe/Istanbul"

Finally, you may check the TZ in your testcontainer via

SELECT @@GLOBAL.time_zone, @@SESSION.time_zone;

after connecting to the container:

mysql -u root -ptest
VolkanT
  • 574
  • 5
  • 10
0

It's possible to add an init script setting the timezone:

new MySQLContainer("mysql:8.0.32").withInitScript("init.sql");

Then you add to your init.sql file the following command:

-- Here you can put other SQL commands

SET time_zone =  'UTC';
Luan Kevin Ferreira
  • 1,184
  • 2
  • 16
  • 40