我安裝了Python 2.7以嘗試連接到MySQL在線。基本上,MySQL和phpMyAdmin在服務器上,我可以通過localhost:8888/phpmyadmin
通過膩子在我的Windows桌面上訪問它。即使使用膩子,我也無法連接到它。任何想法?我使用CyMySQL面臨與Python 3.3相同的問題。Python連接到mysql
import MySQLdb
db = MySQLdb.connect(host="127.0.0.1", # your host, usually 127.0.0.1
user="megamonster", # your username
passwd="", # your password
db="extractor") # name of the data base
# you must create a Cursor object. It will let
# you execute all the query you need
cur = db.cursor()
# Use all the SQL you like
cur.execute("SELECT * FROM abc")
# print all the first cell of all the rows
for row in cur.fetchall() :
print row[0]
錯誤:
Traceback (most recent call last):
File "C:\Users\Jonathan\Desktop\testSQL.py", line 6, in <module>
db="extractor") # name of the data base
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)
OperationalError: (2003, "Can't connect to MySQL server on '127.0.0.1' (10061)")
更新
我添加的端口(3306),並得到這個。 OperationalError:(2013年, 「在 '等待初始通信數據包',系統錯誤而丟失連接到MySQL服務器:0」)
嗯着工作還是......
@hcwhsa ???????? – CodeGuru
你可以在機器命令行上連接「mysql -umegamonster extractor」嗎? – joeButler
試試'localhost'而不是ip地址? – kevinsa5