python - Python 中的 MySQL 连接器

标签 python mysql timeout connector

尝试连接到我的 VPS 上的 MySQL 数据库。使用以下代码时出现以下错误;

(我已经删除了我的凭据 - 但我知道它们有效,因为我也将它们用于我的 PHP 开发。

代码

import mysql.connector as mysql
from mysql.connector import errorcode

try:
  conn = mysql.connect( user= %USER%
                        ,password=%PW%
                        ,host = %HOST%
                        ,database = %DB%
    )
except mysql.Error as err:
  if err.errno == errorcode.ER_ACCESS_DENIED_ERROR:
    print("Something is wrong with your user name or password")
  elif err.errno == errorcode.ER_BAD_DB_ERROR:
    print("Database does not exists")
  else:
    print(err)
else:
    conn.close()

错误消息

2003: Can't connect to MySQL server on '%HOST%:3306' (10060 A connection attempt failed because the connected party did not properly respond after a period of time, or established   connection failed because connected host has failed to respond)

我正在使用的连接器

http://cdn.mysql.com/Downloads/Connector-Python/mysql-connector-python-2.0.1-py3.4.msi

知道我做错了什么吗?

最佳答案

检查GRANT在您的 MySQL 数据库上。可能是您未被授予使用给定用户名和密码从该主机进行连接的权限。

MySQL 错误号是 2003。我通常发现通过 Google 搜索看看其他人是否也遇到过这个问题会很有帮助:

http://dev.mysql.com/doc/refman/5.0/en/access-denied.html

客户端计算机和数据库服务器之间是否有防火墙?我希望默认情况下会阻止 3306 端口。如果是这样,您需要设置防火墙规则以允许客户端 IP 通过端口 3306 访问数据库 IP。

关于python - Python 中的 MySQL 连接器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26381260/

相关文章:

简单连接到 MySQL 时 PHP 超时 - 在 win2k8 64 位 IIS 7/FastCGI php 上 - 新安装

javascript - d3 每次迭代等待 n 秒

python - Tkinter 如何循环浏览图像?

python - 该功能不适用于所有表格

python - Django 升级 : Static Files Not Served

mysql - 按日期范围检索数据

MySQL 查询不同行的计数

java - Android 2.2 (level 8) httpclient.execute 一致超时

php - 在特定时间后在 php 中终止函数?

python - 在 Python 中创建类后向其添加方法