我安装了Python 2.7以尝试在线连接到MySQL。基本上,MySQL和phpMyAdmin在服务器上,我可以通过我的Windows桌面上的putty通过localhost:8888/phpmyadmin
访问它。即使用腻子,我似乎无法连接到它。任何的想法?我使用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”)
仍然无法工作......
答案 0 :(得分:2)
我使用sqlplus工作
sqlplus User_name/password@Host_ip:Port/Service_Name@$SQLFILENAME
如果要使用文件,只需指定SQLFILENAME
即可。否则,您可以忽略此参数并可以直接运行sql语句
答案 1 :(得分:1)
可能有很多问题,但就MySQL而言,localhost
和127.0.0.1
的权限是独立设置的。确保您可以使用确切的主机和凭据进行连接。 Possibly related
例如,在与PUTTY连接连接时进行检查。
mysql> use mysql;
Database changed
mysql> SELECT host,user,select_priv FROM user;
+-------------------------+------+-------------+
| host | user | select_priv |
+-------------------------+------+-------------+
| localhost | root | Y |
| 127.0.0.1 | root | Y |
+-------------------------+------+-------------+
同时检查您的连接对象(在PUTTY上)并在脚本中使用相同的信息:
mysql> SELECT USER(),CURRENT_USER();
+----------------+----------------+
| USER() | CURRENT_USER() |
+----------------+----------------+
| root@localhost | root@localhost |
+----------------+----------------+