How to fix InterfaceError: 2003: Can't connect to MySQL server on '127.0.0.1:3306:3306' (11001 getaddrinfo failed)

Question:

my MySQL connection is successful but ran into this interface errror

import mysql.connector

db=mysql.connector.connect(
    host="127.0.0.1:3306",
    user="root",
    passwd="teja",
    database="test"
)
InterfaceError: 2003: Can't connect to MySQL server on '127.0.0.1:3306:3306' (11001 getaddrinfo failed)
Asked By: Sai YuvaTeja

||

Answers:

Take the “:3306” out of the “host” line – mysql connector is adding the port in itself leading to an invalid address.

For future reference if you do need to specify a port then you can just specify a separate parameter like so:

import mysql.connector

db=mysql.connector.connect(
   host="127.0.0.1",
   port="3306",
   user="root",
   passwd="teja",
   database="test"
)

You don’t need to though – 3306 is the default MySQL port and it would appear that’s what you are using.

Answered By: motosubatsu
import mysql.connector

# establishing the connection
conn = mysql.connector.connect(
    user='root', host='localhost', port=3306, database='test')

cursor = conn.cursor()
Answered By: sumir vasanth
Categories: questions Tags: ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.