【问题标题】:OperationalError: (2003, "Can't connect to MySQL server on 'mysql2.000webhost.com' (10060)")OperationalError:(2003,“无法连接到 'mysql2.000webhost.com' (10060) 上的 MySQL 服务器”)
【发布时间】:2014-05-13 19:34:44
【问题描述】:

我一直在学习一些基本的 MySQL,并决定将它放入我有更多经验的 Python 中。我有 PHP 脚本在这个 MySQL 服务器上运行,所以我知道服务器正在运行。但是每次连接时都会出现此错误:

Traceback (most recent call last):
File "E:/Geekster_Bot/Geekster_Bot Code/Geekster_Bot_alpha_API_MySQL", line 6, in <module>
db="MY DATABASE")# 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 'mysql2.000webhost.com' (10060)")

我真的不知道为什么。我已经打开了 MySQL 端口(3306),就像我说的,服务器/数据库正在工作。有什么想法吗?

这是整个MySQL连接的代码;

import MySQLdb

db = MySQLdb.connect(host="mysql2.000webhost.com", # your host, usually localhost
                 user="MY USER", # your username
                  passwd="MY PASSWORD", # your password
                  db="MY DATABASE")# name of the data base

 #you must create a Cursor object. It will let
 #you execute all the queries you need
 cur = db.cursor() 

 #Use all the SQL you like
 cur.execute("SELECT * twitch_follows_ign")

 #print all the first cell of all the rows
 for row in cur.fetchall() :
 print row[0]

所有被屏蔽的数据都输入正确。

【问题讨论】:

  • 000webhost 不允许外部 MySQL 连接,并且由于免费帐户不支持 Python,我假设您没有从主机运行它,这会导致错误。

标签: python mysql operationalerror


【解决方案1】:

您的数据库似乎位于外部服务器“mysql2.000webhost.com”上,在您从 Python 应用程序连接到 MySQL 服务器之前,您必须创建一个数据库用户并授予该用户从您的服务器访问服务器的权限IP 地址。

如果您在“000webhost”上有一个免费帐户,您可能无法创建一个可以从外部访问数据库的数据库用户。 试试这个网站http://www.freemysqlhosting.net/,他们将授予您从任何 IP 地址访问新创建的数据库的权限。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-08-05
    • 2019-04-10
    • 2014-03-10
    • 2014-08-22
    • 2014-11-08
    • 1970-01-01
    相关资源
    最近更新 更多