Python 系统时间与Mysql时间对比

本文涉及的产品
云数据库 RDS MySQL Serverless,0.5-2RCU 50GB
云数据库 RDS MySQL Serverless,价值2615元额度,1个月
简介:

        由于自己是负责海外项目,常常会遇到一些问题,最近被系统时间与mysql时间不在一个时区,而坑了自己,一般修改了系统时区之后,MySQL必须重启,不然MySQL时区是不对的,会导致数据全部都是错的~~~,哎,只有坑到了自己,才会想到要去避免这种事情再次出现,所以用python写了一个简单判断时区的脚本,时区不对并邮件发出来,大家参考参考,详情如下:

1、脚本实例

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
#!/usr/bin/env python
# coding=utf8
# auther:kuangl
# This is system time and sql time diff
 
from   datetime   import   *
import  os,sys,socket,fcntl,struct
import  MySQLdb
import  smtplib
from  email.mime.multipart  import  MIMEMultipart
from  email.mime.base  import  MIMEBase
from  email.mime.text  import  MIMEText
reload (sys)
sys.setdefaultencoding( 'utf8' )
 
'''定义发送邮件函数'''
def  sendmail(html,emailaddress,mailSubject,from_address = "monitor@test.com" ):
         mail_list = emailaddress.split( "," )
         msg = MIMEMultipart()
         msg[ 'Accept-Language' ] = 'zh-CN'
         msg[ 'Accept-Charset' ] =  'ISO-8859-1,utf-8'
         msg[ 'From' ] = from_address
         msg[ 'to' ] = ";" .join(mail_list)
         msg[ 'Subject' ] = mailSubject.decode( "utf-8" )
         txt = MIMEText(html, 'html' , 'utf-8' )
         txt.set_charset( 'utf-8' )
         msg.attach(txt)
         smtp = smtplib.SMTP( "mail.test.com" )
         smtp.sendmail(msg[ "From" ],mail_list,msg.as_string())
         smtp.close()
 
'''查看本机hostname'''
hostname = socket.gethostname()
print  hostname
 
'''查看本机IP地址'''
def  get_ip_address(ifname):
     s = socket.socket(socket.AF_INET,socket.SOCK_DGRAM)
     return  socket.inet_ntoa(fcntl.ioctl(
             s.fileno(),
             0x8915 ,
             struct.pack( '256s' ,ifname[: 15 ])
     )[ 20 : 24 ])
 
ip_add  =  get_ip_address( 'eth0' )
print  ip_add
 
'''查看系统时间'''
nowtime  =  datetime.now()
daytime  =  nowtime.strftime( '%Y-%m-%d %H:%M' )
print  'system time time:' , daytime
 
 
'''查看数据库时间'''
pwd = '111133334444'
SQL = 'select now()'
def  mysql_connect(sql,host):
     try :
         conn = MySQLdb.connect(host = '127.0.0.1' ,user = 'rd' ,passwd = pwd,port = 3306 )
         cur = conn.cursor()
         cur.execute(sql)
         result = cur.fetchall()
         cur.close()
         conn.close()
         return  result
     except  MySQLdb.Error,e:
         print  "Mysql Error %d: %s"  %  (e.args[ 0 ],e.args[ 1 ])
server_result = mysql_connect(sql = SQL,host = '127.0.0.1' )
sql_gettime = server_result[ 0 ][ 0 ]
SQLTime = sql_gettime.strftime( '%Y-%m-%d %H:%M' )
print  'SQL server time:' ,SQLTime
 
''' 定义邮件参数'''
Content =  'Dear ALL: <br> &nbsp;&nbsp; '  +  ip_add  +  ' , System and Database time error, <br> &nbsp;&nbsp; System time is : ' +  daytime  + ' <br> &nbsp;&nbsp; Database time is : '  +  SQLTime  +  ' <br> &nbsp;&nbsp; Please check all time! <br> &nbsp;&nbsp; <br> &nbsp;&nbsp; Friendly reminder,Please note!'
Subject =  '[监控][海外时区监控]['  +  hostname  +  ']System and Database time error'
 
''' 判断时间是否相等'''
if  daytime  = =  SQLTime:
     print  "system and sql time is OK, not sednemail"
else :
     print  "system and sql time is Fail,Start email to all"
     sendmail(html = Content,emailaddress = 'kuangling@test.com' ,mailSubject = Subject)

2、测试结果

wKiom1S-Z5zRt5mTAADj3H9YT60609.jpg



本文转自 kuangling 51CTO博客,原文链接:http://blog.51cto.com/kling/1606401


相关实践学习
基于CentOS快速搭建LAMP环境
本教程介绍如何搭建LAMP环境,其中LAMP分别代表Linux、Apache、MySQL和PHP。
全面了解阿里云能为你做什么
阿里云在全球各地部署高效节能的绿色数据中心,利用清洁计算为万物互联的新世界提供源源不断的能源动力,目前开服的区域包括中国(华北、华东、华南、香港)、新加坡、美国(美东、美西)、欧洲、中东、澳大利亚、日本。目前阿里云的产品涵盖弹性计算、数据库、存储与CDN、分析与搜索、云通信、网络、管理与监控、应用服务、互联网中间件、移动服务、视频服务等。通过本课程,来了解阿里云能够为你的业务带来哪些帮助 &nbsp; &nbsp; 相关的阿里云产品:云服务器ECS 云服务器 ECS(Elastic Compute Service)是一种弹性可伸缩的计算服务,助您降低 IT 成本,提升运维效率,使您更专注于核心业务创新。产品详情: https://www.aliyun.com/product/ecs
相关文章
|
1月前
|
SQL 关系型数据库 MySQL
python操作mysql
python操作mysql
|
1月前
|
JSON 关系型数据库 数据库
【python】Python将100个PDF文件对应的json文件存储到MySql数据库(源码)【独一无二】
【python】Python将100个PDF文件对应的json文件存储到MySql数据库(源码)【独一无二】
【python】Python将100个PDF文件对应的json文件存储到MySql数据库(源码)【独一无二】
|
12天前
|
SQL 关系型数据库 MySQL
使用Python的pymysql库连接MySQL,执行CRUD操作
使用Python的pymysql库连接MySQL,执行CRUD操作:安装pymysql,然后连接(host=&#39;localhost&#39;,user=&#39;root&#39;,password=&#39;yourpassword&#39;,database=&#39;yourdatabase&#39;),创建游标。查询数据示例:`SELECT * FROM yourtable`;插入数据:`INSERT INTO yourtable...`;更新数据:`UPDATE yourtable SET...`;删除数据:`DELETE FROM yourtable WHERE...`。
26 0
|
12天前
|
SQL 关系型数据库 MySQL
Python操作mysql数据库
Python操作mysql数据库
|
17天前
|
缓存 NoSQL 关系型数据库
在Python Web开发过程中:数据库与缓存,MySQL和NoSQL数据库的主要差异是什么?
MySQL与NoSQL的主要区别在于数据结构、查询语言和可扩展性。MySQL是关系型数据库,依赖预定义的数据表结构,使用SQL进行复杂查询,适合垂直扩展。而NoSQL提供灵活的存储方式(如JSON、哈希表),无统一查询语言,支持横向扩展,适用于处理大规模、非结构化数据和高并发场景。选择哪种取决于应用需求、数据模型及扩展策略。
26 0
|
22天前
|
SQL 关系型数据库 MySQL
Python与MySQL数据库交互:面试实战
【4月更文挑战第16天】本文介绍了Python与MySQL交互的面试重点,包括使用`mysql-connector-python`或`pymysql`连接数据库、执行SQL查询、异常处理、防止SQL注入、事务管理和ORM框架。易错点包括忘记关闭连接、忽视异常处理、硬编码SQL、忽略事务及过度依赖低效查询。通过理解这些问题和提供策略,可提升面试表现。
34 6
|
29天前
|
SQL 关系型数据库 MySQL
「Python系列」Python MySQL
在Python中使用MySQL数据库通常涉及使用一个称为`mysql-connector-python`的库,这是MySQL官方推荐的Python连接器。下面是如何在Python中连接到MySQL数据库、执行查询和插入数据的基本步骤。
33 1
|
索引 Python
python——时间模块
python——时间模块
python——时间模块
|
Python
2018最后一天,Python的时间模块
2018最后一天,Python的时间模块
2018最后一天,Python的时间模块