简体   繁体   中英

Having trouble connecting with MySQL.connector in python

I am learning how to do MySQL in python using the mysql.connector module. However, whenever I try creating a connection, I get the error "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)".

I done the exact same as the example that I am using states, and it still isn't working.

Below is the two different type of the example provided:

import mysql.connector

mydb = mysql.connector.connect(
  host="localhost",
  user="yourusername",
  password="yourpassword"
)

print(mydb)

and

import mysql.connector

mydb = mysql.connector.connect(
  host="localhost",
  user="myusername",
  password="mypassword"
)

print(mydb)

I tried exactly what the example said, and it didn't work. I also tried using a different username and password, but kept on getting the same error. Any idea what I can do to get the expected output which, according to the second example that I am referring to, is:

<mysql.connector.connection.MySQLConnection object ar 0x016645F0>

?

Thank you in advance

import mySQL.connector
Mybd=mysql.connector.connect(
    host='myusername',
    user='myusername', 
    password='mypassword', 
    database='mydatabase')
Cursor=mycon.cursor()
Cursor.execute('show databases')
Data=cursor.fetchall()
for row in data:
    Print(row)
mycon.close()

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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