我安装了mysql connector/python并使用MAMP。当我尝试连接到数据库时,终端会出现以下情况:

Traceback (most recent call last):
File "database.py", line 7, in <module>
    raise_on_warnings= True)
File "/Library/Python/2.7/site-packages/mysql/connector/__init__.py", line 179, in connect
        return MySQLConnection(*args, **kwargs)
  File "/Library/Python/2.7/site-packages/mysql/connector/connection.py", line 95, in __init__
    self.connect(**kwargs)
  File "/Library/Python/2.7/site-packages/mysql/connector/abstracts.py", line 719, in connect
    self._open_connection()
  File "/Library/Python/2.7/site-packages/mysql/connector/connection.py", line 206, in _open_connection
    self._socket.open_connection()
  File "/Library/Python/2.7/site-packages/mysql/connector/network.py", line 475, in open_connection
    errno=2003, values=(self.get_address(), _strioerror(err)))
mysql.connector.errors.InterfaceError: 2003: Can't connect to MySQL server on 'localhost:8889:3306' (60 Operation timed out)

我试着进入connector文件夹并签出network.py脚本,似乎错误是由接口错误引起的。文件上说:
对于源自Connector/Python本身的错误(与MySQL服务器无关),会引发此异常。
errors.InterfaceError是errors.Error的一个子类
如果需要,这里是我的python脚本:
import mysql.connector

con = mysql.connector.connect(user='root',
                              password ='root',
                              host='localhost',
                              database='testdb',
                              raise_on_warnings= True)

cur = con.cursor()

cur.close()
con.close()

如何消除接口错误?

最佳答案

从主机中删除端口和冒号:

 con = mysql.connector.connect(user='root',
                               password ='root',
                               host='localhost',
                               database='testdb',
                               raise_on_warnings= True)

您的端口已设置为3306

10-08 13:33