【问题标题】:I am trying to connect python and mysql and I am facing the following error:我正在尝试连接 python 和 mysql,但遇到以下错误:
【发布时间】:2020-06-16 13:44:27
【问题描述】:

源代码:

import mysql.connector

from mysql.connector import errorcode
try:
    cnx = mysql.connector.connect(user='root',database='menagerie')
except mysql.connector.Error as err:
    if err.errno == errorcode.ER_ACCESS_DENIED_ERROR:
        print("Something is wrong with your user name or password")
    elif err.errno == errorcode.ER_BAD_DB_ERROR:
        print("Database does not exist")
    else:
        print(err)
else:
    cnx.close()

我面临的错误: 2003: Can't connect to MySQL server on '127.0.0.1:3306' (10061 No connection could be made because the target machine拒绝了)

【问题讨论】:

标签: mysql python-3.x


【解决方案1】:
import mysql.connector
cnx=mysql.connector.connect(user="root",host="localhost",passwd="*******",database="menagerie")

以上代码建立了与MySql的连接。

【讨论】:

  • 这里的host是一个可选参数,但是passwd是建立连接所必需的。
  • mysql.connector.errors.InterfaceError: 2003: Can't connect to MySQL server on 'localhost:3306' (10061 No connection could be made because the target machine拒绝了)
  • ^ 我该怎么办?
  • 当我不包含 passwd 参数时,您的代码会打印“您的用户名或密码有问题”,但如果我包含 passwd,则它可以正常工作。
  • 如果遇到同样的问题,请尝试重新安装mysql客户端。
猜你喜欢
  • 2021-11-04
  • 1970-01-01
  • 1970-01-01
  • 2013-08-06
  • 1970-01-01
  • 2022-06-11
  • 2019-10-04
  • 2023-03-25
  • 2018-04-14
相关资源
最近更新 更多