使用 SQL 连接器处理 SQL 时出现接口错误

使用 SQL 连接器处理 SQL 时出现接口错误

我想运行使用 sqlconnector 的代码,但是不能,我的代码:

数据库连接.py

import mysql.connector
mydb = mysql.connector.connect(host="localhost", user="yusharthsingh",passwd="xxxxxxxxx")

我的终端输入和输出:

(ritconda) yusharthsingh@pop-os:~/Documents$ python dbconnection.py
Traceback (most recent call last):
  File "/home/yusharthsingh/anaconda3/envs/ritconda/lib/python3.9/site-packages/mysql/connector/network.py", line 509, in open_connection
    self.sock.connect(sockaddr)
ConnectionRefusedError: [Errno 111] Connection refused

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/home/yusharthsingh/Documents/dbconnection.py", line 3, in <module>
    mydb = mysql.connector.connect(host="localhost", user="yusharthsingh",passwd="fcukdwrld")
  File "/home/yusharthsingh/anaconda3/envs/ritconda/lib/python3.9/site-packages/mysql/connector/__init__.py", line 179, in connect
    return MySQLConnection(*args, **kwargs)
  File "/home/yusharthsingh/anaconda3/envs/ritconda/lib/python3.9/site-packages/mysql/connector/connection.py", line 95, in __init__
    self.connect(**kwargs)
  File "/home/yusharthsingh/anaconda3/envs/ritconda/lib/python3.9/site-packages/mysql/connector/abstracts.py", line 716, in connect
    self._open_connection()
  File "/home/yusharthsingh/anaconda3/envs/ritconda/lib/python3.9/site-packages/mysql/connector/connection.py", line 206, in _open_connection
    self._socket.open_connection()
  File "/home/yusharthsingh/anaconda3/envs/ritconda/lib/python3.9/site-packages/mysql/connector/network.py", line 511, in open_connection
    raise errors.InterfaceError(
mysql.connector.errors.InterfaceError: 2003: Can't connect to MySQL server on 'localhost:3306' (111 Connection refused)

答案1

原因可能有很多。最常被提及的一个原因(参见例如https://stackoverflow.com/questions/55790121/cant-connect-to-mysql-server-on-localhost3306-even-though-mysqld-is-running) 是一个小配置问题:MySQL 不听从,localhost但听从127.0.0.1。你可以试试这个吗?

相关内容