简体   繁体   English

为什么 MySql 的响应时间与数据库中的时间不同?

[英]Why does MySql respond with a different time than is in the database?

So, from MySql workbench, the date is '2021-01-01 05:35:40', but when i fetch the date column through nodejs via the mysql npm package, I get 2021-01-01T10:52:48.000Z. So, from MySql workbench, the date is '2021-01-01 05:35:40', but when i fetch the date column through nodejs via the mysql npm package, I get 2021-01-01T10:52:48.000Z. The weirdest thing to me, is the local date I did created this record would've actually been 2021-01-01 00:35:40.对我来说最奇怪的是,我创建此记录的本地日期实际上是 2021-01-01 00:35:40。 So, the value I am getting in workbench is 5 hours ahead, and I don't even know how to read the response I am getting on my server(T and.000Z bit) but I assume that it is 10 hours ahead of my actual time, and 5 hours ahead of what I am getting from workbench.因此,我在工作台中获得的值提前 5 小时,我什至不知道如何阅读我在服务器上获得的响应(T 和 .000Z 位),但我认为它比我的提前 10 小时实际时间,比我从工作台得到的时间提前 5 小时。

Here's the answer.这是答案。 It is because you are using NodeJS or more specifically, JavaScript.这是因为您使用的是 NodeJS,或者更具体地说,是 JavaScript。 Dates from MySQL get converted to ISO date strings in JS but their value remains the same.来自 MySQL 的日期在 JS 中被转换为 ISO 日期字符串,但它们的值保持不变。

You can use these functions to format dates from ISOdateStrings to readable formats.您可以使用这些函数将日期从ISOdateStrings为可读格式。

module.exports.formatDate = (date) => {
    const _date = new Date(date);
    const day = _date.getDate();
    const month = _date.getMonth() + 1;
    const year = _date.getFullYear();
    return `${year}-${month}-${day}`;
}

module.exports.formatTime = (date) => {
    const _date = new Date(date);
    const hours = _date.getHours()
    const minutes = _date.getMinutes();
    const seconds = _date.getSeconds();
    return `${hours}:${minutes}:${seconds}`;
}

module.exports.toDateTimestamp = (date) => {
    const dateStamp = this.formatDate(date);
    const timeStamp = this.formatTime(date);
    return `${dateStamp} ${timeStamp}`
}

Try the following steps:尝试以下步骤:

  • Restart MySql重启 MySql

sudo /etc/init.d/mysqld restart sudo /etc/init.d/mysqld 重启

Reference - https://major.io/2007/07/01/mysql-time-zone-different-from-system-time-zone/参考 - https://major.io/2007/07/01/mysql-time-zone-different-from-system-time-zone/

  • my.cnf has the wrong setting for timezone, correct format is like this: my.cnf 的时区设置错误,正确的格式如下:

default_time_zone=Europe/Riga default_time_zone=欧洲/里加

Also make sure you have updated mysql timezones via shell like this: https://dev.mysql.com/doc/refman/5.5/en/time-zone-support.html Also make sure you have updated mysql timezones via shell like this: https://dev.mysql.com/doc/refman/5.5/en/time-zone-support.html

using mysql_tzinfo_to_sql...

  • Probably MySql has a correct time setting already, but it's just that you're SSHing into the server I recently had to troubleshoot this exact issue, thus finding this post.可能 MySql 已经有一个正确的时间设置,但这只是你正在通过 SSH 连接到我最近不得不解决这个确切问题的服务器,因此找到了这篇文章。 In our case the dev was using an alias to connect to MySQL, and he did not remember that MySQL was actually running on a different host than he was SSHing into (also with an alias).在我们的例子中,开发人员使用别名连接到 MySQL,他不记得 MySQL 实际上运行在与他通过 SSH 连接的不同主机上(也使用别名)。

Try the following steps: Check timezone of server and your client end.尝试以下步骤: 检查服务器和客户端的时区。 Restart Sql service after correct Time zone.在正确的时区后重新启动 Sql 服务。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 与MySQL服务器不同的服务器中的MySQL数据库 - MySQL database in a different server than MySQL server AJAX提供与MySQL不同的结果 - AJAX gives different result than MySQL does 为什么MySQL浮动与Java浮动不同? - Why is MySQL float different than Java float? 为什么 sqlalchemy 执行查询比使用 mysql 客户端花费更长的时间? - Why does sqlalchemy takes longer time to execute a query than using mysql client? 为什么没有查询缓存的mysql查询的第一次执行比以后的执行需要更长的时间? - Why does the first execution of a mysql query with no query cache takes longer time than later executions? 为什么减号运算符给出的结果与 mysql 中的 TIMESTAMPDIFF() 函数不同? - Why does the minus operator give different result than the TIMESTAMPDIFF() function in mysql? 为什么这个MySQL存储函数给出的结果与在查询中进行计算的结果不同? - Why does this MySQL stored function give different results than to doing the calculation in the query? 为什么Sequel Pro的表键值结果与命令行上的MySQL结果不同? - Why does Sequel Pro show a different result for a table's key values than MySQL on the command line? MySQL外连接:为什么在ON中选择记录与在WHERE中选择会产生不同的结果? - MySQL outer join: why does selecting records in ON gives different results than selecting in WHERE? 为什么对 mysql 数据库的这种搜索没有结果? 第一次使用 LIKE - why does this search of a mysql database produce no results? Using LIKE for the first time
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM