MySQL如何跨时区迁移数据

五 15th, 2010

MySQL跨时区迁移数据的解决方法,重要!

问: 如果数据表中有时间字段,现在要迁移到其他时区的服务器上,该如何处理呢?
答:在高版本的mysqldump中,新增了一个选项:–tz-utc,默认是启用的,来看看它的说明

SET TIME_ZONE='+00:00' at top of dump to allow dumping of
TIMESTAMP data when a server has data in different time
zones or data is being moved between servers with
different time zones.

看明白了吧。
简言之,就是导出时增加一个说明,把当地时间都转换成0时区的时间,然后再导入,就能确保跨时区迁移数据了,来看下例子:

mysql> select * from t1;
+----+---------------------+
| id | cur_time            |
+----+---------------------+
|  1 | 2010-03-14 08:58:18 |
+----+---------------------+

现在导出数据:

mysqldump --tz-utc=1 -t test t1 > t1.sql
--tz-utc=false
#查看内容
cat t1.sql
/*!40103 SET TIME_ZONE='+00:00' */;
....
INSERT INTO `t1` VALUES (1,'2010-03-14 00:58:18');

可以看到,增加了设定时区的标志。

mysqldump --tz-utc=0 -t test t1 > t1_1.sql
#查看内容
cat t1_1.sql
INSERT INTO `t1` VALUES (1,'2010-03-14 08:58:18');

Del.icio.us Google书签 Digg Live Bookmark Technorati Furl Yahoo书签 Facebook 百度搜藏 新浪ViVi 365Key网摘 天极网摘 和讯网摘 博拉网 POCO网摘 添加到饭否 QQ书签 Digbuzz我挖网
标签: ,
目前还没有任何评论.