如何使用python连接mysql

时间:2019-06-15 09:38:37

标签: python mysql

我正在尝试使用python连接mysql数据库,但是运行它时却给我一个错误。 这是我的代码:

import MySQLdb
import os

db = MySQLdb.connect(host="dev.addverb.in",port=1433,user="aditya",passwd="Aditya@123!",db="wcs")

cursor = db.cursor()

sql="SELECT * FROM outbound_master WHERE status='%d " % (1)

try:
    cursor.excute(sql)

    result = cursor.fetchall()

    for row in result:
        hostname = "google.com" 
        response = os.system("ping -c 1 " + hostname)
        if response == 0:
            print (hostname, 'is up!')
        else:
            print (hostname, 'is down!')

except:
    print ("error")

这是我运行代码时出现的错误:


Traceback (most recent call last):
  File "test.py", line 4, in <module>
    db = MySQLdb.connect(host="dev.addverb.in",port=1433,user="aditya",passwd="Aditya@123!",db="wcs")

  File "C:\Users\HP\AppData\Local\Programs\Python\Python35\lib\site-packages\MySQLdb\__init__.py", line 86, in Connect
    return Connection(*args, **kwargs)

  File "C:\Users\HP\AppData\Local\Programs\Python\Python35\lib\site-packages\MySQLdb\connections.py", line 204, in __init__
    super(Connection, self).__init__(*args, **kwargs2)

_mysql_exceptions.OperationalError: (2013, "Lost connection to MySQL server at 'reading initial communication packet', system error: 0")

请告诉我该怎么办?任何帮助将不胜感激。 谢谢!

0 个答案:

没有答案