Linux

如何確保我的伺服器和 MYSQL 具有相同的時區?

  • March 31, 2010

因為當我在 Python 腳本中創建時間時……我希望它與 MYSQL 的時間相匹配。

我怎樣才能讓他們倆都和平相處?

你沒有提到作業系統,但對於 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'

Bob 是你的叔叔:

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)

引用自:https://serverfault.com/questions/127973