简体   繁体   English

无法连接到 Python 中的 MySql

[英]Can't connect to MySql in Python

How do I debug this?我该如何调试?

mydb = pymysql.connect(
            host="<db-host-ip>",
            port=3306,
            user="<username>",
            password="<password>",
            database="<dbname>"
 )

fails with:失败:

pymysql.err.OperationalError: (1045, "Access denied for user '<myuser>'@'<long_and_boring_hostname>' (using password: YES)")

while this:而这:

mysql --host=<db-host-ip> --user=<username> --password=<password> <dbname>

connects just fine.连接得很好。

I can also connect to the db from java and MySql workbench我还可以从 java 和 MySql 工作台连接到数据库

I've tried using mysql.connector, same error我试过使用 mysql.connector,同样的错误

I can connect to another db from Python just fine我可以从 Python 连接到另一个数据库就好了

Why does Python error has long hostname after my username?为什么 Python 错误在我的用户名后面有很长的主机名? can it be suppressed?可以压制吗?

** Some more details ** **更多细节**

MySql is remote, poython and other tools are local MySql为远程,poython等工具为本地

I am connecting just fine to other databases, only this one seems to conflict.我可以很好地连接到其他数据库,只有这个似乎有冲突。

My main question is how can I troubleshoot this, I need to know more than "Access denied"我的主要问题是如何解决这个问题,我需要知道的不仅仅是“访问被拒绝”

Questions: (1) Which computer is running your Python script?问题:(1)哪台计算机正在运行您的 Python 脚本? (2) Which computer is running your MySQL command-line? (2) 哪台计算机正在运行您的 MySQL 命令行? (3) Which computer is running MySQL database? (3) 哪台电脑运行MySQL数据库?

If all 3 of these questions don't have the same answer, then it's possible that you have a port issue in MySQL user administration.如果所有 3 个问题都没有相同的答案,那么您可能在 MySQL 用户管理中遇到了端口问题。 In other words, it may be possible that your MySQL server settings are not allowing remote access … if so, there are different ways to troubleshoot but that may require a DBA/expert in MySQL.换句话说,您的 MySQL 服务器设置可能不允许远程访问……如果是这样,有不同的故障排除方法,但可能需要 MySQL 的 DBA/专家。

Separately, I have used the 'sqlalchemy' Python library and it works well, especially if you run the connection_engine through pandas.read_sql().另外,我使用了“sqlalchemy”Python 库,它运行良好,特别是如果您通过 pandas.read_sql() 运行 connection_engine。 I have never used pymysql.我从来没有使用过pymysql。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM