开发者社区> 重庆八怪> 正文

LINUX时区设置及与数据库之间(ORACLE MYSQL)的关系

简介: LINUX时区    LINUX 操作系统时区由/etc/localtime设置,其可以是一个指向/usr/share/zoneinfo下文件的软连接, 当然也可以拷贝,在/usr/share/zoneinfo目录下每个文件都包含了特定地区的时区信息,很多都分...
+关注继续查看

LINUX时区
   LINUX 操作系统时区由/etc/localtime设置,其可以是一个指向/usr/share/zoneinfo下文件的软连接,
当然也可以拷贝,在/usr/share/zoneinfo目录下每个文件都包含了特定地区的时区信息,很多都分为
洲目录/地区目录 
如:
UTC:GMT标准时间+0时区
CET:欧洲中部时间
EST:美国东部标准时间
Asia/Shanghai:中国上海+8时区  (亚洲目录/上海地区)
我们可以简单的建立一个连接
cd /etc
ln -s  /usr/share/zoneinfo/Asia/Shanghai  /etc/localtime 
[root@testmy etc]# date -R
Wed, 07 Dec 2016 06:30:53 +0800
[root@testmy etc]# unlink localtime
[root@testmy etc]# ln -s  /usr/share/zoneinfo/UTC  /etc/localtime 
[root@testmy etc]# date -R
Tue, 06 Dec 2016 22:31:18 +0000

也可以拷贝:
[root@testmy etc]# cp /usr/share/zoneinfo/UTC localtime
cp: overwrite `localtime'? y
[root@testmy etc]# date
Wed Dec  7 00:13:22 UTC 2016
[root@testmy etc]# cp /usr/share/zoneinfo/Asia/Shanghai localtime
cp: overwrite `localtime'? y
[root@testmy etc]# date
Wed Dec  7 08:13:41 CST 2016

可以看到时区的变换,注意时区更改变数据库某些函数的返回

ORACLE数据库SYSDATE和SYSTIMESTAMP函数的返回将会随着系统时区的改变而改变,LOCALTIMESTAMP
CURRENT_TIMESTAMP和CURRENT_DATE ,它们跟随的是会话级别的时区也就是应用连接本地时区
下来是来自metalink的描述:

LOCALTIMESTAMP returns the current date and time in the session time zone in
 a value of datatype TIMESTAMP, that is date time similar to CURRENT_DATE 
 but the datatype is TIMESTAMP.
 
CURRENT_TIMESTAMP returns the current date and time in the session time zone,
 in a value of datatype TIMESTAMP WITH TIME ZONE, that is date time similar to 
 CURRENT_DATE but the datatype is TIMESTAMP WITH TIME ZONE.

SYSTIMESTAMP returns the system date, including fractional seconds and time zone,
 of the system on which the database resides. The return type is TIMESTAMP WITH 
 TIME ZONE. Unlike SYSDATE, which you can set to a constant using FIXED_DATE, 
 SYSTIMESTAMP will give the system date even though FIXED_DATE is set.
 
"SYSDATE" and "SYSTIMESTAMP" are purely dependent on the operating system clock, 
hence it IS depending on the timezone information of this operating system and/or 
the operating system settings when the database and listener where started.


列子:
SQL>  select to_CHAR(systimestamp,'YYYY-MM-DD HH24:MI:SS:TZR') from dual;
TO_CHAR(SYSTIMESTAMP,'YYYY-MM-
----------------------------------------------------
2016-12-06 23:00:16:+00:00


更改系统时区后,重新连接一个session
SQL>  select to_CHAR(systimestamp,'YYYY-MM-DD HH24:MI:SS:TZR') from dual;
TO_CHAR(SYSTIMESTAMP,'YYYY-MM-
----------------------------------------------------
2016-12-07 07:00:58:+08:00

看到时区改变了

MYSQL数据库如果设置参数:
time_zone 为system,那么代表每一个连接线程使用服务器OS的系统时间
那么时区受
| system_time_zone                   | UTC               |
的影响,可以看到我这里是UTC标准时区
那么这个时候某些函数如now(),current_time(),sysdate()都将受到影响,
但是和ORACLE不同如果MYSQL数据库不重启数据库将不会受到影响

列子:
mysql> show variables like '%time_zone%';
+------------------+--------+
| Variable_name    | Value  |
+------------------+--------+
| system_time_zone | UTC    |
| time_zone        | SYSTEM |
+------------------+--------+
2 rows in set (0.00 sec)


mysql> select now(),current_time(),sysdate();
+---------------------+----------------+---------------------+
| now()               | current_time() | sysdate()           |
+---------------------+----------------+---------------------+
| 2016-12-06 23:06:17 | 23:06:17       | 2016-12-06 23:06:17 |
+---------------------+----------------+---------------------+
更改OS时区后重启MYSQL数据库
mysql> show variables like '%time_zone%';
+------------------+--------+
| Variable_name    | Value  |
+------------------+--------+
| system_time_zone | CST    |
| time_zone        | SYSTEM |
+------------------+--------+
2 rows in set (0.00 sec)

mysql> select now(),current_time(),sysdate();
+---------------------+----------------+---------------------+
| now()               | current_time() | sysdate()           |
+---------------------+----------------+---------------------+
| 2016-12-07 07:07:11 | 07:07:11       | 2016-12-07 07:07:11 |
+---------------------+----------------+---------------------+
1 row in set (0.00 sec)
可以看到时区改变。
当然time_zone是连接级别可以设置的,我们可以设置它为正确的时区,而不依赖
OS的时区如:set global  time_zone = '+8:00'; 


最后还是给出一段简单的代码用于查看时区的变化对系统时间的影响

gaopeng@bogon:~/linuxapinew$ TZ="Asia/Shanghai"  ./a.out 
ctime() is:Wed Dec 28 19:15:55 2016
asmtime is:Wed Dec 28 19:15:55 2016
gaopeng@bogon:~/linuxapinew$ TZ="UTC"  ./a.out              
ctime() is:Wed Dec 28 11:16:01 2016
asmtime is:Wed Dec 28 11:16:01 2016

代码如下:

点击(此处)折叠或打开

  1. /*************************************************************************
  2.     > File Name: timezone.c
  3.     > Author: gaopeng QQ:22389860 all right reserved
  4.     > Mail: gaopp_200217@163.com
  5.     > Created Time: Wed 28 Dec 2016 06:57:50 PM CST
  6.  ************************************************************************/

  7. #include<stdio.h>
  8. #include <time.h>
  9. #include <errno.h>


  10. int main(void)
  11. {
  12.         time_t t;
  13.         struct tm *loc;
  14.         char* ctc;
  15.         char* asc;

  16.         t = time(NULL);
  17.         ctc = ctime(&t);
  18.         if ((loc = localtime(&t)) ==NULL)
  19.         {
  20.                 perror("localtime:");
  21.         }

  22.         asc = asctime(loc);

  23.         printf("ctime() is:%sasmtime is:%s",ctc,asc);
  24.         return 0;

  25. }


版权声明:本文内容由阿里云实名注册用户自发贡献,版权归原作者所有,阿里云开发者社区不拥有其著作权,亦不承担相应法律责任。具体规则请查看《阿里云开发者社区用户服务协议》和《阿里云开发者社区知识产权保护指引》。如果您发现本社区中有涉嫌抄袭的内容,填写侵权投诉表单进行举报,一经查实,本社区将立刻删除涉嫌侵权内容。

相关文章
SpringCloud Alibaba学习(四):Linux版Nacos+MySQL生产环境配置
SpringCloud Alibaba学习(四):Linux版Nacos+MySQL生产环境配置
43 0
linux下开启、关闭、重启mysql服务命令
linux下开启、关闭、重启mysql服务命令
46 0
在Linux下安装Mysql教程(图文)
在Linux下安装Mysql教程(图文)
87 0
【Linux】挂载硬盘、镜像文件、rpm软件包管理器、yum软件包管理器、软件安装jdk、tomcat、mysql
【Linux】挂载硬盘、镜像文件、rpm软件包管理器、yum软件包管理器、软件安装jdk、tomcat、mysql
31 0
linux下查找mysql配置文件及错误检查方法
linux下查找mysql配置文件及错误检查方法
133 0
Linux安装Mysql(图文解说详细版,安装包tar包版)
Linux安装Mysql(图文解说详细版,安装包tar包版)
88 0
Linux系统:第十二章:AWS服务器X86架构安装配置Mysql与MongoDB
Linux系统:第十二章:AWS服务器X86架构安装配置Mysql与MongoDB
42 0
在Linux服务器上安装MySQL并配置,远程连接,以及MySQL的一些常规操作
在Linux服务器上安装MySQL并配置,远程连接,以及MySQL的一些常规操作
120 0
Linux(Centos7)离线安装mysql
Linux(Centos7)离线安装mysql
22 0
+关注
重庆八怪
10年ORACLE/MYSQL DBA,有一定C/C++基础
文章
问答
文章排行榜
最热
最新
相关电子书
更多
Decian GNU/Linux安全合规之路
立即下载
从 Linux 系统内核层面来解决实际问题的实战经验
立即下载
冬季实战营第二期:Linux操作系统实战入门
立即下载