因为当我在 Python 脚本中创建时间时,我希望它与 MYSQL 的时间相匹配。
我怎样才能让它们都达到太平洋时间?
答案1
您没有提到操作系统,但对于 RedHat 衍生系统,应该system-config-time
设置时区。对于 MySQL,请阅读此 URL:
http://dev.mysql.com/doc/refman/5.1/en/mysql-tzinfo-to-sql.html
总而言之,我必须从操作系统加载时区:
mysql_tzinfo_to_sql /usr/share/zoneinfo | mysql -u root mysql
在``/etc/my.cnf`部分中添加以下行mysqld
并重新启动:
default-time-zone='Pacific/Honolulu'
鲍勃是你的叔叔:
mysql> SELECT @@global.time_zone, @@session.time_zone;
+--------------------+---------------------+
| @@global.time_zone | @@session.time_zone |
+--------------------+---------------------+
| Pacific/Honolulu | Pacific/Honolulu |
+--------------------+---------------------+
1 row in set (0.00 sec)
我尝试了以下测试脚本:
import MySQLdb
import os
import re
import time
import datetime
from datetime import date, datetime, time
db = 'test'
dbhost = 'localhost'
dbport = 3306
dbuser = 'test'
dbpass = 'test'
start_time = datetime.today()
con = MySQLdb.connect(host=dbhost, port=dbport, user=dbuser, passwd=dbpass, db=db)
cursor = con.cursor()
sql = "SELECT current_time;"
cursor.execute(sql)
list = cursor.fetchall()
con.close()
print "------------------------------------------------------------------------------------"
print "Python time is: "
print start_time
print
print "MYSQL time is:"
for result in list:
print result[0]
print "--
----------------------------------------------------------------------------------”
输出:
------------------------------------------------------------------------------------
Python time is:
2010-03-30 22:29:19.358184
MYSQL time is:
22:29:19
------------------------------------------------------------------------------------
您是否已填充时区表:
mysql> use mysql;
mysql> select count(*) from time_zone;
+----------+
| count(*) |
+----------+
| 1678 |
+----------+
1 row in set (0.00 sec)
答案2
在我的服务器上,我将其time_zone
设置为“SYSTEM”,如下所述:
http://dev.mysql.com/doc/refman/5.0/en/time-zone-support.html
全局 time_zone 系统变量表示服务器当前运行的时区。time_zone 的初始值是“SYSTEM”,表示服务器时区与系统时区相同。