当我尝试使用Python的MySQL连接器来完成一些学校作业时,我注意到了这个问题。
我使用了正确的语法,代码如下:
import mysql.connector
connector_obj=mysql.connector.connect(host='localhost',user='root',passwd='deven')
my_cursor=connector_obj.cursor()
my_cursor.execute('show databases')
print(my_cursor)下面是我得到的错误消息:
---------------------------------------------------------------------------
ConnectionRefusedError Traceback (most recent call last)
~\AppData\Local\Programs\Python\Python39\lib\site-packages\mysql\connector\network.py in open_connection(self)
573 self.sock.settimeout(self._connection_timeout)
--> 574 self.sock.connect(sockaddr)
575 except IOError as err:
ConnectionRefusedError: [WinError 10061] No connection could be made because the target machine actively refused it
During handling of the above exception, another exception occurred:
InterfaceError Traceback (most recent call last)
d:\...\CS_Board_SQP_Codes.py in
108 import mysql.connector
----> 109 connector_obj=mysql.connector.connect(host='localhost',user='root',passwd='deven')
110 my_cursor=connector_obj.cursor()
111 my_cursor.execute('show databases')
112 print(my_cursor)
~\AppData\Local\Programs\Python\Python39\lib\site-packages\mysql\connector\__init__.py in connect(*args, **kwargs)
271 if HAVE_CEXT and not use_pure:
272 return CMySQLConnection(*args, **kwargs)
--> 273 return MySQLConnection(*args, **kwargs)
274 Connect = connect # pylint: disable=C0103
275
~\AppData\Local\Programs\Python\Python39\lib\site-packages\mysql\connector\connection.py in __init__(self, *args, **kwargs)
105 if kwargs:
106 try:
--> 107 self.connect(**kwargs)
108 except:
109 # Tidy-up underlying socket on failure
~\AppData\Local\Programs\Python\Python39\lib\site-packages\mysql\connector\abstracts.py in connect(self, **kwargs)
1001
1002 self.disconnect()
-> 1003 self._open_connection()
1004 # Server does not allow to run any other statement different from ALTER
1005 # when user's password has been expired.
~\AppData\Local\Programs\Python\Python39\lib\site-packages\mysql\connector\connection.py in _open_connection(self)
348 self._socket = self._get_connection()
349 try:
--> 350 self._socket.open_connection()
351 self._do_handshake()
352 self._do_auth(self._user, self._password,
~\AppData\Local\Programs\Python\Python39\lib\site-packages\mysql\connector\network.py in open_connection(self)
574 self.sock.connect(sockaddr)
575 except IOError as err:
--> 576 raise errors.InterfaceError(
577 errno=2003, values=(
578 self.server_host,
InterfaceError: 2003: Can't connect to MySQL server on 'localhost:3306' (10061 No connection could be made because the target machine actively refused it)我试着手动打开MySQL,看看它是否正常工作。我输入了正确的密码,密码在那里停留了几秒钟,然后窗口关闭了。
对于那些更喜欢看视频的人:Click Here to watch the video
发布于 2021-06-24 17:47:31
您安装了哪种sql连接器?
尝试删除当前已安装的,然后使用下面提到的命令重新安装:
pip install mysql-connector-pythonhttps://stackoverflow.com/questions/67815113
复制相似问题