【问题标题】:Unable to connect Python to MySQL无法将 Python 连接到 MySQL
【发布时间】:2021-03-21 13:59:03
【问题描述】:

我正在使用 pycharm 2017 和 SQL Server 2019,我的问题是我使用以下代码:

import mysql.connector

cnx = mysql.connector.connect(user='sa', password='pooria1376',
                              host='localhost',
                              database='pooriadb')
cnx.close()

我看到了这个错误:

C:\Python34\python.exe C:/Users/LA/PycharmProjects/untitled/main
Traceback (most recent call last):
  File "C:\Python34\lib\site-packages\mysql\connector\network.py", line 509, in open_connection
    self.sock.connect(sockaddr)
ConnectionRefusedError: [WinError 10061] No connection could be made because the target machine actively refused it

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "C:/Users/LA/PycharmProjects/untitled/main", line 5, in <module>
    database='pooriadb')
  File "C:\Python34\lib\site-packages\mysql\connector\__init__.py", line 179, in connect
    return MySQLConnection(*args, **kwargs)
  File "C:\Python34\lib\site-packages\mysql\connector\connection.py", line 95, in __init__
    self.connect(**kwargs)
  File "C:\Python34\lib\site-packages\mysql\connector\abstracts.py", line 716, in connect
    self._open_connection()
  File "C:\Python34\lib\site-packages\mysql\connector\connection.py", line 206, in _open_connection
    self._socket.open_connection()
  File "C:\Python34\lib\site-packages\mysql\connector\network.py", line 512, in open_connection
    errno=2003, values=(self.get_address(), _strioerror(err)))
mysql.connector.errors.InterfaceError: 2003: Can't connect to MySQL server on 'localhost:3306' (10061 No connection could be made because the target machine actively refused it)

Process finished with exit code 1

我还禁用了防火墙,“sa”是 SQL Server 的默认用户名,我确定我的密码是真的(因为我使用此信息登录)。谁能帮帮我?

【问题讨论】:

  • 请运行以下命令并提供相应的输出Select user from mysql.user;
  • 您说“sa”是默认用户名,但您的代码使用“admin”作为用户名
  • @ozgursar 是的,我创建了名为“admin”的完整授权用户名,但即使使用 sa 或 admin 它也不起作用。
  • @SamakshGupta 也许我对这个主题很陌生,我需要更清楚地指导,我将您的代码复制到 SSMS 中,终端给了我这个:Incorrect syntax near the keyword 'user'.
  • 您正在使用 MySQL 驱动程序尝试连接到 SQL Server - 它们是完全不同的数据库引擎......它永远不会工作。您需要正确的驱动程序。

标签: python mysql sql-server database python-requests


【解决方案1】:

尝试检查 mysql 服务是否在 localhost 上运行

【讨论】:

  • 感谢您的回复,但这不是问题的答案。
  • 我应该怎么做?你能解释更多吗?
猜你喜欢
  • 2017-04-09
  • 2022-01-19
  • 2021-11-30
  • 2019-05-10
  • 2014-11-06
  • 1970-01-01
  • 2011-06-19
  • 2019-07-29
  • 1970-01-01
相关资源
最近更新 更多