尝试连接到我的 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/