Shiven Sharma
Shiven Sharma

Reputation: 1

Unable to connect to mysql database in python

I am unable to connect to mysql db in python.

Code -

import mysql.connector

mydb = mysql.connector.connect(host="localhost", user="admin", passwd="1234")

Error -

mysql.connector.errors.InterfaceError: 2003: Can't connect to MySQL server on 'localhost:3306' (61 Connection refused)

What can I do?

Upvotes: 0

Views: 902

Answers (2)

rasjani
rasjani

Reputation: 7970

Connection refused typically indicates that a network connection to a give host/port has failed because a) There is nothing listening on that port in given IP address or b) that port/ip combination is blocked by firewall. c) while a service (in this case, mysql) might be running in your local machine, it might not be binding itself to 127.0.0.1..

First, verify that first 2 of the cases aren't happening.

You could try to use telnet to open a connection, something like this: telnet localhost 3306 - if that gives similar "connection refused" - there's nothing wrong in your code if the server is supposed to be running on localhost and in that port.

Use of netstat could be also used to determine if any of your ethernet devices is listening on port 3306 ..

Upvotes: 1

Hema Jayachandran
Hema Jayachandran

Reputation: 86

Can you try this? Replace localhost to 127.0.0.1 as below:

mydb = mysql.connector.connect(host="127.0.0.1", user="admin", passwd="1234")

Upvotes: 0

Related Questions