How do I set the time zone of MySQL?

后端 未结 20 3275
闹比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:29

    On Windows (IIS) in order to be able to SET GLOBAL time_zone = 'Europe/Helsinki' (or whatever) the MySQL time_zone description tables need to be populated first.

    I downloaded these from this link https://dev.mysql.com/downloads/timezones.html

    After running the downloaded SQL query I was able to set the GLOBAL time_zone and resolve the issue I had where SELECT NOW(); was returning GMT rather than BST.

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

    To set the standard time zone at MariaDB you have to go to the 50-server.cnf file.

    sudo nano /etc/mysql/mariadb.conf.d/50-server.cnf
    

    Then you can enter the following entry in the mysqld section.

    default-time-zone='+01:00'
    

    Example:

    #
    # These groups are read by MariaDB server.
    # Use it for options that only the server (but not clients) should see
    #
    # See the examples of server my.cnf files in /usr/share/mysql/
    #
    
    # this is read by the standalone daemon and embedded servers
    [server]
    
    # this is only for the mysqld standalone daemon
    [mysqld]
    
    #
    # * Basic Settings
    #
    user            = mysql
    pid-file        = /var/run/mysqld/mysqld.pid
    socket          = /var/run/mysqld/mysqld.sock
    port            = 3306
    basedir         = /usr
    datadir         = /var/lib/mysql
    tmpdir          = /tmp
    lc-messages-dir = /usr/share/mysql
    skip-external-locking
    
    ### Default timezone ###
    default-time-zone='+01:00'
    
    # Instead of skip-networking the default is now to listen only on
    # localhost which is more compatible and is not less secure.
    

    The change must be made via the configuration file, otherwise the MariaDB server will reset the mysql tables after a restart!

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

    If anyone is using GoDaddy Shared Hosting, you can try for following solution, worked for me.

    When starting DB connection, set the time_zone command in my PDO object e.g.:

    $pdo = new PDO($dsn, $user, $pass, $opt);
    $pdo->exec("SET time_zone='+05:30';");
    

    Where "+05:30" is the TimeZone of India. You can change it as per your need.

    After that; all the MySQL processes related to Date and Time are set with required timezone.

    Source : https://in.godaddy.com/community/cPanel-Hosting/How-to-change-TimeZone-for-MySqL/td-p/31861

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

    First to figure out what the time_zone is you can query

    SHOW VARIABLES LIKE '%time_zone%'; 
    

    Your output should be something similar as follows

    **Variable_name**     **Value**
    system_time_zone      CDT
    time_zone             SYSTEM
    

    Then if you want to confirm that you are in say some time zone like CDT instead of something like EST you can check what time it thinks your machine is in by saying

    SELECT NOW();
    

    If this is not the time you want you need to change it... all you need to do is SET time_zone = timezone_name. Make sure it is one that is in Continent/City format.

    If you are on a shared server because you have a hosting service please refer to these answers regarding changing the php.ini file or the .htaccess file.

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

    Edit the MySQL config file

    sudo nano /etc/mysql/my.cnf
    

    Scroll and add these to the bottom. Change to relevant time zone

    [mysqld]
    default-time-zone = "+00:00"
    

    Restart the server

    sudo service mysql restart
    
    0 讨论(0)
  • 2020-11-21 07:32

    I thought this might be useful:

    There are three places where the timezone might be set in MySQL:

    In the file "my.cnf" in the [mysqld] section

    default-time-zone='+00:00'
    

    @@global.time_zone variable

    To see what value they are set to:

    SELECT @@global.time_zone;
    

    To set a value for it use either one:

    SET GLOBAL time_zone = '+8:00';
    SET GLOBAL time_zone = 'Europe/Helsinki';
    SET @@global.time_zone = '+00:00';
    

    (Using named timezones like 'Europe/Helsinki' means that you have to have a timezone table properly populated.)

    Keep in mind that +02:00 is an offset. Europe/Berlin is a timezone (that has two offsets) and CEST is a clock time that corresponds to a specific offset.

    @@session.time_zone variable

    SELECT @@session.time_zone;
    

    To set it use either one:

    SET time_zone = 'Europe/Helsinki';
    SET time_zone = "+00:00";
    SET @@session.time_zone = "+00:00";
    

    Both might return SYSTEM which means that they use the timezone set in my.cnf.

    For timezone names to work, you must setup your timezone information tables need to be populated: http://dev.mysql.com/doc/refman/5.1/en/time-zone-support.html. I also mention how to populate those tables in this answer.

    To get the current timezone offset as TIME

    SELECT TIMEDIFF(NOW(), UTC_TIMESTAMP);
    

    It will return 02:00:00 if your timezone is +2:00.

    To get the current UNIX timestamp:

    SELECT UNIX_TIMESTAMP();
    SELECT UNIX_TIMESTAMP(NOW());
    

    To get the timestamp column as a UNIX timestamp

    SELECT UNIX_TIMESTAMP(`timestamp`) FROM `table_name`
    

    To get a UTC datetime column as a UNIX timestamp

    SELECT UNIX_TIMESTAMP(CONVERT_TZ(`utc_datetime`, '+00:00', @@session.time_zone)) FROM `table_name`
    

    Note: Changing the timezone will not change the stored datetime or timestamp, but it will show a different datetime for existing timestamp columns as they are internally stored as UTC timestamps and externally displayed in the current MySQL timezone.

    I made a cheatsheet here: Should MySQL have its timezone set to UTC?

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