NOW()函数的复制

原文链接:http://www.mysqlperformanceblog.com/2012/11/28/replication-of-the-now-function-also-time-travel/

Notice the result of the NOW() function in the following query.  The query was run on a real database server and I didn’t change the clock of the server or change anything in the database configuration settings.

注意下面查询里NOW()函数的返回值。这查询跑在一台真实服务器上,我没有修改服务器时钟,也没有对数据库配置做任何的修改。

mysql> SELECT NOW(),SYSDATE();
+---------------------+---------------------+
| NOW()               | SYSDATE()           |
+---------------------+---------------------+
| 1999-01-01 00:00:00 | 2012-11-29 05:50:03 |
+---------------------+---------------------+
1 row in set (0.00 sec)

You may proceed to party like it is 1999. 

How can the NOW() function return a value in the past?

你可能关注到了1999这部分内容,NOW函数怎么会返回一个过去的时间值?

The “secret” is the TIMESTAMP variable, which is a special MySQL variable that can be set by the MySQL client.  MySQL adds special events in the binary log which set the TIMESTAMP and INSERT_ID (which is used for AUTO_INCREMENT replication) to the correct values to ensure that statements replicate properly.

这个秘密在于TIMESTAMP变量,是MYSQL的一个特殊的变量,能够通过MYSQ客户端进行设置。MYSQL在二进制日志里面增加特殊的事件去设置TIMESTAMP和INSERT_ID(用于AUTO_INCREMENT复制)为正确的值,确保语句级的复制的正确性。

Here is the SQL to produce the above output:

这是产生上面输出的SQL

SET TIMESTAMP=UNIX_TIMESTAMP('1999-01-01');
SELECT NOW(),SYSDATE();

Notice that SYSDATE returns the correct date. This means the SYSDATE() function is not replication safe by default. If you want to use the SYSDATE function in your app in DML statements, make sure you use the –sysdate-as-now option to mysqld. 

注意SYSDATE返回了正确的时间。这意味着SYSDATE函数在默认情况下的复制是不安全的。如果你想在应用的DML语句里面用SYSDATE函数,确保MYSQLD使用了–sysdate-as-now选项。

原文地址:https://www.cnblogs.com/zuoxingyu/p/2804844.html