You can set the system time zone for MySQL Server at startup
with the
--timezone=
option to mysqld_safe. You can also set it by
setting the timezone_nameTZ environment variable before
you start mysqld. The allowable values for
--timezone or TZ are
system-dependent. Consult your operating system documentation to
see what values are acceptable.
Before MySQL 4.1.3, the server operates only in the system time zone set at startup. Beginning with MySQL 4.1.3, the server maintains several time zone settings, some of which can be modified at runtime:
The system time zone. When the server starts, it attempts to
determine the time zone of the host machine and uses it to
set the system_time_zone system variable.
The value does not change thereafter.
The server's current time zone. The global
time_zone system variable indicates the
time zone the server currently is operating in. The initial
value for time_zone is
'SYSTEM', which indicates that the server
time zone is the same as the system time zone.
The initial global server time zone value can be specified
explicitly at startup with the
--default-time-zone=
option on the command line, or you can use the following
line in an option file:
timezone
default-time-zone='timezone'
If you have the SUPER privilege, you can
set the global server time zone value at runtime with this
statement:
mysql> SET GLOBAL time_zone = timezone;
Per-connection time zones. Each client that connects has its
own time zone setting, given by the session
time_zone variable. Initially, the
session variable takes its value from the global
time_zone variable, but the client can
change its own time zone with this statement:
mysql> SET time_zone = timezone;
The current session time zone setting affects display and
storage of time values that are zone-sensitive. This includes
the values displayed by functions such as
NOW() or CURTIME(), and
values stored in and retrieved from TIMESTAMP
columns. Values for TIMESTAMP columns are
converted from the current time zone to UTC for storage, and
from UTC to the current time zone for retrieval. The current
time zone setting does not affect values displayed by functions
such as UTC_TIMESTAMP() or values in
DATE, TIME, or
DATETIME columns.
The current values of the global and client-specific time zones can be retrieved like this:
mysql> SELECT @@global.time_zone, @@session.time_zone;
timezone values can be given in
several formats, none of which are case sensitive:
The value 'SYSTEM' indicates that the
time zone should be the same as the system time zone.
The value can be given as a string indicating an offset from
UTC, such as '+10:00' or
'-6:00'.
The value can be given as a named time zone, such as
'Europe/Helsinki',
'US/Eastern', or
'MET'. Named time zones can be used only
if the time zone information tables in the
mysql database have been created and
populated.
The MySQL installation procedure creates the time zone tables in
the mysql database, but does not load them.
You must do so manually using the following instructions. (If
you are upgrading to MySQL 4.1.3 or later from an earlier
version, you can create the tables by upgrading your
mysql database. Use the instructions in
Section 5.5.4, “mysql_fix_privilege_tables — Upgrade MySQL System Tables”. After creating the
tables, you can load them.)
Loading the time zone information is not necessarily a one-time operation because the information changes occasionally. For example, the rules for Daylight Saving Time in the United States, Mexico, and parts of Canada changed in 2007. When such changes occur, applications that use the old rules become out of date and you may find it necessary to reload the time zone tables to keep the information used by your MySQL server current. See the notes at the end of this section.
If your system has its own zoneinfo
database (the set of files describing time zones),
you should use the mysql_tzinfo_to_sql
program for filling the time zone tables. Examples of such
systems are Linux, FreeBSD, Sun Solaris, and Mac OS X. One
likely location for these files is the
/usr/share/zoneinfo directory. If your
system does not have a zoneinfo database, you can use the
downloadable package described later in this section.
The mysql_tzinfo_to_sql program is used to load the time zone tables. On the command line, pass the zoneinfo directory pathname to mysql_tzinfo_to_sql and send the output into the mysql program. For example:
shell> mysql_tzinfo_to_sql /usr/share/zoneinfo | mysql -u root mysql
mysql_tzinfo_to_sql reads your system's time zone files and generates SQL statements from them. mysql processes those statements to load the time zone tables.
mysql_tzinfo_to_sql also can be used to load a single time zone file or to generate leap second information:
To load a single time zone file
tz_file that corresponds to a
time zone name tz_name, invoke
mysql_tzinfo_to_sql like this:
shell> mysql_tzinfo_to_sql tz_file tz_name | mysql -u root mysql
With this approach, you must execute a separate command to load the time zone file for each named zone that the server needs to know about.
If your time zone needs to account for leap seconds,
initialize the leap second information like this, where
tz_file is the name of your time
zone file:
shell> mysql_tzinfo_to_sql --leap tz_file | mysql -u root mysql
If your system is one that has no zoneinfo database (for example, Windows or HP-UX), you can use the package of pre-built time zone tables that is available for download at the MySQL Developer Zone:
http://dev.mysql.com/downloads/timezones.html
This time zone package contains .frm,
.MYD, and .MYI files
for the MyISAM time zone tables. These tables
should be part of the mysql database, so you
should place the files in the mysql
subdirectory of your MySQL server's data directory. The server
should be stopped while you do this and restarted afterward.
Warning: Do not use the downloadable package if your system has a zoneinfo database. Use the mysql_tzinfo_to_sql utility instead. Otherwise, you may cause a difference in datetime handling between MySQL and other applications on your system.
For information about time zone settings in replication setup, please see Section 6.7, “Replication Features and Known Problems”.
Staying Current with Time Zone Changes
As mentioned earlier, when the time zone rules change, applications that use the old rules become out of date. To stay current, it is necessary to make sure that your system uses current time zone information is used. For MySQL, there are two factors to consider in staying current:
The operating system time affects the value that the MySQL
server uses for times if its time zone is set to
SYSTEM. Make sure that your operating
system is using the latest time zone information. For most
operating systems, the latest update or service pack
prepares your system for the time changes. Check the Web
site for your operating system vendor for an update that
addresses the time changes.
If you replace the system's
/etc/localtime timezone file with a
verion that uses rules differing from those in effect at
mysqld startup, you should restart
mysqld so that it uses the updated rules.
Otherwise, mysqld might not notice when
the system changes its time.
If you use named time zones with MySQL, make sure that the
time zone tables in the mysql database
are up to date. If your system has its own zoneinfo
database, you should reload the MySQL time zone tables
whenever the zoneinfo database is updated, using the
instructions given earlier in this section. For systems that
do not have their own zoneinfo database, check the MySQL
Developer Zone for updates. When a new update is available,
download it and use it to replace your current time zone
tables. mysqld caches time zone
information that it looks up, so after replacing the time
zone tables, you should restart mysqld to
make sure that it does not continue to serve outdated time
zone data.
For versions of MySQL older than 4.1.3 that do not have time
zone support, the server always tracks the operating system
time (much like a time zone setting of
SYSTEM in 4.1.3 and up). Assuming that
the server host itself has its operating system updated to
handle any changes to Daylight Saving Time rules, the MySQL
server should know the correct time.
If you are uncertain whether named time zones are available, for use either as the server's time zone setting or by clients that set their own time zone, check whether your time zone tables are empty. The following query determines whether the table that contains time zone names has any rows:
mysql> SELECT COUNT(*) FROM mysql.time_zone_name;
+----------+
| COUNT(*) |
+----------+
| 0 |
+----------+
A count of zero indicates that the table is empty. In this case, no one can be using named time zones, and you don't need to update the tables. A count greater than zero indicates that the table is not empty and that its contents are available to be used for named time zone support. In this case, you should be sure to reload your time zone tables so that anyone who uses named time zones will get correct query results.
To check whether your MySQL installation is updated properly for a change in Daylight Saving Time rules, use a test like the one following. The example uses values that are appropriate for the 2007 DST 1-hour change that occurs in the United States on March 11 at 2 a.m.
The test uses these two queries:
SELECT CONVERT_TZ('2007-03-11 2:00:00','US/Eastern','US/Central');
SELECT CONVERT_TZ('2007-03-11 3:00:00','US/Eastern','US/Central');
The two time values indicate the times at which the DST change occurs, and the use of named time zones requires that the time zone tables be used. The desired result is that both queries return the same result (the input time, converted to the equivalent value in the 'US/Central' time zone).
Before updating the time zone tables, you would see an incorrect result like this:
mysql>SELECT CONVERT_TZ('2007-03-11 2:00:00','US/Eastern','US/Central');+------------------------------------------------------------+ | CONVERT_TZ('2007-03-11 2:00:00','US/Eastern','US/Central') | +------------------------------------------------------------+ | 2007-03-11 01:00:00 | +------------------------------------------------------------+ mysql>SELECT CONVERT_TZ('2007-03-11 3:00:00','US/Eastern','US/Central');+------------------------------------------------------------+ | CONVERT_TZ('2007-03-11 3:00:00','US/Eastern','US/Central') | +------------------------------------------------------------+ | 2007-03-11 02:00:00 | +------------------------------------------------------------+
After updating the tables, you should see the correct result:
mysql>SELECT CONVERT_TZ('2007-03-11 2:00:00','US/Eastern','US/Central');+------------------------------------------------------------+ | CONVERT_TZ('2007-03-11 2:00:00','US/Eastern','US/Central') | +------------------------------------------------------------+ | 2007-03-11 01:00:00 | +------------------------------------------------------------+ mysql>SELECT CONVERT_TZ('2007-03-11 3:00:00','US/Eastern','US/Central');+------------------------------------------------------------+ | CONVERT_TZ('2007-03-11 3:00:00','US/Eastern','US/Central') | +------------------------------------------------------------+ | 2007-03-11 01:00:00 | +------------------------------------------------------------+

User Comments
You must restart mysqld after you run mysql_tzinfo_to_sql for the changes to take effect. Not mentioned in this article.
Add your own comment.