YAPC::Asia 2015 ã®ã»ãã·ã§ã³ã§ãMySQL ã®ã¿ã¤ã ã¾ã¼ã³ã®è©±ãåºã¦ãã¾ãããã以åã¿ã¤ã ã¾ã¼ã³å¨ãã§å°ãã¯ã¾ã£ããã¨ããã£ãã®ãæãåºããã®ã§æ¸ãã¦ã¿ã¾ãã
MySQLã®ããã©ã«ãã®ã¿ã¤ã ã¾ã¼ã³ã¯ mysqld èµ·åæã®ã·ã¹ãã è¨å®ã§ããTZ ç°å¢å¤æ°ã®å¤ããå¤æ°ãè¨å®ããã¦ããªããã° /etc/localtime(Ubuntu ã®å ´å) ã§ãã
# TZ=Japan /usr/sbin/mysqld
mysql> SHOW VARIABLES LIKE '%time_zone%'; +------------------+--------+ | Variable_name | Value | +------------------+--------+ | system_time_zone | JST | | time_zone | SYSTEM | +------------------+--------+ mysql> system date; SELECT NOW(); 2015å¹´ 8æ 22æ¥ åææ¥ 21:39:12 JST +---------------------+ | now() | +---------------------+ | 2015-08-22 21:39:12 | +---------------------+
# TZ=UTC /usr/sbin/mysqld
mysql> SHOW VARIABLES LIKE '%time_zone%'; +------------------+--------+ | Variable_name | Value | +------------------+--------+ | system_time_zone | UTC | | time_zone | SYSTEM | +------------------+--------+ mysql> system date; SELECT NOW(); 2015å¹´ 8æ 22æ¥ åææ¥ 21:39:52 JST +---------------------+ | now() | +---------------------+ | 2015-08-22 12:39:52 | +---------------------+
MySQL ã®ãµã¼ãã¼ã¨ã¯ã©ã¤ã¢ã³ãã§ã¿ã¤ã ã¾ã¼ã³ãåã£ã¦ããªãã¨ãä½ãã¾ãããã¨ãèµ·ããã®ãè¦ã¦ã¿ã¾ãã
mysql> CREATE TABLE test (ts TIMESTAMP, dt DATETIME); mysql> INSERT INTO test (ts, dt) VALUES (NOW(), NOW()); mysql> INSERT INTO test (ts, dt) VALUES ('2015-08-22 21:56:39', '2015-08-22 21:56:39'); mysql> SELECT * FROM test; +---------------------+---------------------+ | ts | dt | +---------------------+---------------------+ | 2015-08-22 12:56:09 | 2015-08-22 12:56:09 | | 2015-08-22 21:56:39 | 2015-08-22 21:56:39 | +---------------------+---------------------+
NOW()
ã¯ãµã¼ãã¼å´ã§å®è¡ãããã®ã§ããµã¼ãã¼å´ã®æå»ã«ãªãã¾ããã¯ã©ã¤ã¢ã³ããèªåã®ã¿ã¤ã ã¾ã¼ã³ã®æå»ãåå¾ãã¦å¤ãè¨å®ããã¨ã¯ã©ã¤ã¢ã³ãã®æå»ã«ãªãã¾ãã
ããã§ãã¿ã¤ã ã¾ã¼ã³ã®è¨å®ã誤ã£ã¦ãããã¨ã«æ°ã¥ãã¦ãmysqld ãèµ·åããªããã¾ããã
# TZ=Japan /usr/sbin/mysqld
mysql> select * from test; +---------------------+---------------------+ | ts | dt | +---------------------+---------------------+ | 2015-08-22 21:56:09 | 2015-08-22 12:56:09 | | 2015-08-23 06:56:39 | 2015-08-22 21:56:39 | +---------------------+---------------------+
TIMESTAMP ã«ã©ã ã«è¨å®ããå¤ãå¤ãã£ã¦ãã¾ãã¾ããã
TIMESTAMP ã«ã©ã ã¯å é¨çã«ã¯ 1970-01-01 00:00:00 UTC ããã®çµéç§æ°ã§å¤ãä¿æãã¦ãã¦ããã®å¤ãç¾å¨ã®ã¿ã¤ã ã¾ã¼ã³ã«åããã¦æ¥æå½¢å¼ã«å¤æãã¦ããããã§ãã UTC ã® 12:56:09 㨠JST ã® 21:56:09 㯠TIMESTAMP ã®å é¨è¡¨ç¾çã«ã¯åãå¤ã«ãªã£ã¦ãã¾ãã
DATETIME ã«ã©ã ã¯æå®ããå¤ããã®ã¾ã¾ä¿æããã¦ãããããã¿ã¤ã ã¾ã¼ã³ã®å½±é¿ãåãã¾ããã
TIMESTAMP ã«ã©ã ã«å¤ãç»é²ããå¾ã«ã¿ã¤ã ã¾ã¼ã³ãå¤æ´ãããããªãã¨ã¯é¿ããã»ããããã§ãããã å人çã«ã¯ãæå»ãä¿æããã«ã¯ DATETIME ã«ã©ã ã«ãã¦ãTIMESTAMP ã¯ä½¿ããªãã®ããããããããã§ãã
DATETIME ã«ã©ã ã®å ´åã§ããNOW()
çã®æå»åå¾é¢æ°ã¯ã¿ã¤ã ã¾ã¼ã³ã®å½±é¿ãåããã®ã§ããµã¼ãã¼ã¨ã¯ã©ã¤ã¢ã³ãã®ã¿ã¤ã ã¾ã¼ã³ã¯åããã¦ãããæ¹ãç¡é£ã§ãã
AWS ã® RDS ã®ããã«ãã·ã¹ãã è¨å®ãå¤æ´ããããmysqld èµ·åæã®è¨å®ãããããã¨ãã§ããªãå ´åã¯ãæ¥ç¶å¾ã« time_zone
å¤æ°ãè¨å®ãããã¨ã§åããå¤æ´ã§ãã¾ãã
mysql> SELECT @@system_time_zone, @@global.time_zone, @@time_zone; +--------------------+--------------------+-------------+ | @@system_time_zone | @@global.time_zone | @@time_zone | +--------------------+--------------------+-------------+ | UTC | SYSTEM | SYSTEM | +--------------------+--------------------+-------------+ mysql> SET time_zone = '+09:00'; mysql> SELECT @@system_time_zone, @@global.time_zone, @@time_zone; +--------------------+--------------------+-------------+ | @@system_time_zone | @@global.time_zone | @@time_zone | +--------------------+--------------------+-------------+ | UTC | SYSTEM | +09:00 | +--------------------+--------------------+-------------+ mysql> SET global time_zone = '+09:00'; mysql> SELECT @@system_time_zone, @@global.time_zone, @@time_zone; +--------------------+--------------------+-------------+ | @@system_time_zone | @@global.time_zone | @@time_zone | +--------------------+--------------------+-------------+ | UTC | +09:00 | +09:00 | +--------------------+--------------------+-------------+
system_time_zone
ã¯å¤æ´ã§ãã¾ãããtime_zone
ã¯ç¾å¨ã®æ¥ç¶ã®ã¿ã§æå¹ã§ããglobal.time_zone
ã¯ãã¹ã¦ã®æ¥ç¶ã§æå¹ã«ãªãã¾ããæ¢ã«æ¥ç¶ä¸ã®ã¯ã©ã¤ã¢ã³ãã«ã¯å½±é¿ãã¾ãããglobal.time_zone
ã®è¨å®ã¯ mysqld ãçµäºããã¾ã§æå¹ã§ãã
æ¥æ¬ã®å ´åã¯å¤æéãç¡ããUTC ã¨ã®æå·®ã¯å¸¸ã« +09:00 ãªã®ã§ä¸è¨ã®ãããªè¨å®ã§ããã®ã§ãããå¤æéããããUTC ã¨ã®æå·®ãå£ç¯ã«ãã£ã¦ç°ãªããããªå°åã§ã¯åºå®ã®å¤ã®è¨å®ã§ã¯åé¡ã«ãªãã¾ãã
ããã©ã«ãç¶æ
ã§ã¯ Japan
ã®ãããªã·ã³ãã«ã time_zone
å¤æ°ã«æå®ãããã¨ã¯ã§ãã¾ããã
mysql> SET time_zone = 'Japan'; ERROR 1298 (HY000): Unknown or incorrect time zone: 'Japan'
å®ã¯ MySQL ã¯ã¿ã¤ã ã¾ã¼ã³ç¨ã®ã·ã¹ãã ãã¼ãã«ãæã£ã¦ãã¾ãããã ãåæç¶æ ã§ã¯ä¸èº«ã¯ç©ºã£ã½ã§ãã
mysql> USE mysql mysql> SHOW TABLES LIKE '%time_zone%'; +-------------------------------+ | Tables_in_mysql (%time_zone%) | +-------------------------------+ | time_zone | | time_zone_leap_second | | time_zone_name | | time_zone_transition | | time_zone_transition_type | +-------------------------------+ mysql> SELECT * FROM time_zone; Empty set (0.00 sec)
mysql_tzinfo_to_sql
ã¨ããã³ãã³ã㧠OS ã®ã¿ã¤ã ã¾ã¼ã³æ
å ±ãããããã®ãã¼ãã«ã«æ ¼ç´ãããã㪠SQL æã«å¤æãããã¨ãã§ãã¾ãã
# mysql_tzinfo_to_sql /usr/share/zoneinfo TRUNCATE TABLE time_zone; TRUNCATE TABLE time_zone_name; TRUNCATE TABLE time_zone_transition; TRUNCATE TABLE time_zone_transition_type; INSERT INTO time_zone (Use_leap_seconds) VALUES ('N'); SET @time_zone_id= LAST_INSERT_ID(); INSERT INTO time_zone_name (Name, Time_zone_id) VALUES ('Africa/Abidjan', @time_ zone_id); INSERT INTO time_zone_transition (Time_zone_id, Transition_time, Transition_type _id) VALUES (@time_zone_id, -2147483648, 0) ,(@time_zone_id, -1830383032, 1) ; ãå¾ç¥ã
ãã®ã¯ã¨ãªã次ã®ããã«ã㦠mysql ãã¼ã¿ãã¼ã¹ã§å®è¡ããã¨ã¿ã¤ã ã¾ã¼ã³ãç»é²ããã¾ãã
# mysql_tzinfo_to_sql /usr/share/zoneinfo | mysql -uroot mysql
ãã㧠Japan ã®ãããªã·ã³ãã«ã§æå®ãããã¨ãã§ããããã«ãªãã¾ãã
mysql> set time_zone = 'Japan'; mysql> select @@system_time_zone, @@global.time_zone, @@time_zone; +--------------------+--------------------+-------------+ | @@system_time_zone | @@global.time_zone | @@time_zone | +--------------------+--------------------+-------------+ | UTC | SYSTEM | Japan | +--------------------+--------------------+-------------+
ãªããtime_zone
ã SYSTEM ã®å ´åã¯ã·ã¹ãã ã®ã¿ã¤ã ã¾ã¼ã³ã使ãããã®ã§ãå¤æéãæ£ããå¦çããã¾ãã
èªåã¯å¤æéã絡ããããªã·ã¹ãã ã¯ä½ã£ããã¨ãªãã®ã§ãã¾ãç¥è¦ããªãã®ã§ãããæå»ã¯ UTC ã§ç»é²ãã¦ããã¦ãã¢ããªå´ã§å¿ è¦ã«å¿ãã¦å¤æããæ¹ãããã¨æã£ã¦ãã¾ãããªãã¨ãªãâ¦ã