MySQL JDBC Driver 5.1.33 - Time Zone Issue

前端 未结 30 1195
栀梦
栀梦 2020-11-22 05:22

Some background:

I have a Java 1.6 webapp running on Tomcat 7. The database is MySQL 5.5. Previously, I was using Mysql JDBC driver 5.1.23 to connect to the DB. Ever

相关标签:
30条回答
  • 2020-11-22 05:53
    1. I added in mysql config file in section [mysqld]

      default_time_zone='+03:00'
      
    2. And restart mysql server:

      sudo service mysql restart
      

    Where +03:00 my UTC time zone.

    Path to config file on my os ubuntu 16.04:

    /etc/mysql/mysql.conf.d/mysqld.cnf
    

    WARNING: IF YOUR TIME ZONE HAVE SUMMER AND WINTER TIME. YOU MUST CHANGE UTC IN CONFIG IF CHANGE TIME. TWICE IN YEAR(USUALLY) OR SET CRONTAB WITH SUDO.

    My url jdbc connection:

    "jdbc:mysql://localhost/java"
    
    0 讨论(0)
  • 2020-11-22 05:53

    Agree with @bluecollarcoder answer, but it's better to use TimeZone.getDefault().getID(); at the end of the connection string:

    "jdbc:mysql://localhost/db?useUnicode=true&useJDBCCompliantTimezoneShift=true&useLegacyDatetimeCode=false&serverTimezone=" + TimeZone.getDefault().getID();  
    

    In this case Timezone parameter automatically updates depending on the local machine timezone.

    0 讨论(0)
  • 2020-11-22 05:55

    I'm using mysql-connector-java-8.0.13 and had the same problem. I created my database in the command line console and solved this problem by using @Dimitry Rud's solution on the command line:

    SET GLOBAL time_zone = '-6:00';
    

    I didn't need to restart anything, set the time and immediately run my code in eclipse, it connected with no problems.

    The bug is supposed to be fixed in an older version, but I think I got this error because after I created the database in the console, I didn't set this. I'm not using workbench nor another app to manage this rather than the console.

    0 讨论(0)
  • 2020-11-22 05:56

    I am late, But If you are struggling through the following error and using datasource(javax.sql.DataSource):

    The server time zone value 'CEST' is unrecognized or represents more than one time zone.
    

    Set following line to get rid of the error:

    MysqlDataSource dataSource = new MysqlDataSource();
    dataSource.setServerTimezone("UTC");
    
    0 讨论(0)
  • 2020-11-22 05:59

    I faced the same error and in my case, I change the Server Port Number to 3308 where previously it was 3306. This connect my project to the MySQL database.

    Here we have to change the connection code also.

    Class.forName("com.mysql.cj.jdbc.Driver");
    cn=(java.sql.Connection)DriverManager.getConnection("jdbc:mysql://localhost:3308/test2?zeroDateTimeBehavior=convertToNull","root","");
    

    Changing the port number in the connection code is also necessary as localhost:3308 to resolved the error.

    Also, the admin properties in my case.

    0 讨论(0)
  • 2020-11-22 06:00

    There is no impact of setting server time as UTC (for instance with jdbc:mysql://localhost:3306/myschema?serverTimezone=UTC, even if your application/database servers are not in this timezone. The important is for the application connection string + database to be synchronized with the same time zone.

    In other words, simply setting serverTimezone=UTC with a different time zone on the database server will shift any dates extracted from the database

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