2012-04-13 21 views
0

我正在使用mysqldb/python将一些数据推送到mysql数据库中。MYSQL - 服务器通过脚本中途退出

该脚本解析一堆XML文件的数据。

MySQL服务器似乎退出,并给我一个'#2002 - 服务器没有响应(或本地MySQL服务器的套接字配置不正确)'错误中途通过交易 - 在我每次运行时在不同的地方它(所以我假设数据是使其翻倒它不是一个特定的一块...)

它完美,直到它到达〜12或13文件,它给了我这个错误:

Error 2003: Can't connect to MySQL server on 'localhost' (10055) 
Traceback (most recent call last): 
File "sigFileParser.py", line 113, in <module> 
doParser(sigfile_filename) 
File "sigFileParser.py", line 106, in 
doParser 
    doFormatsPush(packedFormats) 
File "sigFileParser.py", line 27, in 
doFormatsPush 
sys.exit (1) 
NameError: global name 'sys' is not defined 

一旦发生错误,我无法进入MySQL控制台或通过PHOPmyadmin

如果我离开,如果有一阵子,我可以回去到MySQL

MySQL表:

CREATE TABLE IF NOT EXISTS patterns 
(Version int(3), 
DateCreated DATETIME, 
SigID int(4), 
SigSpecificity CHAR(10), 
ByteSeqReference CHAR(12), 
MinFragLength int(4), 
Position int(4), 
SubSeqMaxOffset int(4), 
SubSeqMinOffset int(4), 
Pattern TEXT) 

CREATE TABLE IF NOT EXISTS formats 
(Version int(3), 
DateCreated DATETIME, 
FormatID int(4), 
FormatName TEXT, 
PUID TEXT, 
FormatVersion TEXT, 
FormatMIMEType TEXT, 
InternalSignatureID int(4), 
Extension TEXT, 
HasPriorityOverFileFormatID int(4)) 

PY代码

from lxml import etree 
import re, os, MySQLdb 
def doPatternPush(packedPatterns): 
try: 
    db = MySQLdb.connect (host = "localhost", user = "root", passwd = "", db = "sigfiles") 
    c = db.cursor() 
    c.execute('''INSERT INTO sigfiles.patterns 
    (Version,DateCreated,SigID,SigSpecificity,ByteSeqReference,MinFragLength,Position,SubSeqMaxOffset,SubSeqMinOffset,Pattern) 
    VALUES (%s, %s, %s, %s, %s, %s, %s, %s, %s, %s)''', packedPatterns) 
    db.commit() 
    except MySQLdb.Error, e: 
    print "Error %d: %s" % (e.args[0], e.args[1]) 
    sys.exit (1) 
return (db) 
def doFormatsPush(packedFormats): 
try: 
    db = MySQLdb.connect (host = "localhost", user = "root", passwd = "", db = "sigfiles") 
    c = db.cursor() 
    c.execute('''INSERT INTO sigfiles.formats 
    (Version,DateCreated,FormatID,FormatName,PUID,FormatVersion,FormatMIMEType,InternalSignatureID,Extension,HasPriorityOverFileFormatID) 
    VALUES (%s, %s, %s, %s, %s, %s, %s, %s, %s, %s)''', packedFormats) 
    db.commit() 
except MySQLdb.Error, e: 
    print "Error %d: %s" % (e.args[0], e.args[1]) 
    sys.exit (1) 
return(db) 
def doParser(sigfile_filename): 
tree = etree.parse(sigfile_filename) 
root = tree.getroot() 
attributes = root.attrib 
if 'DateCreated' in root.attrib: 
    DateCreated = (attributes["DateCreated"]) 
if 'Version' in root.attrib: 
    Version = (attributes["Version"]) 
##--------- get internal sig details ------------------ 
for a in range (len(root[0])): #loops for sig ID 
    attributes = root[0][a].attrib 
    SigID=(attributes["ID"]) 
    SigSpecificity = (attributes["Specificity"]) 
    for b in range (len(root[0][a])): # loops for sequence pattern inside each sig 
    attributes = root[0][a][b].attrib 
    if 'Reference' in root[0][a][b].attrib: 
    ByteSeqReference = (attributes["Reference"]) 
    else: 
    ByteSeqReference = "NULL" 
    attributes = root[0][a][b][0].attrib 
    if 'MinFragLength' in root[0][a][b][0].attrib: 
    MinFragLength=(attributes["MinFragLength"]) 
    else: 
    MinFragLength='' 
    if 'Position' in root[0][a].attrib: 
    Position=(attributes["Position"]) 
    else: 
    Position='' 
    if 'SubSeqMaxOffset' in root[0][a][b][0].attrib: 
    SubSeqMaxOffset=(attributes["SubSeqMaxOffset"]) 
    else: 
    SubSeqMaxOffsee = '' 
    if 'SubSeqMinOffset' in root[0][a][b][0].attrib: 
    SubSeqMinOffset=(attributes["SubSeqMinOffset"]) 
    else: 
    SubSeqMinOffset = ''  
    Pattern = root[0][a][b][0][0].text 
    packedPatterns =  [Version,DateCreated,SigID,SigSpecificity,ByteSeqReference,MinFragLength,Position,SubSeqMaxOffset,SubSeqMinOffset,Pattern] 
    doPatternPush(packedPatterns) 
##-------- get format ID details------------- 
for a in range (len(root[1])): 
    attributes = root[1][a].attrib 
    if 'ID' in root[1][a].attrib: 
    FormatID = (attributes['ID']) 
    else: 
    FormatID = "NULL" 
    if 'Name' in root[1][a].attrib: 
    FormatName = (attributes['Name']) 
    else: 
    FormatName = "NULL" 
    if 'PUID' in root[1][a].attrib: 
    PUID = (attributes['PUID']) 
    else: 
    PUID = "NULL" 
    if 'Version' in root[1][a].attrib: 
    FormatVersion = (attributes['Version']) 
    else: 
    FormatVersion = "NULL" 
    if 'MIMEType' in root[1][a].attrib: 
    FormatMIMEType = (attributes['MIMEType']) 
    else: 
    FormatMIMEType = "NULL" 
    InternalSignatureID,Extension,HasPriorityOverFileFormatID = ('', 'NULL', '') 
    for b in range (len(root[1][a])): #extracts the tags for each format ID 
    tagType = root[1][a][b].tag 
    tagText = root[1][a][b].text 
    tagType = re.sub('{http://www.nationalarchives.gov.uk/pronom/SignatureFile}', '', tagType) 
    if tagType == 'InternalSignatureID': 
    InternalSignatureID = tagText 
    elif tagType == 'Extension': 
    Extension = tagText 
    HasPriorityOverFileFormatID = '' 
    else: 
    HasPriorityOverFileFormatID = tagText 
    Extension = 'NULL' 
    packedFormats = [Version,DateCreated,FormatID,FormatName,PUID,FormatVersion,FormatMIMEType,InternalSignatureID,Extension,HasPriorityOverFileFormatID] 
    doFormatsPush(packedFormats) 
if __name__ == "__main__": 
path = "C:\Users\NDHA\Desktop\droid sigs all" 
for (path, dirs, files) in os.walk(path): 
    for file in files: 
    sigfile_filename = str(path)+"\\"+str(file) 
    doParser(sigfile_filename) 
    print sigfile_filename 
db.close()  

所有XML来自这里:http://www.nationalarchives.gov.uk/aboutapps/pronom/droid-signature-files.htm

回答

1

你得到的这个错误告诉你到底出了什么问题

NameError: global name 'sys' is not defined 

你在你的Python文件不import sys

至于数据库连接,如果您的套接字没有放置在/tmp/mysql.sock中,那么当您尝试使用参数unix_socket连接到数据库时,可以指定在哪里查找它。

尝试:

db = MySQLdb.connect (unix_socket = 'path_to_sock', host = "localhost", 
         user = "root", passwd = "", db = "sigfiles") 

如果你的实际路径到mysql袜子取代 'path_to_sock'。

你应该检查的情况下其他的东西,这不是问题:

  • 检查以确保用户名/密码组合是正确
  • 尝试停止并重新启动mysqld服务
  • 检查错误日志文件以更具体的错误
+0

我已经导入SYS,问题仍然存在,感谢您的期待! – Jay 2012-04-13 03:38:38

+0

我如何知道我的权威mysql袜子是什么? – Jay 2012-04-13 03:43:25

+0

你可以试着在你的系统上找到这个文件。假设你使用的是基于Linux的系统,可以在终端 – 2012-04-13 03:48:54

0

这是你的第一个错误:

Error 2003: Can't connect to MySQL server on 'localhost' (10055)

看来你在某些时候断开了与MySQL的连接。检查你的代码,看看你是否显式或隐式地从服务器断开连接,并检查你的MySQL服务器是否仍在监听连接......也许你是从应用程序外部杀死服务器......谁知道?:)

+1

Yupe,我可以看到mysql服务器正在死亡,但我不知道为什么......当它失败时它锁定了几分钟,并淘汰整个网络连接(基于浏览器不响应新的页面请求 - 它不超时,它只是不响应)。没有什么会写入mysql.log – Jay 2012-04-13 03:48:36

+0

嗯,这是一个相当奇怪的行为。我不认为MySQL正在淘汰整个网络。您的计算机正在发生其他事情......恶意软件可能是? – 2012-04-13 03:54:07