我安装了 Python 2.7 以尝试在线连接到 MySQL。基本上,MySQL 和 phpMyAdmin 在服务器上,我可以通过localhost:8888/phpmyadmin
Windows 桌面上的 putty 访问它。即使使用腻子,我似乎也无法连接到它。任何想法?我在使用 CyMySQL 的 Python 3.3 中遇到了同样的问题。
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”)
嗯,还是不行。。。