简体   繁体   English

无法将 Python 连接到 MySQL

[英]Unable to connect Python to MySQL

I'm using pycharm 2017 and SQL Server 2019 and my problem is I use this code:我正在使用 pycharm 2017 和 SQL Server 2019,我的问题是我使用以下代码:

import mysql.connector

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

I see this error:我看到这个错误:

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

Also I disabled firewall, "sa" is default username of SQL Server and I'm sure my password is true (because I login with this information).我也禁用了防火墙,“sa”是 SQL 服务器的默认用户名,我确定我的密码是真的(因为我使用此信息登录)。 Anyone can please help me?任何人都可以帮助我吗?

try checking if the mysql service is running on the localhost尝试检查 mysql 服务是否在 localhost 上运行

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

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