How do I set the time zone of MySQL?

后端 未结 20 3274
闹比i
闹比i 2020-11-21 07:07

On one server, when I run:

mysql> select now();
+---------------------+
| now()               |
+---------------------+
| 2009-05-30 16:54:29 |
+---------         


        
相关标签:
20条回答
  • 2020-11-21 07:21

    In my case, the solution was to set serverTimezone parameter in Advanced settings to an appropriate value (CET for my time zone).

    As I use IntelliJ, I use its Database module. While adding a new connection to the database and after adding all relevant parameters in tab General, there was an error on "Test Connection" button. Again, the solution is to set serverTimezone parameter in tab Advanced.

    0 讨论(0)
  • 2020-11-21 07:26

    For anyone still having this issue:

    value="jdbc:mysql://localhost:3306/dbname?serverTimezone=UTC"
    

    Worked for me. Just append ?serverTimezone=UTC at the end.

    0 讨论(0)
  • 2020-11-21 07:27

    This work for me for a location in India:

    SET GLOBAL time_zone = "Asia/Calcutta";
    SET time_zone = "+05:30";
    SET @@session.time_zone = "+05:30";
    
    0 讨论(0)
  • 2020-11-21 07:28

    If you're using PDO:

    $offset="+10:00";
    $db->exec("SET time_zone='".$offset."';");
    

    If you're using MySQLi:

    $db->MySQLi->query("SET time_zone='".$offset."';");
    

    More about formatting the offset here: https://www.sitepoint.com/synchronize-php-mysql-timezone-configuration/

    0 讨论(0)
  • 2020-11-21 07:29

    Simply run this on your MySQL server:

    SET GLOBAL time_zone = '+8:00';
    

    Where +8:00 will be your time zone.

    0 讨论(0)
  • 2020-11-21 07:29

    This is a 10 years old question, but anyway here's what worked for me. I'm using MySQL 8.0 with Hibernate 5 and SpringBoot 4.

    I've tried the above accepted answer but didn't work for me, what worked for me is this:

    db.url=jdbc:mysql://localhost:3306/testdb?useSSL=false&serverTimezone=Europe/Warsaw
    

    If this helps you don't forget to upvote it :D

    0 讨论(0)
提交回复
热议问题