在2008 vm上运行我的python脚本时出现以下错误 运行mysql服务器5.6 使用ajax
Traceback (most recent call last):
File "mypythonjob.py", line 22, in <module>
db = mdb.connect('localhost', 'website','servername', 'website')
File "C:\Python27\lib\site-packages\MySQLdb\__init__.py", line 81, in Connect
return Connection(*args, **kwargs)
File "C:\Python27\lib\site-packages\MySQLdb\connections.py", line 187, in __init__
super(Connection, self).__init__(*args, **kwargs2)
_mysql_exceptions.OperationalError: (2003, "Can't connect to MySQL server on 'localhost' (10055)")
我可以在资源监视器中观察,因为cpu在大约75秒后爬升到100%,mysql.exe有30个线程,而python.exe有6个线程 错误开始,python.exe终止,mysql服务器无法访问大约2分钟,然后它重新上线。
import os, datetime, pymssql , time, subprocess
import MySQLdb as mdb
today = datetime.datetime.now().replace(hour=0, minute=0, second=0, microsecond=0)
sixmonth = today - datetime.timedelta(days=180)
db = mdb.connect('localhost', 'website','server', 'website') #values changed for post
cursor = db.cursor()
sql2 = "select 1column from mytable where 4column like '"+str(today)+"'" #values changed for post (query produces 1100 rows)
cursor.execute(sql2)
data2 = cursor.fetchall()
cursor.close()
db.close()
for row2 in data2:
db = pymssql.connect(host="sqldb",user="username", password="pwd", database="somedatabase") #values changed for post
cursor = db.cursor()
sql3 = "SET TRANSACTION ISOLATION LEVEL READ UNCOMMITTED; select col1,col2,col3 from tbl where col1 like '%somedata%' and col3 < '"+str(sixmonth)+"' and col2 ='data'" #values changed for post
cursor.execute(sql3)
data3 = cursor.fetchall()
db.commit()
cursor.close()
db.close()
for row3 in data3:
db = mdb.connect('localhost', 'website','server', 'website')
cursor = db.cursor()
sql4 = "update mytable set 2column ='"+str(row3[2])+"', 3column ='"+str(row3[1])+"' where 4column like '"+str(today)+"' and 1column like '"+str(row3[0])+"'" #values changed for post (seems to finish the update properly at 575 rows)
cursor.execute(sql4)
data4 = cursor.fetchall()
db.commit()
cursor.close()
db.close()
分辨率尝试 用一小段10行起始数据和1100运行代码 检查以确保我关闭连接和数据库时我已完成它们 我检查mysql日志文件什么都没有 将my.ini中的设置max_allowed_packet更改为32mg然后500mg,最后注释掉
感谢您寻找
补充说明: 如果我删除SET TRANSACTION ISOLATION LEVEL READ UNCOMMITTED;从第二个查询它似乎继续运行。我让它走了5分钟,计数停在575更新并保持在那里。
答案 0 :(得分:1)
解决了这个问题,我没有正确地将变量从第一个查询传递到第二个查询,因此它使用结果查杀服务器,然后将该膨胀行计数传递给更新。