简体   繁体   English

无法通过python连接mysql

[英]Cannot connect mysql through python

I did a python -m pip install mysql-connector and able to successfully run import mysql.connector through python.我做了一个 python -m pip install mysql-connector 并且能够通过 python 成功运行import mysql.connector But when I am trying to run the below code.但是当我尝试运行以下代码时。

import mysql.connector

mydb = mysql.connector.connect(
  host="localhost",
  user="yourusername",
  passwd="yourpassword"
)

print(mydb)

It is failing with InterfaceError: 2003: Can't connect to MySQL server on 'localhost:3306' (10061 No connection could be made because the target machine actively refused it)它失败了InterfaceError: 2003: Can't connect to MySQL server on 'localhost:3306' (10061 由于目标机器主动拒绝,无法建立连接)

Since I did pip install for mysql.connector I am not sure of user and passwd.由于我为 mysql.connector 安装了 pip ,因此我不确定用户和密码。

I connected my database instance from Amazon RDS to mySql workbench, created a python file that looks like his and got the exact error.我将我的数据库实例从 Amazon RDS 连接到 mySql 工作台,创建了一个看起来像他的 python 文件并得到了确切的错误。 Everyone says you could have a system firewall problem but nothing straight forward.每个人都说你可能有系统防火墙问题,但没有直接的问题。

The connector is just a means of communicating with a mysql database programically.连接器只是一种以编程方式与 mysql 数据库通信的方式。

You need this, or access to a mysql server to use the connector.您需要这个,或者访问 mysql 服务器以使用连接器。 https://dev.mysql.com/downloads/mysql/ https://dev.mysql.com/downloads/mysql/

The host is obviously localhost when you want to access it locally on your machine.当您想在您的机器上本地访问它时,主机显然是localhost This only works if the server is running on your machine.这仅在服务器在您的机器上运行时才有效。 You can connect a remote server by changing the host to a valid IP address and providing valid credentials.您可以通过将主机更改为有效 IP 地址并提供有效凭据来连接远程服务器。 If you use it remotely make sure the server has access through the firewall and that you properly forward the TCP port you decide to use.如果您远程使用它,请确保服务器可以通过防火墙进行访问,并且您正确转发了您决定使用的 TCP 端口。 You may not have to forward, but I would as a general rule of thumb to make it one less thing to check when troubleshooting.您可能不必转发,但作为一般经验法则,我会在故障排除时减少检查的一件事。

Good luck祝你好运

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

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