我有一个cron作业,可以在一天结束时从db查询数据并将其转储到csv文件中。我正在使用mysql数据库和python客户端。 Cron作业可以正常工作几天,然后由于错误而崩溃
mysql.connector.errors.InterfaceError: 2003: Can't connect to MySQL server on 'localhost:3306'(111 Connection refused)
我的代码
def job():
x = datetime.datetime.now()
date = str(x.date() - datetime.timedelta(0))
connection = mysql.connector.connect(host='localhost',database='',user='',
password='',auth_plugin='mysql_native_password')
cursor = connection.cursor()
cursor.execute("SELECT * FROM Data WHERE date = '%s'" % date)
results = cursor.fetchall()
connection.commit()
cursor.close()
connection.close()
print(len(results))
c = csv.writer(open('milo-'+date+'.csv', 'w'))
try:
headers = [header_list]
c.writerow(i for i in headers)
print('added headers')
for x in results:
c.writerow(x)
except Exception as e:
print('CSV generation failed')
print(str(e))
如果我重新运行该脚本,它可以在几天内正常工作,然后再次崩溃。有人可以帮助我吗?