MySQL data export changes times

混江龙づ霸主 提交于 2019-12-05 13:12:08

问题


I have some backup and restore scripts that I am using for my database. The table has a timestamp field. The backup script looks like this:

mysqldump -u user -ppass database --tab="../" --fields-terminated-by="|" --skip-comments table

It creates two files, table.sql and table.txt. The restore script looks like this:

mysql -u user -ppass database < "../table.sql"
mysqlimport -u user -ppass --local --fields-terminated-by="|" database "../table.txt"

However the backup script is outputting the wrong time - it's an hour behind what is in the database - but it doesn't correct it when importing.

For example the time on one row was 15:10:25 but when the backup script is run, 14:10:25 is listed in table.txt. When I run the restore script, the same row now has 14:10:25 as the time in the database. If I backup again, it says 13:10:25! And so on...

I can't figure out why this is. The time zone appears to be set to "SYSTEM" (I'm on GMT). The table.sql file has a few lines mentioning time zones, maybe something is wrong there? Here is the full file in question:

/*!40101 SET @OLD_CHARACTER_SET_CLIENT=@@CHARACTER_SET_CLIENT */;
/*!40101 SET @OLD_CHARACTER_SET_RESULTS=@@CHARACTER_SET_RESULTS */;
/*!40101 SET @OLD_COLLATION_CONNECTION=@@COLLATION_CONNECTION */;
/*!40101 SET NAMES utf8 */;
/*!40103 SET @OLD_TIME_ZONE=@@TIME_ZONE */;
/*!40103 SET TIME_ZONE='+00:00' */;
/*!40101 SET @OLD_SQL_MODE=@@SQL_MODE, SQL_MODE='' */;
/*!40111 SET @OLD_SQL_NOTES=@@SQL_NOTES, SQL_NOTES=0 */;
DROP TABLE IF EXISTS `news_article`;
/*!40101 SET @saved_cs_client     = @@character_set_client */;
/*!40101 SET character_set_client = utf8 */;
CREATE TABLE `news_article` (
  `id` smallint(5) unsigned NOT NULL AUTO_INCREMENT,
  `title` varchar(100) NOT NULL,
  `alias` varchar(65) NOT NULL,
  `author` tinyint(3) unsigned NOT NULL,
  `category` tinyint(3) unsigned NOT NULL,
  `posted` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
  `opening` text NOT NULL,
  `content` text NOT NULL,
  PRIMARY KEY (`id`),
  UNIQUE KEY `alias` (`alias`)
) ENGINE=MyISAM AUTO_INCREMENT=93 DEFAULT CHARSET=utf8;
/*!40101 SET character_set_client = @saved_cs_client */;

/*!40103 SET TIME_ZONE=@OLD_TIME_ZONE */;

/*!40101 SET SQL_MODE=@OLD_SQL_MODE */;
/*!40101 SET CHARACTER_SET_CLIENT=@OLD_CHARACTER_SET_CLIENT */;
/*!40101 SET CHARACTER_SET_RESULTS=@OLD_CHARACTER_SET_RESULTS */;
/*!40101 SET COLLATION_CONNECTION=@OLD_COLLATION_CONNECTION */;
/*!40111 SET SQL_NOTES=@OLD_SQL_NOTES */;

回答1:


Found a solution in the end: adding the --skip-tz-utc option to the export script.

This simply makes sure the exact date you export is what's imported in the second database. It works for me since the databases are the same time zone, but may not be ideal for others whose databases are different time zones.



来源:https://stackoverflow.com/questions/14886427/mysql-data-export-changes-times

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!