Help Center/ Data Lake Insight/ FAQs/ Flink Jobs/ Flink SQL/ Why Is the RDS Database Time Read by a Flink Opensource SQL Job Different from RDS Database Time?
Updated on 2023-05-19 GMT+08:00

Why Is the RDS Database Time Read by a Flink Opensource SQL Job Different from RDS Database Time?

The time zone of a database is improperly set. Generally, there is a 13-hour difference.

Run the following statement in the RDS database:

show variables like '%time_zone%'

The execution result if as follows:

  • time_zone indicates the database time zone. The value SYSTEM indicates the system time of the database server ('system_time_zone'). The value of the system time zone is CST. Therefore, the database time zone is CST.
  • system_time_zone indicates the time zone of the server where the database is located. The server is a computer.

    If the default time zone of the computer where the local database is located is China Standard Time, the value of system_time_zone is CST.

The real problem here is that time_zone is set to SYSTEM and system_time_zone is set to CST for an RDS for MySQL database. In the MySQL database, CST is China Standard Time (UTC+8), but it is Central Standard Time (USA) (UTC-5) in Java. Flink TaskManager is a Java process that reads the time zone set in the JDBC driver code of the MySQL database through TimeZone.getTimeZone(canonicalTimezone). The method gets time zone CST (UTC+8), but the actual time zone is CST (UTC-5).

Solution (either of the following method)

  1. Do not set the value of time_zone to SYSTEM. You can set it to, for example, +08:00.
  2. Add the time zone to jdbcUrl.

    For example, jdbc:mysql://localhost:3306/test?serverTimezone=Asia/Shanghai.