欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

MySQL 5.6 中的 TIMESTAMP 和 explicit_defaults_for_timestamp 参数

程序员文章站 2024-02-25 19:33:39
安装mysql时,有warning: [root@localhost mysql]# scripts/mysql_install_db --user=mysql...

安装mysql时,有warning:

[root@localhost mysql]# scripts/mysql_install_db --user=mysql
installing mysql system tables...2015-08-13 14:20:09 0 [warning] timestamp with implicit default value is deprecated. please use --explicit_defaults_for_timestamp server option (see documentation for more details).
2015-08-13 14:20:09 0 [note] ./bin/mysqld (mysqld 5.6.26) starting as process 1934 ...
2015-08-13 14:20:09 1934 [note] innodb: using mutexes to ref count buffer pool pages
2015-08-13 14:20:09 1934 [note] innodb: the innodb memory heap is disabled
2015-08-13 14:20:09 1934 [note] innodb: mutexes and rw_locks use innodb's own implementation
2015-08-13 14:20:09 1934 [note] innodb: memory barrier is not used
2015-08-13 14:20:09 1934 [note] innodb: compressed tables use zlib 1.2.3
2015-08-13 14:20:09 1934 [note] innodb: using linux native aio
2015-08-13 14:20:09 1934 [note] innodb: not using cpu crc32 instructions
2015-08-13 14:20:09 1934 [note] innodb: initializing buffer pool, size = 128.0m
2015-08-13 14:20:09 1934 [note] innodb: completed initialization of buffer pool
2015-08-13 14:20:10 1934 [note] innodb: the first specified data file ./ibdata1 did not exist: a new database to be created!
2015-08-13 14:20:10 1934 [note] innodb: setting file ./ibdata1 size to 12 mb
2015-08-13 14:20:10 1934 [note] innodb: database physically writes the file full: wait...
2015-08-13 14:20:10 1934 [note] innodb: setting log file ./ib_logfile101 size to 48 mb
2015-08-13 14:20:11 1934 [note] innodb: setting log file ./ib_logfile1 size to 48 mb
2015-08-13 14:20:12 1934 [note] innodb: renaming log file ./ib_logfile101 to ./ib_logfile0
2015-08-13 14:20:12 1934 [warning] innodb: new log files created, lsn=45781
2015-08-13 14:20:12 1934 [note] innodb: doublewrite buffer not found: creating new
2015-08-13 14:20:12 1934 [note] innodb: doublewrite buffer created
2015-08-13 14:20:12 1934 [note] innodb: 128 rollback segment(s) are active.
2015-08-13 14:20:12 1934 [warning] innodb: creating foreign key constraint system tables.
2015-08-13 14:20:12 1934 [note] innodb: foreign key constraint system tables created
2015-08-13 14:20:12 1934 [note] innodb: creating tablespace and datafile system tables.
2015-08-13 14:20:12 1934 [note] innodb: tablespace and datafile system tables created.
2015-08-13 14:20:12 1934 [note] innodb: waiting for purge to start
2015-08-13 14:20:12 1934 [note] innodb: 5.6.26 started; log sequence number 0
2015-08-13 14:20:14 1934 [note] binlog end
2015-08-13 14:20:14 1934 [note] innodb: fts optimize thread exiting.
2015-08-13 14:20:14 1934 [note] innodb: starting shutdown...
2015-08-13 14:20:16 1934 [note] innodb: shutdown completed; log sequence number 1625977

ok

filling help tables...2015-08-13 14:20:16 0 [warning] timestamp with implicit default value is deprecated. 
please use --explicit_defaults_for_timestamp server option (see documentation for more details).
2015-08-13 14:20:16 0 [note] ./bin/mysqld (mysqld 5.6.26) starting as process 1957 ...
2015-08-13 14:20:16 1957 [note] innodb: using mutexes to ref count buffer pool pages
2015-08-13 14:20:16 1957 [note] innodb: the innodb memory heap is disabled
2015-08-13 14:20:16 1957 [note] innodb: mutexes and rw_locks use innodb's own implementation
2015-08-13 14:20:16 1957 [note] innodb: memory barrier is not used
2015-08-13 14:20:16 1957 [note] innodb: compressed tables use zlib 1.2.3
2015-08-13 14:20:16 1957 [note] innodb: using linux native aio
2015-08-13 14:20:16 1957 [note] innodb: not using cpu crc32 instructions
2015-08-13 14:20:16 1957 [note] innodb: initializing buffer pool, size = 128.0m
2015-08-13 14:20:16 1957 [note] innodb: completed initialization of buffer pool
2015-08-13 14:20:16 1957 [note] innodb: highest supported file format is barracuda.
2015-08-13 14:20:16 1957 [note] innodb: 128 rollback segment(s) are active.
2015-08-13 14:20:16 1957 [note] innodb: waiting for purge to start
2015-08-13 14:20:16 1957 [note] innodb: 5.6.26 started; log sequence number 1625977
2015-08-13 14:20:17 1957 [note] binlog end
2015-08-13 14:20:17 1957 [note] innodb: fts optimize thread exiting.
2015-08-13 14:20:17 1957 [note] innodb: starting shutdown...
2015-08-13 14:20:18 1957 [note] innodb: shutdown completed; log sequence number 1625987

ok

to start mysqld at boot time you have to copy
support-files/mysql.server to the right place for your system

please remember to set a password for the mysql root user !
to do so, start the server, then issue the following commands:

 ./bin/mysqladmin -u root password 'new-password'
 ./bin/mysqladmin -u root -h localhost.localdomain password 'new-password'

alternatively you can run:

 ./bin/mysql_secure_installation

which will also give you the option of removing the test
databases and anonymous user created by default. this is
strongly recommended for production servers.

see the manual for more instructions.

you can start the mysql daemon with:

 cd . ; ./bin/mysqld_safe &

you can test the mysql daemon with mysql-test-run.pl

 cd mysql-test ; perl mysql-test-run.pl

please report any problems at http://bugs.mysql.com/

the latest information about mysql is available on the web at

 http://www.mysql.com

support mysql by buying support/licenses at http://shop.mysql.com

new default config file was created as ./my.cnf and
will be used by default by the server when you start it.
you may edit this file to change server settings

warning: default config file /etc/my.cnf exists on the system
this file will be read by default by the mysql server
if you do not want to use this, either remove it, or use the
--defaults-file argument to mysqld_safe when starting the server

[root@localhost mysql]#

[warning] timestamp with implicit default value is deprecated. please use --explicit_defaults_for_timestamp server option (see documentation for more details).

其原因是从 5.6开始,timestamp 的默认行为已经是 deprecated 了。

在mysql 5.6.6之前,timestamp的默认行为:

timestamp列如果没有明确声明null属性,默认为not null。(而其他数据类型,如果没有显示声明为not null,则允许null值。)设置timestamp的列值为null,会自动存储为当前timestamp。

表中的第一个timestamp列,如果没有声明null属性、default或者 on update,会自动分配 default current_timestamp和on update current_timestamp 属性。

表中第二个timestamp列,如果没有声明为null或者default子句,默认自动分配'0000-00-00 00:00:00′。插入行时没有指明改列的值,该列默认分配'0000-00-00 00:00:00′,且没有警告。
要关闭警告,需要加入下面的参数:

explicit_defaults_for_timestamp=true

重启mysql后错误消失,这时timestamp的行为如下:

timestamp如果没有显示声明not null,是允许null值的,可以直接设置改列为null,而没有默认填充行为。
timestamp不会默认分配default current_timestamp 和 on update current_timestamp属性。
声明为not null且没有默认子句的timestamp列是没有默认值的。往数据表中插入列,又没有给timestamp列赋值时,如果是严格sql模式,会抛出一 个错误,如果严格sql模式没有启用,该列会赋值为'0000-00-00 00:00:00′,同时出现一个警告。(这和mysql处理其他时间类型数据一样,如datetime)
(参见:)

也就是 explicit_defaults_for_timestamp 关闭了 timestamp 类型字段锁拥有的一些会让人感到奇怪的默认行为,加入了该参数之后,如果还需要为 timestamp类型的字段指定默认行为,那么就需要显示的在创建表时显示的指定。explicit_defaults_for_timestamp 也就是这个意思:显示指定默认值为timestamp类型的字段。